2016-08-03 1 views
0

J'ai essayé de faire un magasin pour mon jeu. Cela a échoué.Comment est-ce que je peux corriger cette exception NullPointerException en essayant de rendre sur un JPanel avec Graphics2D?

J'ai essayé drawComponent, ne fonctionnait pas. Aucune erreur, le code a été exécuté mais n'a pas fonctionné. Maintenant, je suis en train de faire:

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(); 
} 

Maintenant je NullPointerException sur G2D. J'ai tout essayé.

`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)` 

Mes objectifs doivent pouvoir avoir des boutons cliquables. Il a fonctionné. Mais j'ai dû redémarrer presque à chaque fois. Parce que la plupart du temps pour coder n'était même pas exécuté. J'ai donc essayé de le réparer. Maintenant tout est foiré.

Ceci est le code. (Doubleint est une partie de ma bibliothèque, il n'y a rien plus que x et 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); 
    } 
} 

Si vous avez besoin d'un accès à plus de code, me demander.

+0

On dirait que 'grahpics' est' null' –

+0

ne devrait pas être un objet 'Graphics'? –

+0

mettre à jour votre question avec le code en utilisant paintComponent() ok? –

Répondre

1

Vous devriez remplacer la méthode paintComponent comme ceci:

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 

    } 

} 

alors si vous avez besoin de repeindre votre composant, il suffit d'appeler repeindre() sur elle.

+0

J'ai déjà fait les deux. Repaint arrive mais je ne vois toujours rien. C'est la chose étrange à ce sujet. Il y a probablement quelque chose qui ne va pas quand les choses sont appelées trop tôt ou trop tard. –