【问题标题】:Why does this not draw my Polygon? (Java game)为什么这不绘制我的多边形? (Java游戏)
【发布时间】:2014-04-22 17:18:30
【问题描述】:

基本上,所有显示的都是一个 JFrame,里面有黑色的 JPanel,但任何地方都没有球/多边形。现在真的很烦我,我看不出原因。非常感谢任何帮助。

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

【问题讨论】:

  • 135行不算什么,贴吧。
  • 抱歉,已编辑帖子。

标签: java swing graphics jframe polygon


【解决方案1】:
  1. 没有Ball 扩展Polygon

  2. Ball 类中添加一个drawBall(Grapchics g) {} 方法,然后在其中进行球类绘画。

  3. paint中调用drawBall方法

    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); 完全没有任何作用。您应该只使用第二个构造函数,并使用 that 构造函数创建球。每个球都应该是不同的,所以无参构造函数是没有意义的

【讨论】:

  • 谢谢!这很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-15
  • 1970-01-01
  • 2011-03-29
  • 1970-01-01
  • 1970-01-01
  • 2019-09-12
相关资源
最近更新 更多