I'm simply trying to I’m simply trying to draw a circle in the same location as mouse click, but I can’t get it to work. Here is the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LineApp
{
public static void main ( String args[])
{
MainWindow mainWdw = new MainWindow();
}
}
class MainWindow extends Frame
{
private Point recPoint;
private boolean mouseClick;
MainWindow()
{
mouseClick = false;
setBackground( Color.BLACK );
setSize( 400,300 );
setLocation( new Point( 300,300));
addComponentListener(new ComponentCatcher());
addWindowListener(new WindowCatcher());
addMouseListener(new MouseCatcher());
setVisible( true );
}
public void paint( Graphics gc )
{
gc.setColor(Color.ORANGE);
if(mouseClick)
{
gc.setColor(Color.ORANGE);
gc.fillOval(recPoint.x, recPoint.y, 30,30);
}
}
class WindowCatcher extends WindowAdapter
{
public void windowClosing( WindowEvent evt)
{
evt.getWindow().dispose();
System.exit (0);
}
}
class ComponentCatcher extends ComponentAdapter
{
public void componentResized (ComponentEvent evt)
{
repaint();
}
}
class MouseCatcher extends MouseAdapter
{
public void mousedPressed ( MouseEvent evt)
{
Point mousePt = new Point();
mousePt = evt.getPoint();
recPoint = new Point(mousePt.x,mousePt.y);
mouseClick = true;
repaint();
开发者_如何学JAVA }
}
}
The method in MouseAdapter
is called mousePressed
, not mousedPressed
.
The @Override
-annotation can help you to avoid errors like that. If you would have used
@Override
public void mousedPressed ( MouseEvent evt)
{
...
your code would not have compiled.
精彩评论