What is the difference between mousePressed and mouseDragged in my program?
In a drawing program like Microsoft’s PaintTM, one of the features available is drawing straight lines. You can draw a straight line by pressing down on the mouse to indicate where the line should start. Then, with the mouse still pressed down, you may move the mouse (a mouse drag), and the end point of the line is created as you move. When you release the mouse, the line remains. You may repeat this process many times, creating many different lines in your PaintTM document.
If you wish to emulate this effect in a java program, you may use MouseListener (the mousePressed method), and MouseMotionListener (the mouseDragged method), to create a line segment. In addition, we would like to be able to clear the paint area with a “clear button” at the top. Also, we would like to change the colors of all of the lines by placing some “color buttons” at the top. In order to accomplish all of this, you will need to use arrays of coordinates, because each time you call repaint, you will need to redraw all of the lines stored. */
import java.awt.*;
import java.awt.event.*; //brings in the ability to read loops by 'event' of something
import javax.swing.*; //interface that extends both the MouseMotionListener and MouseListener interfaces
public class Draw extends JApplet implements MouseListener, MouseMotionListener
{
private int [] x;
private int [] y;
private boolean changed = true;
Display draw;
public void init()
{
draw = new Display(); //use Display not draw
setContentPane(draw); //lets you draw the stuff
draw.setBackground(Color.green); //sets the background color to whatever you want
draw.setFont (new Font("SansSerif", Font.BOLD, 24)); //this sets the font size and type
draw.addMouseListener(this);
draw.addMouseMotionListener(this); //adds the MouseMotionListener
} //to read in actions of the mouse
class Display extends JPanel
{
public void paintComponent(Graphics g) //Graphics __ <--name can be anything
{
super.paintComponent(g); //paintComponent(__) <-- __ has to match w/ the one above
g.setColor(Color.black); //these are the 5 buttons at the top
g.fillRect(2, 2, 95, 70); //
g.setColor(Color.red); //
g.fillRect(100, 2, 95, 70); //
g.setColor(Color.blue); //
g.fillRect(198, 2, 95, 70); //
g.setColor(Color.gray); //
g.fillRect(296, 2, 95, 70); //
g.setColor(Color.cyan); //
g.fillRect(394, 2, 95, 70); //
g.setColor(Color.white);
g.drawString("RESET", 10, 45);
g.drawString("RED", 125, 45);
g.drawString("BLUE", 215, 45);
g.drawString("GRAY", 310, 45);
g.drawString("CYAN", 410, 45);
}
}
public void mousePressed(MouseEvent evt)
{
int x = evt.getX();
int y = evt.getY();
changed = false;
if (y > 2 && y < 70)
{
changed = true;
if (x > 2 && x < 100)
{
draw.repaint();
}
else
开发者_运维知识库 changed = false;
}
}
public void mouseDragged(MouseEvent evt)
{
}
public void mouseReleased(MouseEvent evt) {}
public void mouseMoved(MouseEvent evt) {}
public void mouseEntered(MouseEvent evt) {} // Some empty routines.
public void mouseExited(MouseEvent evt) {} // (Required by the MouseListener
public void mouseClicked(MouseEvent evt) {} // and MouseMotionListener interfaces).
}
mousePressed should fire when someone clicks (i.e. when they push down the button on the mouse - not release).
mouseDragged should fire after the person pushes the mouse button, and subsequently moves the mouse.
So, you might consider storing the x,y coordinates of the mouse on mousePressed, and then drawing a line between that x,y position and the current mouse x,y position when mouseDragged has fired.
精彩评论