2

I am trying to make a program that draws a circle in the center of a JFrame and am drawing the circle using paintComponent. My goal is to have the circle centered in the frame even if the JFrame is resized. I have tried and searched for different things but nothing has worked. I am guessing that I have to use repaint() and timers but don't know how exactly. My code is as follows:

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

public class ImageFrame extends JFrame {
    private static final long serialVersionUID = 1L;

    int width = 40;
    int height = 40;
    int x;
    int y;

    JPanel panel = new JPanel() {
        private static final long serialVersionUID = 2L;
        public void paintComponent(Graphics g) {
            super.paintComponents(g);
          g.drawOval(x, y, width, height);
        }
    };

    public ImageFrame() {
        add(panel);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(400, 300);
        x = (getWidth()/2) - (width/2)-20;
        y = (getHeight()/2) - (height/2)-40;
        setLocationRelativeTo(null);
        setVisible(true);
    }
}

Update:

I have added TrashGod's method but it says to remove the @Override and then if ran, the JFrame opens but with no circle. The code is below and I have edited out the paintComponent from my old code.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ImageFrame extends JFrame implements {
    private static final long serialVersionUID = 1L;

    public ImageFrame() {
        addMouseListener(this);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(400, 300);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        Dimension size = this.getSize();
        int d = 200;
        int x = (size.width - d) / 2;
        int y = (size.height - d) / 2;
        g.fillOval(x, y, d, d);
        g.setColor(Color.blue);
        g.drawOval(x, y, d, d);
    }
}
0xCursor
  • 2,242
  • 4
  • 15
  • 33
  • 1
    This [example](http://stackoverflow.com/a/3538279/230513) centers the circle and adjusts the size to the smaller dimension, but you can make `d` constant. – trashgod Aug 05 '16 at 03:39
  • Do you know anything about keeping the circle centered if the frame is resized? It seems like an easy task but I don't know how to do so. – 0xCursor Aug 05 '16 at 03:47
  • I've elaborated [below](http://stackoverflow.com/a/38780788/230513). – trashgod Aug 05 '16 at 03:51

1 Answers1

3

This example centers the circle and adjusts the size to the smaller dimension, but you can make d constant. The essential step is to render relative to the panel's current size. Resize the enclosing frame to see the effect. Adding RenderingHints, seen here, makes the drawing smother.

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        Dimension size = this.getSize();
        int d = 200;
        int x = (size.width - d) / 2;
        int y = (size.height - d) / 2;
        g.fillOval(x, y, d, d);
        g.setColor(Color.blue);
        g.drawOval(x, y, d, d);
    }

image

Changes to the example:

$ diff OldSwingPaint.java SwingPaint.java 
38a39,41
>             Graphics2D g2d = (Graphics2D) g;
>             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
>                 RenderingHints.VALUE_ANTIALIAS_ON);
40c43
<             int d = Math.min(size.width, size.height) - 10;
---
>             int d = 200;
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • How would I be able to implement this into my code? And does this code keep the circle the same size when the frame is resized? – 0xCursor Aug 05 '16 at 15:02
  • Try it. It's a one line change to the [example cited](http://stackoverflow.com/a/3538279/230513), setting `int d = 200`. – trashgod Aug 05 '16 at 15:32
  • When I tried to use it, it said that there was an error with the paintComponent. – 0xCursor Aug 05 '16 at 15:37
  • 1
    Please edit your question to include a [mcve] that exhibits the problem you describe; include the error and stack trace. – trashgod Aug 05 '16 at 15:39
  • 1
    I forgot to check this as the best answer when I figured out this problem a long time ago. – 0xCursor Feb 05 '18 at 04:53