Drawing rectangle using mouse [duplicate]
Possible Duplicate:
How to draw a rectangle on a java applet using mouse drag event and make it stay
Hello. I would like to know how I can draw rectangle using mousedragged event. I know that I must record mouse location using class Point. I need to implement paint function in paint method and in mousedragged call it or my paint code should implement in mousedragged event?
At this time I put my code in mouse dragged event. This is code:
@Override
public void mousePressed(MouseEvent e)
{
super.mousePressed(e);
System.out.println("f.getGlassPane() mousePressed");
if(e.getButton() == MouseEvent.BUTTON1)
frame.getGlassPane().setVisible(true);
startPoint=e.getPoint();
Graphics2D g = null;
Graphics2D g2 = (Graphics2D) g;
Rectangle2D prostokat = new Rectangle2D.Double();
prostokat.setFrameFromDiagonal(e.getPoint().x, e.getPoint().y,startPoint.x, startPoint.y);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5F));
g2.setColor(Color.BLUE);
g2.fill(prostokat);
g2.draw(prostokat);
}
});
You'll also need to handle mouseReleased
and mouseDragged
, as shown here and here.
精彩评论