0

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
stepbysteptomathpro
  • 455
  • 1
  • 7
  • 16
  • First, please override `paintComponent()` not `paint()`: https://docs.oracle.com/javase/tutorial/uiswing/painting/closer.html – markspace Feb 04 '20 at 20:38
  • Also please check your requirements: a 9x9 grid of circles offset by 60 pixels will need 560 pixels, not 500, or it won't fit. I think your problem relates to this. You're having to squish the last row in. – markspace Feb 04 '20 at 20:45
  • Or... 40 pixels apart on centers means the circles should be offset by 40 pixels, not 60. – markspace Feb 04 '20 at 20:47
  • @markspace Tried both, but they don't seem to fix it... – stepbysteptomathpro Feb 04 '20 at 20:53
  • `g.drawOval(i, 20, 20, 20);` and `g.fillOval(i, 20, 20, 20);` What are these lines for? The drawOval(int x, int y, int width, int height) method indicates that every time through the inner loop, you are creating an oval at y=20. – bcr666 Feb 04 '20 at 20:59
  • Not that it is part of your question, like @markspace says, they should be 40 apart. Testing for j < 500 and testing for i < 500 is a bad way to do this since you need a 9x9. j should go from 0 to < 9 and so should i. then you `g.drawOval(i * 40, j * 40, 20, 20)`. That is not going to be centered on your 500x500 canvas as it should take up only 340. So maybe you want something like `g.drawOval(i * 40 + 80, j * 40 + 80, 20, 20)` – bcr666 Feb 04 '20 at 21:08

1 Answers1

1

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);
               }
            });
         }
      });
   }
}
FredK
  • 4,094
  • 1
  • 9
  • 11