| | |
Mouse listener
![]() |
•
•
Join Date: Mar 2008
Posts: 56
Reputation:
Solved Threads: 1
I am trying to write a program that displays a rectangle when user drag the mouse and when user release mouse, it displays the rectangle with green color.
I am terribly confused here is what I am thinking to do
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class DragRectangle extends JApplet
{
public void paint(Graphics g)
{
super.paint(g)
}
public void mouseDragged(MouseEvent e)
{
e.setColor(Color.GREEN)
e.drawRect(I am not sure what the coordinate will be)
repaint();
}
Am I in right track? Any suggestion or help will be highly appreciated!!
I am terribly confused here is what I am thinking to do
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class DragRectangle extends JApplet
{
public void paint(Graphics g)
{
super.paint(g)
}
public void mouseDragged(MouseEvent e)
{
e.setColor(Color.GREEN)
e.drawRect(I am not sure what the coordinate will be)
repaint();
}
Am I in right track? Any suggestion or help will be highly appreciated!!
•
•
Join Date: Sep 2008
Posts: 1,563
Reputation:
Solved Threads: 196
I've never used MouseListener, but your mouseDragged code is wrong. "e" refers to a MouseEvent object, NOT your rectangle. Lets say you had an Object that represented your rectangle, here's what your code should look similar to.
Java Syntax (Toggle Plain Text)
//Note this shouldn't be of type object, I'm just showing that it is an object Object myRectangle; public void mouseDragged(MouseEvent e){ Object source= e.getSource(); if (source == myRectangle){ myRectangle.setColor(whatever.GREEN); //call some repaint method here } }
This should give you a start to work from. Similar, but different enough that you'll have to figure out the rectangle part.
java Syntax (Toggle Plain Text)
import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import javax.swing.JApplet; import javax.swing.JPanel; public class PaintApplet extends JApplet { public void init() { getContentPane().add(new CanvasPanel()); } class CanvasPanel extends JPanel implements MouseMotionListener{ Color backColor = Color.WHITE; Color foreColor = Color.BLUE; Point p0 = new Point(0,0); Point p1 = new Point(0,0); public CanvasPanel(){ super(); addMouseMotionListener(this); } @Override protected void paintComponent(Graphics g) { g.setColor(backColor); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(foreColor); g.drawLine(p0.x, p0.y, p1.x, p1.y); } public void mouseDragged(MouseEvent e) { p1.x = e.getX(); p1.y = e.getY(); repaint(); } public void mouseMoved(MouseEvent e) { // nothing here - just want drag operations } } }
•
•
Join Date: Mar 2008
Posts: 56
Reputation:
Solved Threads: 1
so far I got upto this part! can anyone help me how to proceed from here:
/codeimport javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class DragRectangle extends JApplet
{
private int previousX, previousY;
private boolean drag;
/* public void init()
{
addMouseDragged(this);
addMouseReleased(this);
}*/
public void paint(Graphics g)
{
super.paint(g);
}
public void mouseDragged(MouseEvent event)
{
//e.setColor(Color.GREEN)
//e.drawRect(
if(drag==false)
{
return;
}
int x = event.getX();
int y = event.getY();
previousX=x;
previousY=y;
//Graphics g = getGraphics();
//event.setColor(Color.YELLOW);
//event.drawRect(x,y,10,20);
}
public void mouseReleased(MouseEvent evt)
{
g.setColor(Color.YELLOW);
g.drawRect(previousX, previousY, getWidth(),getHeight());
repaint();
}
}/code
thanks
/codeimport javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class DragRectangle extends JApplet
{
private int previousX, previousY;
private boolean drag;
/* public void init()
{
addMouseDragged(this);
addMouseReleased(this);
}*/
public void paint(Graphics g)
{
super.paint(g);
}
public void mouseDragged(MouseEvent event)
{
//e.setColor(Color.GREEN)
//e.drawRect(
if(drag==false)
{
return;
}
int x = event.getX();
int y = event.getY();
previousX=x;
previousY=y;
//Graphics g = getGraphics();
//event.setColor(Color.YELLOW);
//event.drawRect(x,y,10,20);
}
public void mouseReleased(MouseEvent evt)
{
g.setColor(Color.YELLOW);
g.drawRect(previousX, previousY, getWidth(),getHeight());
repaint();
}
}/code
thanks
@complexcodes
Seems like you are confused in using CODE tags use [ CODE=java] and [ /CODE ] (without the spaces between CODE and brackets)to cover your java code.
Seems like you are confused in using CODE tags use [ CODE=java] and [ /CODE ] (without the spaces between CODE and brackets)to cover your java code.
Last edited by stephen84s; Nov 1st, 2008 at 10:30 am.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
"How to ask questions the smart way ?"
"How to ask questions the smart way ?"
Why not start with the applet that I wrote for you that is very close to what you want to do? Did I completely waste my time trying to give you a simple, solid starting point?
If you will glance at that code, notice that all operations using the Graphics class occur in the paintComponent() method and those method calls just use the coordinate variables in the class. The mouse listener alters those coordinates based upon the x and y values supplied by the mouse event and then calls repaint(), which in turn calls paintComponent() as part of the rendering process.
All you need to do is alter the code in the mouse listener and the paintComponent() method to draw your rectangle instead of the simple line I used as an example.
If you will glance at that code, notice that all operations using the Graphics class occur in the paintComponent() method and those method calls just use the coordinate variables in the class. The mouse listener alters those coordinates based upon the x and y values supplied by the mouse event and then calls repaint(), which in turn calls paintComponent() as part of the rendering process.
All you need to do is alter the code in the mouse listener and the paintComponent() method to draw your rectangle instead of the simple line I used as an example.
@stephen84s
To exclude a piece of text as being interpreted as BBCode, use the
Source for above: [noparse][code=java]your code here[/code][/noparse]
To exclude a piece of text as being interpreted as BBCode, use the
noparse tag. With the use of noparse, you can write something along the lines of [code=java]your code here[/code] and be sure it won't be interpreted by the BBCode parser of vBulletin.Source for above: [noparse][code=java]your code here[/code][/noparse]
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- No action under action listener (ActionListener added!) (Java)
- Mouse Cursor Image (Java)
- JSpinner mouse event (Java)
- Adding mouse listener to a frame (Java)
- Java Visualizations with media (Game Development)
- Applet Help (Java)
- Is there a JOptionPane Listener? (Java)
- Java GUI--Dynamically Adding Buttons to a Menu (Java)
Other Threads in the Java Forum
- Previous Thread: can anyone find my mistake in TrainingZone.java
- Next Thread: Need help from java expert
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card chat class client code collision component crashcourse css csv database eclipse ee error fractal free game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan machine map method methods migrate mobile netbeans newbie objects oriented output panel phone physics problem program programming project projects radio recursion replaydirector reporting scanner se server service set sms socket software sort sql string swing test textfield threads transfer tree trolltech ubuntu utility windows






