Problem repainting

S

Sadalsud

I'm trying to extend JFrame so I can do some basic line drawing. In the example
below, only one red line is supposed to be drawn diagonally. The problem I am
having is that whenever I resize the Window, the previous red diagonal lines do
not disappear. However, if I switch to another application that obscures the
entire window and switch back, the desired behavior is observed. I'm running
Java 1.4.1 on Windows 98. Any help that can be offered in resolving this matter
would be greatly appreciated.

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

public class FooFrame extends JFrame
{
public static void main(String args[])
{
FooFrame f;
f = new FooFrame();
}

public FooFrame()
{
super("FooFrame");
setBackground(Color.black);
setResizable(true);
setSize(300, 300);
show();
}

public void paint(Graphics g)
{
Graphics2D g2;
Line2D line;

g2 = (Graphics2D)g;
g2.setPaint(Color.red);

line = new Line2D.Double(0, 0, getSize().width, getSize().height);
g2.draw(line);
}

}
 
T

Tom Cole

You're painting directly to the JFrame, don't do that.

You need to either paint to the contentPane or better yet you should create
your painting in a JPanel and set it as the contentPane for a JFrame.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top