gpt4 book ai didi

最大化 JFrame 期间发生 Java 绘制错误

转载 作者:行者123 更新时间:2023-12-02 07:00:10 25 4
gpt4 key购买 nike

我已按照代码在 JFrame 中进行绘制。

package march_2013;

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

public class Question7 extends JFrame {

public void paint(Graphics g) {
int[] x = new int[] { 10, 60, 360, 410, 210, 210, 260, 210, 190, 160,
190, 190 };
int[] y = new int[] { 200, 250, 250, 200, 200, 180, 180, 100, 100, 160,
160, 200 };
g.drawPolygon(x, y, x.length);
g.drawLine(190, 100, 190, 180);
g.drawLine(210, 100, 210, 180);
}

public static void main(String[] args) {
Question7 window = new Question7();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(440, 40, 420, 400);
window.setVisible(true);
}
}

它工作正常,给出以下输出。

enter image description here

但是我最大化了 JFrame,图像被重新绘制。但旧形象仍保持原样。

enter image description here

如何解决这个问题?谢谢!

最佳答案

调用super.paint()

public void paint(Graphics g) {
super.paint(g);
// ...

API document of paint说:

If this method is reimplemented, super.paint(g) should be called so that lightweight components are properly rendered.

要确保背景为白色:

public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.BLACK);
// ...

关于最大化 JFrame 期间发生 Java 绘制错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16755943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com