-1

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...

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
lvi
  • 169
  • 1
  • 2
  • 8
  • 1
    1) Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). 2) A single blank line of white space in source code is *always* enough. Blank lines after `{` or before `}` are also typically redundant. – Andrew Thompson Nov 16 '13 at 01:54
  • 2
    *"why `g2d.draw(line);` in `paint()` method does not draw a line?"* I see a green line here.. – Andrew Thompson Nov 16 '13 at 02:03
  • 1) I know Swing is better but it's an exercise my class does so I have no choice. 2) Sorry for that - I was in a hurry. If you see a green line it's the the line that draws before the for loop in paint(); Still don't understand why won't the for loop execute as predicted and draw a line from ArrayList. – lvi Nov 16 '13 at 09:36
  • 1
    *"it's an exercise my class does so I have no choice."* My sympathies. OTOH I **do** have a choice and have better things to do than help out on APIs that should not be used. Good luck. – Andrew Thompson Nov 16 '13 at 09:46
  • "OTOH I do have a choice and have better things to do than help out on APIs that should not be used." I respect that. Can you write why AWT shouldn't be used ? Just one or two short argument's so I can ask my teacher about them. PS. Problem solved. – lvi Nov 16 '13 at 12:14
  • OK.. your gracious acceptance of my decision has turned me around on the matter. I **do** enjoy helping such people (more than I dislike working with old APIs). So.. I'm going to post the source I developed. It is **not an answer** and I will need to delete it soon (before it is flagged as not being an answer, deleted by a moderator and me fined 200 rep. points ;). So please let me know when you've seen it. The thing is, I still do not quite understand what you are seeing. – Andrew Thompson Nov 16 '13 at 12:25
  • *"Can you write why AWT shouldn't be used ?"* Not only did I already do that, but I *linked* to an answer that explains. – Andrew Thompson Nov 16 '13 at 16:56

1 Answers1

1

Using this source..

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.setColor(Color.RED);
        g2d.drawLine(20, 20, 100, 100);
        //Shape k = null;
        //k = new Shape();
        //g2d.setPaint(Color.BLACK );
        g2d.setColor(Color.GREEN);
        for ( Shape line : linesList )
        {
            g2d.draw(line);
        }
        g2d.setColor(Color.BLUE);
    }

    public static void main(String[] args){
        Frame f = new Frame("Rysuje");

        f.setResizable(false);
        f.setSize(400, 400);
        f.setVisible(true);
        Drawing myFrame = new Drawing();

        f.add(myFrame);
    }
}

..I see this:

enter image description here

Now it appears to me that the green line is appearing, so I am a little confused about what it is you expect to see that is different. Do you see the same thing?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I see the same thing you see. I think my NetBeans produced the problem. Thanks ! – lvi Nov 16 '13 at 13:38
  • Yes, I wrote that in a comment before you posted the code. Anyway, thank you for your time. – lvi Nov 16 '13 at 13:47