gpt4 book ai didi

java - 调用了 paintComponent,但没有显示任何内容

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:01:42 26 4
gpt4 key购买 nike

我是 AWT 的新手,这只是一个示例程序,可以了解它是如何工作的。

我的问题是在调整大小/最小化等事件中,窗口被清除。

我已经覆盖了很多 SO 问题中指定的 paintComponent,并且实际调用了代码(因为如果我把 println 放在那里,我会看到输出),但是什么也没有显示。

我已经在 Windows 和 Linux 上试过了,结果是一样的,所以我想我遗漏了什么。

代码如下:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;

class Site {
private Integer x;
private Integer y;
private Integer width;
private Integer height;
private Graphics2D g2d;
private boolean isOpen = false;

private Integer id = null;
private Integer root = null;

public Site(Integer id, Integer x, Integer y, Integer width, Integer height, Graphics2D g2d) {
this.id = id;
this.root = id;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.g2d = g2d;
}

public void Draw() {
g2d.setPaint(new Color(0, 0, 0));
g2d.setStroke(new BasicStroke(2));
Integer x_top_corner = x - (width / 2);
Integer y_top_corner = y - (height / 2);
g2d.drawRect(x_top_corner, y_top_corner, width, height);
System.out.println(x_top_corner);

if (isOpen) {
g2d.setPaint(new Color(150, 150, 200));
} else {
g2d.setPaint(new Color(200, 200, 200));
}
g2d.fillRect(x_top_corner + 1, y_top_corner + 1, width - 2, height - 2);
}

public void Open() {
this.isOpen = true;
}

public void Close() {
this.isOpen = false;
}
}

class Surface extends JPanel {
private ArrayList<Site> sites = new ArrayList<Site>();
private Graphics2D g2d = null;

private void initSites() {
Integer width = 30;
Integer height = 30;
Integer index = 0;

for (Integer y = height; y < 800 - height - 30; y += height) {
for (Integer x = width; x < 800 - width; x += width) {
sites.add(new Site(index, x, y, width, height, g2d));
index++;
}
}
Integer x = 20;
Integer y = 3;
sites.get(x + (y * 25)).Open();
sites.get(x + 1 + (y * 25)).Open();
sites.get(x).Open();
sites.get(4).Open();
sites.get(9).Open();
sites.get(13).Open();
sites.get(18).Open();
}

private void doDrawing(Graphics g) {
if (g2d == null) {
g2d = (Graphics2D) g;
}

if (sites.size() == 0) {
initSites();
}

for (Site site : sites) {
site.Draw();
}
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}

public class Squares extends JFrame {
public Squares() {
initUI();
}

private void initUI() {
add(new Surface());
setTitle("Squares demo");
setSize(800, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
Squares ex = new Squares();
ex.setVisible(true);
}
}

最佳答案

您将 Graphics2D 用作一个字段,假设一个图形对象与下一个对象一样好,而这不是您进行 Swing 绘图的方式。提供给 paintComponent 方法的 Graphics 对象不是长期的或稳定的,它必须被使用,而不是存储在一个字段中供以后使用。而是仅使用通过 paintComponent 的参数从 JVM 动态传递到程序中的 Graphics 对象。例如,如果您给 Draw(将其重命名为绘制)一个 Graphics 或 Graphics2D 参数,然后将其从 paintComponent 方法传入:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

class Site {
private Integer x;
private Integer y;
private Integer width;
private Integer height;
// private Graphics2D g2d;
private boolean isOpen = false;

private Integer id = null;
private Integer root = null;

// !! public Site(Integer id, Integer x, Integer y, Integer width, Integer
// height, Graphics2D g2d) {
public Site(Integer id, Integer x, Integer y, Integer width, Integer height) {
this.id = id;
this.root = id;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
// this.g2d = g2d;
}

public void draw(Graphics2D g2d) {
g2d.setPaint(new Color(0, 0, 0));
g2d.setStroke(new BasicStroke(2));
Integer x_top_corner = x - (width / 2);
Integer y_top_corner = y - (height / 2);
g2d.drawRect(x_top_corner, y_top_corner, width, height);
System.out.println(x_top_corner);

if (isOpen) {
g2d.setPaint(new Color(150, 150, 200));
} else {
g2d.setPaint(new Color(200, 200, 200));
}
g2d.fillRect(x_top_corner + 1, y_top_corner + 1, width - 2, height - 2);
}

public void Open() {
this.isOpen = true;
}

public void Close() {
this.isOpen = false;
}
}

class Surface extends JPanel {
private ArrayList<Site> sites = new ArrayList<Site>();
private Graphics2D g2d = null;
private int width;
private int height;

public Surface(int width, int height) {
this.width = width;
this.height = height;
}

@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(width, height);
}


private void initSites() {
Integer width = 30;
Integer height = 30;
Integer index = 0;

for (Integer y = height; y < 800 - height - 30; y += height) {
for (Integer x = width; x < 800 - width; x += width) {
// sites.add(new Site(index, x, y, width, height, g2d));
sites.add(new Site(index, x, y, width, height)); // !!
index++;
}
}
Integer x = 20;
Integer y = 3;
sites.get(x + (y * 25)).Open();
sites.get(x + 1 + (y * 25)).Open();
sites.get(x).Open();
sites.get(4).Open();
sites.get(9).Open();
sites.get(13).Open();
sites.get(18).Open();
}

private void doDrawing(Graphics g) {
if (sites.size() == 0) {
initSites();
}

for (Site site : sites) {
site.draw((Graphics2D) g);
}
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}

public class Squares extends JFrame {
private static final int SIDE_LENGTH = 800;

public Squares() {
initUI();
}

private void initUI() {
add(new Surface(SIDE_LENGTH, SIDE_LENGTH));
setTitle("Squares demo");
// setSize(800, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Squares ex = new Squares();
ex.setVisible(true);
});
}
}

此外,

  • 仅从 Swing 事件线程运行程序
  • 不要直接设置尺寸。改写 getPreferredSize

关于java - 调用了 paintComponent,但没有显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49224469/

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