Search code examples
javaswingjframepixeloval

Java JFrame distance between ovals not equal


I had to implement a JFrame with the size of 500 x 500 pixels that should have a 9 x 9 "field" of circles, but as you can see in the picture the distance between the first line of ovals and the second line of ovals is not equal.

enter image description here

The diameter should be 20 pixel and the distance between the center of one oval to another oval should be 40 pixel, but I don't know if I did this correctly:

import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Color;

public class KreisFrame extends JFrame {
    public KreisFrame() {

        //Set JFrame size
        setSize(500,500);

        //Make JFrame visible
        setVisible(true);
    }

    public void paint(Graphics g) {
        super.paint(g);

        g.setColor(Color.GREEN);
        g.fillRect(0, 0, 500, 500);

        for (int j = 0; j < 500; j += 60){
            for (int i = 0; i < 500; i += 60) {
                // draw circle
                g.drawOval(i, 20, 20, 20);
                g.drawOval(i, j, 20, 20);
                // fill circle
                g.fillOval(i, 20, 20, 20);
                g.fillOval(i, j, 20, 20);
                g.setColor(Color.BLUE);
            }
        }
    }

    public static void main(String[]args) {
        KreisFrame myframe = new KreisFrame();
    }
}  

Can someone tell me what I did wrong?


Solution

  • You should really subclass a JPanel, not the JFrame. And all of this should be done on the EventDispatchThread, not the main thread. Further, don't use "magic" numbers like 500, 20, and 40. Here's a solution that paints the entire panel, regardless of what size it is (note that there is no provision here for having a border on the panel).

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class Kreis extends JPanel {
       protected int dia = 20;
       protected int sep = 40;
    
       public Kreis() {
    
          // Set JFrame size
          setPreferredSize(new Dimension(500, 500));
          setBackground(Color.green);
       }
    
       @Override
       public void paintComponent(Graphics g) {
          super.paintComponent(g);
    
          Dimension d = getSize();
    
          g.setColor(Color.BLUE);
          for (int y = 0; y < d.height; y += sep) {
             for (int x = 0; x < d.width; x += sep) {
                // draw circle
                g.fillOval(x, y, dia, dia);
             }
          }
       }
    
       public static void main(final String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {
                // Create and set up the window.
                JFrame jf = new JFrame();
    
                Kreis panel = new Kreis();
                jf.add(panel);
                jf.pack();
                jf.setVisible(true);
                jf.addWindowListener(new WindowAdapter() {
                   @Override
                   public void windowClosing( WindowEvent arg0) {
                      System.exit(0);
                   }
                });
             }
          });
       }
    }