questions about implementation of drawing triangle in java

I am learning myself about drawing in java , I found this code , I have some questions about the implementation of the code , Please see the code below :

package shapes;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.event.WindowAdapter;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

import com.sun.glass.events.WindowEvent;

public class drawTriangle extends Frame {

    public Polygon mypolygon = new Polygon();

    public void paint(Graphics g) {
        Graphics2D ga = (Graphics2D) g;
        ga.setPaint(Color.red);
        ga.drawPolygon(mypolygon);

    }


    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        list.add(200);
        list.add(100);
        list.add(950);
        list.add(950);
        list.add(100);
        list.add(200);
        list.add(list.get(0));
        list.add(list.get(1));

        drawTriangle frame = new drawTriangle();
        for (int i=0; i<list.size(); i++ ) {
            int x= list.get(i++);
            int y= list.get(i);

            frame.mypolygon.addPoint(x,y);

        }

        frame.addWindowStateListener(new WindowAdapter()

            { public void windowClosing(WindowEvent we)
            {
                System.exit(0);
            }

            });
        frame.setSize(400,400);
        frame.setVisible(true);


    }

}

First question ) When I run the code I get a window with a drawn triangle … When I press to remove the window it does not work.. I tried to put frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); or frame.setDefaultCloseOperation(drawTriangle.EXIT_ON_CLOSE); but does not work. I changed System.exit(0); to System.exit(1); but does not remove the window . How to remove the window after I run the code ?

Second question ) what is the difference between public Polygon mypolygon = new Polygon(); and drawTriangle frame = new drawTriangle(); ?

Third question ) what is the meaning of the code below !!!

int x= list.get(i++);
int y= list.get(i);

Forth question ) what does this do Graphics2D ga = (Graphics2D) g; ?

Thanks

Answer

  1. You should never overwrite pant method, you should use paintComponent in this case. If you do, you should call super.paint(g) at the beginning of your paint (this is why your window doesn’t close with setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)).
  2. Polygon is graphics primitive, Graphics is graphics context of drawing component, but swing always allocates Graphics2D which more featured.
  3. If you want to use more featured class you need to cast.
  4. new Polygon creates new primitive for drawing, new drawTringle creates new JFrame.
  5. In main x and y coordinates are collected on linear list. Then they unrolled to get x,y pair again.

Leave a Reply

Your email address will not be published. Required fields are marked *