0

I've been trying to make a shop for my game. This has been unsuccessful.

I've tried drawComponent, didn't work. No errors, code executed, but didn't work. Now i'm trying to do:

private void render() {
    Graphics2D g = (Graphics2D) graphics.getGraphics();

    /////////////////////
    g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
    /////////////////////
    g.dispose();

    Graphics2D g2d = (Graphics2D) getGraphics();
    g2d.drawImage(img, 0, 0, null);
    g2d.dispose();
}

Now i get a NullPointerException on g2d. I've tried everything.

`Exception in thread "game" java.lang.NullPointerException
    at com.johnythecarrot.game.Shop$DrawPane.access$2(Shop.java:123)
    at com.johnythecarrot.game.Shop.render(Shop.java:154)
    at com.johnythecarrot.game.Game.render(Game.java:75)
    at com.johnythecarrot.game.Game.run(Game.java:112)
    at java.lang.Thread.run(Unknown Source)`

My goals are to be able to have clickable buttons. It DID work. But i had to restart almost everytime. Because mostly of the time to code wasn't even executed. So i tried to fix it. Now it's all messed up.

This is the code to it. (DoubleInt is a part of my library it's nothing more than just x and y. )

public class Shop {

    public BuildWindow window;
    public static JWindow w;

    private int WIDTH = 860, HEIGHT = 440;

    private BufferedImage graphics = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

    public DrawPane drawPane;

    public Shop() {
        //window = new BuildWindow().setSize(new DoubleInt(100, 100)).at(wi, he).setTitle("Shop").setOpacity(1).setDragable(false).showEmpty(true);
        w = new JWindow();
        w.setOpacity(1);
        w.setSize(WIDTH, HEIGHT);
        w.setLocation(800, 800);
        w.setVisible(false);
        w.setAlwaysOnTop(true);
        //graphics = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

    }

    private void createShop() {
        /***Graphics2D g = (Graphics2D) graphics.getGraphics();
        g.setColor(Color.blue);
        g.drawString("hey", WIDTH-50, HEIGHT-50);
        g.fillRect(0, 0, WIDTH, HEIGHT);*/
    }

    public class DrawPane extends JPanel {

        int width = WIDTH;
        int height = HEIGHT;
        private ArrayList<Shape> buttons;
        private Shape btn1 = new Rectangle2D.Double(20, 60, width/2, height-20);
        private Shape btnClose = new Rectangle2D.Double(width-25, 5, 20, 20);

        Point wCoords;
        Point mCoords;

        public DrawPane() {
            buttons = new ArrayList<>();
            buttons.add(btn1);
            buttons.add(btnClose);
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    super.mouseClicked(e);
                    for(Shape s : buttons) {
                        if(s.contains(e.getPoint())) {
                            System.out.println("Clicked " + s.getBounds());
                            if(s == btnClose) {
                                w.dispose();
                            }
                        }
                    }
                }
                @Override
                public void mousePressed(MouseEvent e) {
                    mCoords = e.getPoint();
                }
                @Override
                public void mouseReleased(MouseEvent arg0) {
                    mCoords = null;
                }
            });
            addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseDragged(MouseEvent e) {
                wCoords = e.getLocationOnScreen();
                w.setLocation(wCoords.x - mCoords.x, wCoords.y - mCoords.y);
                }
            });
        }

        void repaintThis() {
            repaint();
        }

        BufferedImage img = loadImageFrom.LoadImageFrom(Shop.class, "bar.png");

        Graphics gb;

        /**
         * super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g.setColor(Color.red);
            //g.fillRect(0, 0, width, 50);
            g.drawImage(img, 0, 0, width, 50, null);
            g.setColor(Color.WHITE);
            g.drawString("SHOP", 15, 30);
            g.drawString("X", width-20, 20);
            for(Shape b : buttons) {
                g2d.draw(b);
            }
            System.out.println("Built");
            gb = g;
         */

        private void render() {
            Graphics2D g = (Graphics2D) graphics.getGraphics();

            /////////////////////
            g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
            /////////////////////
            g.dispose();

            Graphics2D g2d = (Graphics2D) getGraphics();
            g2d.drawImage(img, 0, 0, null);
            g2d.dispose();
        }

        public void Build() {
            Graphics g = gb;
            Graphics2D g2d = (Graphics2D) g;
            g.setColor(Color.red);
            //g.fillRect(0, 0, width, 50);
            g.drawImage(img, 0, 0, width, 50, null);
            g.setColor(Color.WHITE);
            g.drawString("SHOP", 15, 30);
            g.drawString("X", width-20, 20);
            for(Shape b : buttons) {
                g2d.draw(b);
            }
            System.out.println("Built");
        }

    }

    public void render(Graphics2D g) {
            drawPane.render();
    }

    public void addDrawPane() {
        drawPane = new DrawPane();
        w.add(drawPane);
    }
}

If you need access to more code, just ask me.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122

1 Answers1

1

You should override the paintComponent method like this:

public class DrawPane extends JPanel {

  // all your variables and other things

  @Override
  paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    // Your code goes here, use the g2d

  }

}

then if you need to repaint your component, simply call repaint() on it.

Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32
  • I've already done both. Repaint happens but i still don't see anything. That's the weird thing about it. There's probably something wrong with things being called too early or too late. – Tuur Martens Aug 03 '16 at 10:56