为什么这不能画出我的Polygon? (Java游戏)

时间:2014-04-22 17:18:30

标签: java swing graphics jframe polygon

基本上,显示的是一个内部带有黑色JPanel但在任何地方都没有Ball / polygon的JFrame。现在真的很烦我,我看不出原因。非常感谢任何帮助。

编辑:添加了代码。很抱歉发布到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);
        }
    }
}

1 个答案:

答案 0 :(得分: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);绝对没有任何作用。您应该只使用第二个构造函数,并使用 构造函数创建球。每个球都应该是不同的,所以一个无法构造的人没有意义

相关问题