2014-04-22 89 views
-1

基本上,所有顯示的是帶有黑色JPanel的JFrame,但沒有任何球/多邊形。現在真的很煩人,我看不出原因。任何幫助不勝感激。這爲什麼不繪製我的多邊形? (Java遊戲)

編輯:添加的代碼。對不起,張貼到Github上,不知道它被皺起了眉頭。

public class Board extends JFrame { 
    private int width = 800; 
    private int height = 1000; 
    private int currentKeyCode = 0; 
    private boolean keyHeldDown = false; 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        Board b = new Board(); 
        b.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    public Board() { 
     setSize(width, height); 
     setTitle("Drop"); 
     setBackground(Color.BLACK); 
     setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     addKeyListener(new KeyAdapter() { 
      @Override 
      public void keyPressed(KeyEvent e) { 
       if (e.getKeyCode() == KeyEvent.VK_RIGHT) { 
        currentKeyCode = KeyEvent.VK_RIGHT; 
        keyHeldDown = true; 
        System.out.println("Right + 10"); 
       } 
       if (e.getKeyCode() == KeyEvent.VK_LEFT) { 
        currentKeyCode = KeyEvent.VK_LEFT; 
        keyHeldDown = true; 
        System.out.println("Left + 10"); 
       } 
       if (e.getKeyCode() == KeyEvent.VK_P) { 
        currentKeyCode = KeyEvent.VK_P; 
        keyHeldDown = true; 
        System.out.println("Pause"); 
       } 
      } 

      @Override 
      public void keyReleased(KeyEvent e) { 
       keyHeldDown = false; 
      } 
     }); 
     setContentPane(new Panel(this)); 
     ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(5); 
     executor.scheduleAtFixedRate(new RepaintBoard(this), 0L, 20L, TimeUnit.MILLISECONDS); 
    } 

    @Override 
    public int getWidth() { 
     return width; 
    } 

    public void setWidth(int width) { 
     this.width = width; 
    } 

    @Override 
    public int getHeight() { 
     return height; 
    } 

    public void setHeight(int height) { 
     this.height = height; 
    } 

    private class RepaintBoard implements Runnable { 
     final Board board; 
     public RepaintBoard(Board board) { 
      this.board = board; 
     } 
     @Override 
     public void run() { 
      board.repaint(); 
     } 
    } 
} 

class Panel extends JComponent { 
    Ball ball; 
    private Board board; 
    public Panel(Board board) { 
     this.board = board; 
     ball = new Ball(); 
    } 

    @Override 
    public void paint(Graphics g1) { 
     Graphics2D g = (Graphics2D) g1; 
     g.setColor(Color.BLACK); 
     g.drawRect(0, 0, board.getWidth(), board.getHeight()); 
     g.drawPolygon(ball); 
    } 
} 

class Ball extends Polygon { 
    private int radius = 5; 
    private Point loc; 
    private int[] xPos = new int[radius * 2 + 1]; 
    private int[] yPos = new int[radius * 2 + 1]; 
    public Ball() { 
     for (int i = -radius, j = 0; i <= radius; i++, j++) { 
      xPos[j] = i; 
      yPos[j] = i; 
     } 
     new Ball(xPos, yPos, radius * 2 + 1, 100, 100); 
    } 

    public Ball(int[] xPos, int[] yPos, int points, int x, int y) { 
     super(xPos, yPos, points); 
     loc = new Point(x, y); 
     for (int i : xPos) { 
      System.out.println(i); 
     } 
    } 
} 
+0

此處的郵政編碼。 –

+0

135行不算什麼,發佈它。 – demongolem

+0

對不起,編輯了這篇文章。 – Injustice

回答

3
  1. 不要有Ball延伸Polygon

  2. 把一個drawBall(Grapchics g) {}方法在Ball類,做你的球畫在那裏。

  3. 呼叫drawBall方法在paint

    ball.drawBall(g); 
    
  4. 不要覆蓋paint,而不是在面板上覆蓋paintComponent,不要忘記調用super.paintComponent

    @Override 
    protected void paintComponent(Graphics g) { 
        super.paintComponent(g); 
    } 
    
  5. new Ball(xPos, yPos, radius * 2 + 1, 100, 100);在你的構造函數中完全沒有。您應該改用第二個構造函數,並使用構造函數創建球。每個球應該是不同的,所以一個無參數的構造函數是毫無意義的

+0

謝謝!這幫了很多。 – Injustice