我是Java GUI编程的新手。以下代码是我简化的问题。
单击Panel时会绘制两个图像。一个是使用drawVrImage()
方法绘制的Red Box,另一个是使用drawRealImage()
方法绘制的Red Star。所有图像中的两个沿着增加的行绘制。
点击5次后显示。但问题是drawRealImage()
只会将其图像描绘4次,即使它被调用了5次。第一个红盒在哪里?为什么BufferedImage
无法显示第一个红盒子。
GUI
类仅用于加载图片。加载所有图像并在启动时放置HashMap
。所以,我认为图像加载(Qwirkle.getImage()
)不是问题。我认为缓冲图像没有Image Observer是个问题。但如果真的有问题。我怎么画第二个红盒子。
供您参考。这是代码说明。班级BIsource
有一个BuffedImage
。当我点击UserPanel
时。 BufferedImage
中的BIsource
获取其图形并绘制图像。完成绘制图像后,UserPanel
从BIsource获取该图像。并称为reapint()
方法。 paintCompent()
中的UserPanel
方法仅适用于从BIsource实例中提取缓冲图像。
我该如何解决这个问题?
UserPanel类
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class UserPanel extends JPanel implements MouseListener {
private BIsource draw;
private BufferedImage bi;
public UserPanel() {
this.draw = new BIsource();
this.bi = draw.getBI();
this.addMouseListener(this);
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
g.drawImage(bi, 0, 0, this);
}
@Override
public void mouseClicked(MouseEvent e) {
this.draw.drawVrImage();
this.draw.drawRealImage();
this.draw.incIndex();
bi = this.draw.getBI();
this.repaint();
}
class BIsource {
private BufferedImage biCanvas;
private HashMap<String, BufferedImage> imageMap;
private int index = 0;
public BIsource() {
// biCanvas Declaration
biCanvas = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB_PRE);
Graphics g = biCanvas.createGraphics();
g.setColor(Color.blue);
g.fillRect(0, 0, 500, 500);
g.dispose();
imageMap = new HashMap<>();
this.createVrImage();
}
private void createVrImage() {
BufferedImage vrImage = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB_PRE);
Graphics g = vrImage.createGraphics();
g.setColor(Color.red);
g.fillRect(0, 0, 50, 48);
g.dispose();
this.imageMap.put("TEST IMAGE", vrImage);
}
public BufferedImage getBI() {
return this.biCanvas;
}
public void drawVrImage() {
Graphics g = biCanvas.createGraphics();
g.drawImage(this.imageMap.get("TEST IMAGE"), 0, index * 50, null);
g.dispose();
}
public void drawRealImage() {
Graphics g = biCanvas.getGraphics();
g.drawImage(Qwirkle.getImage("RED STAR"), 200, index * 50, null);
g.dispose();
}
public void incIndex() {
index += 1;
}
}
public static void main(String[] args) {
Qwirkle.loadQwirkleImage();
JFrame mainFrame = new JFrame("Test Frame");
mainFrame.setLayout(null);
mainFrame.setSize(500, 500);
mainFrame.setVisible(true);
mainFrame.setBackground(Color.black);
mainFrame.setLocation(800, 400);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
UserPanel userPanel = new UserPanel();
mainFrame.add(userPanel);
userPanel.setSize(500, 500);
userPanel.setVisible(true);
}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
}
Qwirkle Class
import java.awt.Image;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Random;
public class Qwirkle implements Serializable {
private static HashMap<String, Image> qwirkleImages;
// static initialize
static {
}
public enum Color {
CYAN, GREEN, ORANGE, PURPLE, RED, YELLOW
}
public enum Shape {
CIRCLE, CLOVER, DIAMOND, RECTANGLE, STAR, URCHIN
}
private final Color color;
private final Shape shape;
private final int index;
public Qwirkle(Color color, Shape shape, int index) {
this.color = color;
this.shape = shape;
this.index = index;
}
public static void loadQwirkleImage() {
if (Qwirkle.qwirkleImages == null) {
Qwirkle.qwirkleImages = new HashMap<>();
String key;
try {
for (Qwirkle.Color color : Qwirkle.Color.values()) {
for (Qwirkle.Shape shape : Qwirkle.Shape.values()) {
key = new String(color.toString() + " " + shape.toString());
Qwirkle.qwirkleImages.put(key, GUI.loadImage(key + ".jpg"));
}
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.err.println("Already Loading all Qwirkle Image");
}
}
public static Image getImage(String key) {
return qwirkleImages.get(key);
}
synchronized public String getKey() {
return new String(color.toString() + " " + shape.toString());
}
@Override
public String toString() {
return new String(color.toString() + " " + shape.toString() + " " + index);
}
public Color getColor() {
return color;
}
public Shape getShape() {
return shape;
}
}
答案 0 :(得分:0)
最后我得到了答案。我改变了HashMap对象。我使用HashMap来放置Image Class。但现在我将BufferedImage存储在HashMap中。它运作良好。当我第一次点击时,它会立即绘制一颗红星。然后我读到了Image和BufferedImage之间的区别。但是我不知道为什么Image不能很好地工作并且BufferedImage工作得很好。(请问你能得到吗?)
private HashMap<String, Image> qwirkleImages;
更改为
private HashMap<String, BufferedImage> qwirkleImages;
这是固定代码。这是result image
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class UserPanel extends JPanel implements MouseListener {
private BIsource draw;
private BufferedImage bi;
public UserPanel() {
this.draw = new BIsource();
this.bi = draw.getBI();
this.addMouseListener(this);
}
@Override
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
g.drawImage(bi, 0, 0, this);
}
@Override
public void mouseClicked(MouseEvent e) {
this.draw.drawVrImage();
this.draw.drawRealImage();
this.draw.incIndex();
bi = this.draw.getBI();
this.repaint();
}
class BIsource {
private BufferedImage biCanvas;
// private HashMap<String, Image> qwirkleImages;
private HashMap<String, BufferedImage> qwirkleImages;
private HashMap<String, BufferedImage> imageMap;
private int index = 0;
public BIsource() {
this.qwirkleImages = new HashMap<>();
this.loadAllImage();
// biCanvas Declaration
biCanvas = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB_PRE);
Graphics g = biCanvas.createGraphics();
g.setColor(Color.blue);
g.fillRect(0, 0, 500, 500);
g.dispose();
imageMap = new HashMap<>();
this.createVrImage();
}
public void loadAllImage() {
String key;
try {
for (Qwirkle.Color color : Qwirkle.Color.values()) {
for (Qwirkle.Shape shape : Qwirkle.Shape.values()) {
key = new String(color.toString() + " " + shape.toString());
qwirkleImages.put(key, GUI.loadImage2(key + ".jpg"));
System.out.println(key);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void createVrImage() {
BufferedImage vrImage = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB_PRE);
Graphics g = vrImage.createGraphics();
g.setColor(Color.red);
g.fillRect(0, 0, 50, 48);
g.dispose();
this.imageMap.put("TEST IMAGE", vrImage);
}
public BufferedImage getBI() {
return this.biCanvas;
}
public void drawVrImage() {
Graphics g = biCanvas.createGraphics();
g.drawImage(this.imageMap.get("TEST IMAGE"), 0, index * 50, null);
g.dispose();
}
public void drawRealImage() {
Graphics g = biCanvas.getGraphics();
Image image = this.qwirkleImages.get("RED STAR");
if (image == null) {
System.out.println("image is null");
}
g.drawImage(image, 200, index * 50, null);
System.out.println("DRAW Real");
g.dispose();
}
public void incIndex() {
index += 1;
}
}
public static void main(String[] args) {
System.out.println("load all images");
JFrame mainFrame = new JFrame("Test Frame");
mainFrame.setLayout(null);
mainFrame.setSize(500, 500);
mainFrame.setVisible(true);
mainFrame.setBackground(Color.black);
mainFrame.setLocation(800, 400);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
UserPanel userPanel = new UserPanel();
mainFrame.add(userPanel);
userPanel.setSize(500, 500);
userPanel.setVisible(true);
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
}