J
Jenny
Hi,
Could you tell me why the code comp2D.setBackground(Color.white); does
not make the color white? The code comp2D.fillRect(0, 0, 400, 400);
works.
Thank you very much.
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
public class Line extends JFrame {
public Line() {
super("Line");
setSize(410, 430);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
LinePanel lp = new LinePanel();
Container content = getContentPane();
content.add(lp);
setContentPane(content);
setVisible(true);
}
public static void main(String[] arguments) {
Line line = new Line();
}
}
class LinePanel extends JPanel {
public void paintComponent(Graphics comp) {
super.paintComponent(comp);
Graphics2D comp2D = (Graphics2D) comp;
comp2D.setBackground(Color.white);
// comp2D.fillRect(0, 0, 400, 400);
Line2D.Double l1 = new Line2D.Double(10,200,390,200);
Line2D.Double l2 = new Line2D.Double(200,10,200,390);
BasicStroke pen = new BasicStroke(5F);
comp2D.setColor(Color.blue);
comp2D.draw(l1);
comp2D.draw(l2);
}
}
Could you tell me why the code comp2D.setBackground(Color.white); does
not make the color white? The code comp2D.fillRect(0, 0, 400, 400);
works.
Thank you very much.
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
public class Line extends JFrame {
public Line() {
super("Line");
setSize(410, 430);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
LinePanel lp = new LinePanel();
Container content = getContentPane();
content.add(lp);
setContentPane(content);
setVisible(true);
}
public static void main(String[] arguments) {
Line line = new Line();
}
}
class LinePanel extends JPanel {
public void paintComponent(Graphics comp) {
super.paintComponent(comp);
Graphics2D comp2D = (Graphics2D) comp;
comp2D.setBackground(Color.white);
// comp2D.fillRect(0, 0, 400, 400);
Line2D.Double l1 = new Line2D.Double(10,200,390,200);
Line2D.Double l2 = new Line2D.Double(200,10,200,390);
BasicStroke pen = new BasicStroke(5F);
comp2D.setColor(Color.blue);
comp2D.draw(l1);
comp2D.draw(l2);
}
}