简体   繁体   中英

Why does this not draw my Polygon? (Java game)

Basically, all that shows up is a JFrame with the black JPanel inside but no Ball/polygon anywhere. It's really annoying me now and I can't see the reason why. Any help greatly appreciated.

EDIT: Added code. Sorry for posting to Github, didn't know it was frowned upon.

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. Don't have Ball extends Polygon

  2. Put a drawBall(Grapchics g) {} method in the Ball class, and do your ball painting in there.

  3. call the drawBall method in the paint

    ball.drawBall(g);
  4. Don't override paint , instead override paintComponent on the panel, and don't forget to call super.paintComponent

     @Override protected void paintComponent(Graphics g) { super.paintComponent(g); }
  5. This new Ball(xPos, yPos, radius * 2 + 1, 100, 100); in your constructor does absolutely nothing. You should instead just use the second constructor, and create the ball with that constructor. Each ball should be different, so a no-arg constructor is pointless

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM