-1

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;
    }
    
    
}
Community
  • 1
  • 1
  • 2
    If debugging isn't immediately helpful, create a [mcve] the focuses on just the problem. In your example, access posted images via `URL`, as shown [here](http://stackoverflow.com/a/10862262/230513); use synthetic images as shown [here](http://stackoverflow.com/a/15982915/230513); or use `UIManager` icons, as shown [here](http://stackoverflow.com/a/12228640/230513). – trashgod Sep 23 '16 at 03:37
  • 1
    your program without qwirkle and just another image works fine - I advise you against using static; remove all static (and synchronized unless you really KNOW you need it (not just for fun) – gpasch Sep 23 '16 at 08:48
  • @gpasch Here is fixed code. I wish You know about it is not homework and assignment. – Doyoon Kim Sep 23 '16 at 15:23

1 Answers1

0

Finally I've got answer. I changed HashMap Object. I used HashMap for put Image Class. But now I stored BufferedImage at HashMap. It works well. When I clicked first time, it draws one Red Star immediately. then I read about difference between Image and BufferedImage. But I don't have no idea why Image doesn't work well and BufferedImage works well.(Could you expain that? please)

private HashMap<String, Image> qwirkleImages;

is changed to

private HashMap<String, BufferedImage> qwirkleImages;

Here is fixed code. and Here is 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) {
    }
}