I'm writing a program which I could draw some lines with and a the moment I'm testing how does Canvas
and awt.Frame
cooperate. Here is the code:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.ArrayList;
public class Drawing extends Canvas{
Point2D pt1, pt2;
private ArrayList<Shape> linesList;
private Shape shape = null;
public Drawing(){
linesList = new ArrayList<Shape>();
Point p1 = new Point();p1.x = 200;p1.y = 200;
Point p2 = new Point();p2.x = 300;p2.y = 300;
pt1 = (Point2D) p1;
pt2 = (Point2D) p2;
//Line k = new Line(p1,p2,Color.BLUE);
shape = new Line2D.Double ( pt1, pt2 );
linesList.add(shape);
}
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D) g;
//g2d.drawLine(200, 200, 200, 300);
//Shape k = null;
//k = new Shape();
//g2d.setPaint(Color.BLACK );
g2d.setColor(Color.GREEN);
for ( Shape line : linesList )
{
g2d.draw(line);
}
}
public static void main(String[] args){
Frame f = new Frame("Rysuje");
f.setResizable(false);
f.setSize(600, 600);
f.setVisible(true);
Drawing myFrame = new Drawing();
f.add(myFrame);
}
}
My question is: why g2d.draw(line);
in paint()
method does not draw a line? In fact it doesn't draw anything. But when I write g2d.drawLine(200, 200, 200, 300);
it works...