2016-08-03 13 views
0

나는 내 게임을 위해 상점을 만들려고 노력해 왔습니다. 이것은 성공적이지 못했습니다.Graphics2D를 사용하여 JPanel에서 렌더링 할 때이 NullPointerException을 해결하려면 어떻게해야합니까?

drawComponent를 시도했지만 작동하지 않았습니다. 오류없이 코드가 실행되었지만 작동하지 않았습니다. 지금 내가 할 노력하고있어 :

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

지금 내가 g2d에 NullPointerException이 얻을. 나는 모든 것을 시도했다.

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

나의 목표는 클릭 가능한 버튼을 가질 수있게하는 것입니다. 그것은 작동하지 않았습니다. 그러나 나는 거의 언제나 다시 시작해야했습니다. 대부분 코드 작성에 시간이 걸리지 않았기 때문입니다. 그래서 나는 그것을 고치려고 노력했다. 이제 모든 게 엉망이 됐어.

이것은 코드입니다. 은 (DoubleInt는 그냥 x와 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); 
    } 
} 

당신이 더 많은 코드에 액세스해야하는 경우, 그냥 나 한테 물어.

+0

'grahpics'가'null' 인 것처럼 보입니다 –

+0

'g'가'Graphics' 객체가 아니어야합니까? –

+0

paintComponent()를 사용하여 코드를 업데이트 하시겠습니까? –

답변

1

당신은이 같은의 paintComponent 방법 오버라이드 (override) : 당신이 당신의 구성 요소를 다시 칠해야하는 경우 다음

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 

    } 

} 

를, 단순히에 칠하고()를 호출합니다.

+0

나는 이미 그 두 가지를 다했다. 다시 칠하기는하지만 아무 것도 볼 수 없습니다. 그것에 대해 이상한 점이 있습니다. 너무 일찍 또는 너무 늦게 불리는 것에는 아마 잘못된 것이 있습니다. –