gpt4 book ai didi

java - Repaint() 未被调用

转载 作者:行者123 更新时间:2023-11-29 07:10:05 28 4
gpt4 key购买 nike

最近我一直在开发一个用空白的彩色方 block 绘制区域的程序。它们在屏幕上的位置基于文本文件中的值 1 和 2。 1s 应该制作红色盒子,2s 应该制作绿色盒子。但是,当我运行该程序时,只绘制了红色框。我做了一些测试,发现重绘方法只被调用两次(有时由于某种原因一次),即使文件中有接近 300 个值,并且应该调用 repaint()每个值一次。这是我的代码:

public class MAP extends JFrame {

public static void main(String[] args) throws IOException {
MAP map = new MAP();
}

Shape shape;
int x = -32;
int y = 0;
ArrayList<Shape> shapes = new ArrayList<Shape>();
Graphics2D g2;
Color coulor = null;

private class PaintSurface extends JComponent {

public PaintSurface() {
}

public void paint(Graphics g) {
g2 = (Graphics2D) g;
g2.setColor(coulor);
for (Shape s : shapes) {
g2.draw(s);
}

}
}

public MAP() throws FileNotFoundException, IOException {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.add(panel);
frame.setTitle("Grid Maker");
frame.setSize(400, 200);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.add(new PaintSurface(), BorderLayout.CENTER);
frame.setVisible(true);

readNextLine();
}

private void readNextLine() throws IOException {
File file = new File("map.txt");
BufferedReader in = new BufferedReader(new FileReader(file));
String line = in.readLine();

while (line != null) {
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if (c == '1') {
coulor = Color.RED;
x += 32;
int smallX = x / 32;
int smallY = y / 32;
shape = new Rectangle2D.Float(x, y, 32, 32);
shapes.add(shape);
repaint();
} else if (c == '2') {
coulor = Color.GREEN;
x += 32;
int smallX = x / 32;
int smallY = y / 32;
shape = new Rectangle2D.Float(x, y, 32, 32);
shapes.add(shape);
repaint();

}
}

line = in.readLine();
x = -32;
y += 32;
}
}
}

为什么这段代码不能正常工作?

最佳答案

只是为了补充其他答案,这里有一段代码(基于你的)看起来已经好多了(但仍然存在一些问题,但你还没有):

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class MAP extends JFrame {

public static void main(String[] args) throws IOException {
MAP map = new MAP();
}

public static class ColoredShape {
private Shape shape;
private Color color;

public ColoredShape(Shape shape, Color color) {
super();
this.shape = shape;
this.color = color;
}

public Shape getShape() {
return shape;
}

public Color getColor() {
return color;
}
}

int x = -32;
int y = 0;
List<ColoredShape> shapes = new ArrayList<ColoredShape>();
Graphics2D g2;

private class PaintSurface extends JComponent {

public PaintSurface() {
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g2 = (Graphics2D) g;
for (ColoredShape s : shapes) {
g2.setColor(s.getColor());
g2.draw(s.getShape());
}

}
}

public MAP() throws FileNotFoundException, IOException {
JFrame frame = new JFrame();
frame.setTitle("Grid Maker");
frame.setSize(400, 400);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.add(new PaintSurface(), BorderLayout.CENTER);
frame.setVisible(true);

readNextLine();
}

private void readNextLine() throws IOException {
BufferedReader in = new BufferedReader(new StringReader("11121\n1221\n2212\n221121\n111221\n11221\n222\n2222\n"));
String line = in.readLine();

while (line != null) {
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
Color color = null;
if (c == '1') {
color = Color.RED;
} else if (c == '2') {
color = Color.GREEN;
}
if (color != null) {
shapes.add(new ColoredShape(new Rectangle2D.Float(x, y, 32, 32), color));
x += 32;
repaint();
}
}

line = in.readLine();
x = -32;
y += 32;
}
}
}

关于java - Repaint() 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14803590/

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