0xCursor
0xCursor

Reputation: 2268

Keeping a circle centered when the frame is resized

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

Upvotes: 2

Views: 903

Answers (1)

trashgod
trashgod

Reputation: 205875

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;

Upvotes: 3

Related Questions