about drawing a Polygon in java

83,725

Solution 1

JFrame does not have a paintComponent(Graphics g) method. Add the @Override annotation and you will get a compile time error.

1) Use JPanel and override paintComponent (you would than add JPanel to the JFrame viad JFrame#add(..))

2) Override getPreferredSize() to return correct Dimensions which fit your drawing on Graphics object or else they wont be seen as JPanel size without components is 0,0

3) dont call setSize on JFrame... rather use a correct LayoutManager and/or override getPrefferedSize() and call pack() on JFrame after adding all components but before setting it visible

4) Have a read on Concurrency in Swing specifically about Event Dispatch Thread

5) watch class naming scheme should begin with a capital letter and every first letter of a new word thereafter should be capitalized

6) Also you extend JFrame and have a variable JFrame? Take away the extend JFrame and keep the JFrame variable as we dont want 2 JFrames and its not good practice to extend JFrame unless adding functionality

Here is your code with above fixes (excuse picture quality but had to resize or it was going to 800x600):

enter image description here

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Polygon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JRisk {

    private JFrame mainMap;
    private Polygon poly;

    public JRisk() {

        initComponents();

    }

    private void initComponents() {

        mainMap = new JFrame();
        mainMap.setResizable(false);

        mainMap.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        int xPoly[] = {150, 250, 325, 375, 450, 275, 100};
        int yPoly[] = {150, 100, 125, 225, 250, 375, 300};

        poly = new Polygon(xPoly, yPoly, xPoly.length);
        JPanel p = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.BLUE);
                g.drawPolygon(poly);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(800, 600);
            }
        };
        mainMap.add(p);
        mainMap.pack();
        mainMap.setVisible(true);

    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JRisk();
            }
        });
    }
}

As per your comment:

i am preparing a map which includes lots of polygons and yesterday i used a JPanel over a JFrame and i tried to check if mouse was inside of the polygon with MouseListener. later i saw that mouseListener gave false responds (like mouse is not inside of the polygon but it acts like it was inside the polygon). so i deleted the JPanel and then it worked

Here is updated code with MouseAdapter and overridden mouseClicked to check if click was within polygon.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JRisk {

    private JFrame mainMap;
    private Polygon poly;

    public JRisk() {
        initComponents();
    }

    private void initComponents() {

        mainMap = new JFrame();
        mainMap.setResizable(false);

        mainMap.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        int xPoly[] = {150, 250, 325, 375, 450, 275, 100};
        int yPoly[] = {150, 100, 125, 225, 250, 375, 300};

        poly = new Polygon(xPoly, yPoly, xPoly.length);

        JPanel p = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.BLUE);
                g.drawPolygon(poly);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(800, 600);
            }
        };

        MouseAdapter ma = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent me) {
                super.mouseClicked(me);

                if (poly.contains(me.getPoint())) {
                    System.out.println("Clicked polygon");
                }

            }
        };
        p.addMouseListener(ma);//add listener to panel
        mainMap.add(p);

        mainMap.pack();
        mainMap.setVisible(true);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JRisk();
            }
        });
    }
}

Solution 2

JFrame does not extend JComponent so does not override paintComponent. You can check this by adding the @Override annotation.

To get this functionality extract paintComponent to a new class which extends JComponent. Don't forget to call super.paintComponent(g) rather than super.paintComponents(g).

Share:
83,725
quartaela
Author by

quartaela

always curious...

Updated on December 19, 2020

Comments

  • quartaela
    quartaela over 3 years

    hi there i'm trying to improve myself about java2D and first of all i'm dealing with drawing polygons. However, i can not see the polygon on frame. I read some tutorials and examples but as i said i face with problems. here is the sample code of drawing a polygon;

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Polygon;
    
    import javax.swing.JFrame;
    
    public class jRisk extends JFrame {
    
    
        private JFrame mainMap;
        private Polygon poly;
    
        public jRisk(){
    
            initComponents();
    
        }
    
        private void initComponents(){
    
            mainMap = new JFrame();
            mainMap.setSize(800, 600);
            mainMap.setResizable(false);
    
            mainMap.setVisible(true);
            mainMap.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            int xPoly[] = {150,250,325,375,450,275,100};
            int yPoly[] = {150,100,125,225,250,375,300};
    
            poly = new Polygon(xPoly, yPoly, xPoly.length);
    
        }
    
        protected void paintComponent(Graphics g){
    
            super.paintComponents(g);
    
            g.setColor(Color.BLUE);
            g.drawPolygon(poly);
    
        }   
    
        /**
         * @param args
         */
        public static void main(String[] args) {
    
            new jRisk();
    
        }
    
    }
    
  • quartaela
    quartaela about 11 years
    ok but in here http://stackoverflow.com/questions/15167342/arrayindexoutofb‌​oundsexception-error‌​-while-drawing-a-pol‌​ygon @camickr says that i should use paintComponent() if i going to use swing ?
  • quartaela
    quartaela about 11 years
    well i was using paint() method however, someone told that if i going to use swing rather than awt than i should override paintComponent()
  • quartaela
    quartaela about 11 years
    ok now it says i override the paint method however it doesnt show the polygon
  • quartaela
    quartaela about 11 years
    well actually i am preparing a map which includes lots of polygons and yesterday i used a JPanel over a JFrame and i tried to check if mouse was inside of the polygon with MouseListener. later i saw that mouseListener gave false responds (like mouse is not inside of the polygon but it acts like it was inside the polygon). so i deleted the JPanel and then it worked.
  • David Kroukamp
    David Kroukamp about 11 years
    @quartaela The problem was not the JPanel IMO it was else where
  • quartaela
    quartaela about 11 years
    well you are perfect !. thanks a lot. and could i ask one more question ?. what is the difference between calling new JRisk() and calling it inside the run() method. does it differ so much. i guess it is relevant with concurrency ?
  • David Kroukamp
    David Kroukamp about 11 years
    Glad to help... Yes It has do do with concurrency. All Swing components should be created and manipulated on Event Dispatch Thread. The SwingUtilities.invokeXXX blocks do that for us :)