nightmare with with repaint
Hi There,
I am making a program where when you draw a line with the mouse, in frame using class scribble, it is duplicated automtically in frame4.
I can draw the lines fine in frame using Scribble class, and I can duplicate them in frame4 using repaint(). The problem then, is that the system gets stuck and freezes becuase repaint() is constantly going. What do I need to do, to fix?
Class line2 contains the code to redraw what I am doing with class Scribble.
thanks
else if ( "White Board".equals(actionCommand))
{
System.out.println("white board");
// Create a frame
JFrame frame = new JFrame("White Board");
JFrame frame4 = new JFrame("TEMP White Board");
// Create an instance of the applet
Scribble applet = new Scribble();
// Scribble applet2 = new Scribble();
// Add the applet instance to the frame
frame.getContentPane().add(applet, BorderLayout.CENTER);
frame4.getContentPane().add(new lines2() );
// Invoke init() and start()
applet.init();
applet.start();
//applet2.init();
//applet2.start();
// Display the frame
frame.setLocation(300 , 10);
frame.setSize(400, 300);
frame.setVisible(true);
frame4.setLocation(300 , 310);
frame4.setSize(400, 300);
frame4.setVisible(true);
}
public class Scribble extends JApplet
{
// Initialize the applet
public void init()
{
// Create a PaintPanel and add it to the applet
getContentPane().add(new ScribblePanel(), BorderLayout.CENTER);
//frame2.getContentPane().add(new ScribblePanel(), BorderLayout.CENTER);
}
}
// ScribblePanel for scribbling using the mouse
class ScribblePanel extends JPanel implements MouseListener, MouseMotionListener,
ActionListener
{
final int CIRCLESIZE = 20; // Circle diameter used for erasing
private Point lineStart = new Point(0, 0); // Line start point
private Graphics g; // Create a Graphics object for drawing
JButton butClear = new JButton("Clear");
public ScribblePanel()
{
// Register listener for the mouse event
addMouseListener(this);
addMouseMotionListener(this);
butClear.addActionListener(this);
add(butClear);
System.out.println("scrib1" + "constructor");
//startline();
//drawline();
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
g = getGraphics(); // Get graphics context
if (e.isMetaDown()) // Detect right button pressed
{
// Erase the drawing using an oval
g.setColor(getBackground());
g.fillOval(e.getX() - (CIRCLESIZE/2), e.getY() - (CIRCLESIZE/2), CIRCLESIZE, CIRCLESIZE);
}
else
{
//left mouse button
g.setColor(Color.black);
//g.drawLine(lineStart.x, lineStart.y,e.getX(), e.getY());
g.drawLine(startX, startY,e.getX(), e.getY());
dragX = e.getX();
dragY = e.getY();
System.out.println("Drag X " + dragX);
System.out.println("Drag Y " + dragY);
}
//lineStart.move(e.getX(), e.getY()); //captures end of x&y of mouse cursor only
endX = e.getX();
endY = e.getY();
System.out.println("endX " + endX);
System.out.println("endY " + endY);
// Dispose this graphics context
g.dispose();
}
public void mousePressed(MouseEvent e)
{
//lineStart.move(e.getX(), e.getY()); //captures start x&y of mouse cursor only
startX = e.getX();
startY = e.getY();
System.out.println("Start X " + startX);
System.out.println("Start Y " + startY);
}
public void mouseDragged(MouseEvent e)
{
}
public void mouseMoved(MouseEvent e)
{
}
public void actionPerformed(ActionEvent evt)
{
System.out.println("Clear");
g = getGraphics(); // Get graphics context
g.setColor(getBackground());
g.fillRect(0, 0,600,600);
g.dispose();
}
}
public class lines2 extends JPanel
{
int countbreaker;
private Point lineStart2 = new Point(0, 0); // Line start point
public void paintComponent(Graphics h)
{
super.paintComponent(h);
h.setColor(Color.black);
// while (countbreaker == 0)
// {
if (startX != 0 && startY != 0)
{
lineStart2.move(startX, startY); //captures start x&y of mouse cursor only
}
if (dragX >= 0 && dragY >= 0)
{
h.drawLine(startX, startY,dragX, dragY);
}
repaint();
h.dispose();
countbreaker = 100;
// }
}
}

