Painting Issue

I've made a simple program that allows the user to paint into a JPanel area. It works fine except for the fact that it erases every time another window crosses over it. so basically if I draw something, then minimize the program or drag another window in front of it, everything I've drawn gets erased. How can I prevent this?

[335 byte] By [llampwalla] at [2007-10-3 2:40:00]
# 1
Does anyone know why it erases everything when i move another window over my container?
llampwalla at 2007-7-14 19:38:17 > top of Java-index,Java Essentials,New To Java...
# 2
if you are drawing via getGraphics(), you will get what you've described.the correct way is to do the drawing in paintComponent(..)
Michael_Dunna at 2007-7-14 19:38:17 > top of Java-index,Java Essentials,New To Java...
# 3

Yea I read about paintComponent, but I'm still unsure of how to switch the two. this is what I have now:

public Painter() {

super ("Drag to paint");

JButton printbutton = new JButton("Print");

printbutton.addActionListener(this);

getContentPane().add(printbutton, BorderLayout.SOUTH);

addWindowListener(new ExitListener());

addMouseMotionListener (new MouseMotionAdapter() {

public void mouseDragged(MouseEvent e) {

xval = e.getX();

yval = e.getY();

repaint();

}

});

setSize(500,500);

setVisible(true);

}

public void paint(Graphics g) {

g.setColor(Color.green);

g.fillOval(xval, yval, 10, 10);

}

I can't just change paint() to paintComponent and change repaint() to update(). So I don't know what to do.

llampwalla at 2007-7-14 19:38:17 > top of Java-index,Java Essentials,New To Java...
# 4

try this (it's only a rough demo)

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class Testing extends JFrame

{

public Testing()

{

setLocation(200,100);

setDefaultCloseOperation(EXIT_ON_CLOSE);

getContentPane().add(new Painter());

pack();

}

public static void main(String[] args){new Testing().setVisible(true);}

}

class Painter extends JPanel

{

int xval = 0, yval = 0;

public Painter()

{

setBackground(Color.WHITE);

setPreferredSize(new Dimension(500,500));

addMouseMotionListener (new MouseMotionAdapter() {

public void mouseDragged(MouseEvent e) {

xval = e.getX();

yval = e.getY();

repaint();

}

});

}

public void paintComponent(Graphics g)

{

super.paintComponent(g);

g.setColor(Color.green);

g.fillOval(xval, yval, 10, 10);

}

}

Michael_Dunna at 2007-7-14 19:38:17 > top of Java-index,Java Essentials,New To Java...
# 5
Using that code, the paint doesnt actually stay at all. It just makes a dot that moves when the mouse is dragged, never leaving a trail.
llampwalla at 2007-7-14 19:38:17 > top of Java-index,Java Essentials,New To Java...
# 6
Nevermind, just got rid of the super.paintComponent() statement and it made a trail. HOWEVERIt still gets erased every time something moves over it.
llampwalla at 2007-7-14 19:38:17 > top of Java-index,Java Essentials,New To Java...
# 7

for a trail, try this

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class Testing extends JFrame

{

public Testing()

{

setLocation(200,100);

setDefaultCloseOperation(EXIT_ON_CLOSE);

getContentPane().add(new Painter());

pack();

}

public static void main(String[] args){new Testing().setVisible(true);}

}

class Painter extends JPanel

{

//int xval = 0, yval = 0;

java.util.List points = new java.util.ArrayList();//<--added

public Painter()

{

setBackground(Color.WHITE);

setPreferredSize(new Dimension(500,500));

points.add(new Point(0,0));

addMouseMotionListener (new MouseMotionAdapter() {

public void mouseDragged(MouseEvent e) {

//xval = e.getX();

//yval = e.getY();

points.add(e.getPoint());

repaint();

}

});

}

public void paintComponent(Graphics g)

{

super.paintComponent(g);

g.setColor(Color.green);

for(int x = 0, y = points.size(); x < y; x++)//<-changed all this

{

Point p = (Point)points.get(x);

g.fillOval(p.x, p.y, 10, 10);

}

}

}

Michael_Dunna at 2007-7-14 19:38:17 > top of Java-index,Java Essentials,New To Java...