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?
Does anyone know why it erases everything when i move another window over my container?
if you are drawing via getGraphics(), you will get what you've described.the correct way is to do the drawing in paintComponent(..)
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.
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);
}
}
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.
Nevermind, just got rid of the super.paintComponent() statement and it made a trail. HOWEVERIt still gets erased every time something moves over it.
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);
}
}
}