调整框架大小时保持圆心

时间:2016-08-05 03:29:29

标签: java swing jframe jpanel paintcomponent

我正在尝试制作一个程序,在JFrame的中心绘制一个圆圈并使用paintComponent绘制圆圈。我的目标是即使JFrame调整大小,也让圆圈在框架中居中。我试过并寻找不同的东西但没有任何效果。我猜我必须使用repaint()和计时器但不知道具体如何。我的代码如下:

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

更新

我添加了TrashGod的方法,但是它说要删除@Override,然后如果运行,JFrame会打开,但没有圆圈。代码如下,我已经从旧代码中删除了paintComponent

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

1 个答案:

答案 0 :(得分:2)

example使圆圈居中并将尺寸调整为较小的尺寸,但您可以使d保持不变。关键步骤是相对于面板的当前大小进行渲染。调整封闭框架的大小以查看效果。添加RenderingHints,看到here,会使图形变得窒息。

    @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

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;