2014-04-22 52 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