This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes code not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "code not working" Watch "code not working" New topic
Author

code not working

vijayalakshmi sundararajan
Greenhorn

Joined: Dec 03, 2003
Posts: 4
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

Joined: Oct 18, 2000
Posts: 4121

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.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
Have you checked out Aspose?
 
subject: code not working
 
It's not a secret anymore!