调整框架大小时保持圆心

问题描述 投票:2回答:1

我正在尝试制作一个程序,在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);
    }
}
java swing jframe jpanel paintcomponent
1个回答
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;
© www.soinside.com 2019 - 2024. All rights reserved.