File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

code not working

 
vijayalakshmi sundararajan
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I tried out this code for freehand drawing using mouse.But its not working for me.Can anyone tell me where I went wrong?
the code i hv written is as below
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Painter extends JFrame
{
private int xValue = -10, yValue = -10;
public Painter()
{
super( " Hand free drawing page ");
getContentPane().add(
new Label(" Drag mouse to draw the desired picture "),
BorderLayout.SOUTH);
addMouseMotionListener(
new MouseMotionAdapter()
{
public void mouseDragger( MouseEvent e)
{
xValue = e.getX();
yValue = e.getY();
repaint();
}
}
);
setSize(300,150);
show();
}
public void paint( Graphics g)
{
g.fillOval(xValue,yValue,4,4);
}
public static void main(String arg[])
{
Painter app = new Painter();
app.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
}
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're painting on the JFrame itself, which is covered by the content pane. Make another component, make it listen for mouse movement and paint, and add it to the content pane.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic