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):

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); } 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);