About drawing a polygon in java

Hi, I am trying to improve myself regarding java2D, and first of all I am engaged in drawing polygons. However, I do not see the polygon in the frame. I have read several lessons and examples, but as I said, I ran into problems. Here is an example of polygon drawing code;

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(); } } 
+7
source share
3 answers

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 have to add JPanel in viad JFrame#add(..) )

2) Cancel getPreferredSize() to return the correct Dimension that matches your drawing on the Graphics object, otherwise they will not be considered as JPanel size without 0,0 components

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

4) Read Concurrency in Swing , namely Dispatch Thread Event

5). The clock naming scheme must begin with a capital letter, and each first letter of a new word after this must be capitalized

6) Also you extend JFrame and have a JFrame variable? Take extend JFrame and save the JFrame variable, since we don’t want 2 JFrame and its not a good practice to extend JFrame without adding functionality

Here is your code with the corrections above (sorry for the image quality, but it needs to be changed or it will reach 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(); } }); } } 

According to your comment:

I am preparing a map that includes many polygons and yesterday used a JPanel over a JFrame, and I tried to check if the mouse was inside the polygon with MouseListener. later I saw that mouseListener gave false answers (for example, the mouse is not inside the polygon, but it acts like inside the polygon). so I uninstalled JPanel and then worked

Here is the updated code with MouseAdapter and redefined mouseClicked to check if there was a click in the 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(); } }); } } 
+13
source

JFrame not distributed by JComponent , so it does not override paintComponent . You can verify this by adding the @Override annotation.

To get this functionality, extract paintComponent into a new class that extends JComponent . Remember to call super.paintComponent(g) , not super.paintComponents(g) .

+3
source

Replace

 protected void paintComponent(Graphics g){ super.paintComponents(g); g.setColor(Color.BLUE); g.drawPolygon(poly); } 

FROM

 protected void paint(Graphics g){ super.paint(g); g.setColor(Color.BLUE); g.drawPolygon(poly); } 
0
source

All Articles