I'm new in Java GUI Programming. The following code is the problem which I simplified that.
Two image is drawn when clicked Panel. One is Red Box which is drawn using drawVrImage()
method, another is Red Star which is drawn using drawRealImage()
method. Two of all image is drawn along increasing row.
Here is result Image:
It shows after clicked 5 times. but the problem is drawRealImage()
draws their image just 4 times even though it is called 5 times. Where is first Red Box? Why BufferedImage
can't show the First Red Box.
GUI
class just used for load image. All images is loaded and put HashMap
when it starts. So, I think image loading(Qwirkle.getImage()
) is not problem. I think that buffered image doesn't have Image Observer is problem. But If It is really problem. How can I draw Second Red Box.
For your information. Here is code explanation. Class BIsource
has one BuffedImage
. when I clicked the UserPanel
. BufferedImage
in BIsource
gets its graphics and draw Image. after finished draws image, UserPanel
gets that image from BIsource. and called reapint()
method. paintCompent()
method in UserPanel
only works for drawing buffered image from BIsource Instance.
How can I solve this Problem?
UserPanel Class
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;
}
}