简体   繁体   中英

Why doesn't this code draw and move a rectangle?

I wrote a code about a simple game, but it dosen't work. I mean the rectangle doesn't appear consequently i can't move it. As well as i get no wrong message from ide. I spent several times trying to resolve it for nothing.. any hint or suggestion is greatly appreciated.Thx.

The code i wrote:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;



public class DataGame extends JFrame {
    // Create text fields for balls are left and time elapsed
    private JTextField jtfBallsRemain, jtfTimeElapsed;
    // Button "New game"
    private JButton jbtStartNewGame = new JButton("New game");

    private Game canvas = new Game();

    // the main method
    public static void main(String[] args) {
        JFrame frame = new DataGame();
        frame.setTitle("Test data game ");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(470, 300);
        frame.setVisible(true);
    }

    public DataGame() {
        // Use panel1 to hold text fields, labels and the button
        JPanel panel = new JPanel();

        // Add panel to south
        add(panel, BorderLayout.SOUTH);
        panel.add(new JLabel("Balls remain"));
        panel.add(jtfBallsRemain = new JTextField(4));
        panel.add(new JLabel("Time elapsed"));
        panel.add(jtfTimeElapsed = new JTextField(4));
        panel.add(jbtStartNewGame);

        jtfBallsRemain.setEditable(false);
        jtfTimeElapsed.setEditable(false);

        add(canvas, BorderLayout.CENTER); // Add canvas to centre

        // register listener
        jbtStartNewGame.addActionListener(new StartNewGame());

        canvas.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1)
                    canvas.moveUpRect();
                else if (e.getButton() == MouseEvent.BUTTON3)
                    canvas.moveDownRect();
            }
        });

    }

    class StartNewGame implements ActionListener { // inner class
        @Override
        public void actionPerformed(ActionEvent e) {
            canvas.StartNewGame();
        }
    }

}

class Game extends JPanel {
    //The ball
    private int radius = 10;
    private int x;
    private int y;
    private int dx = 5;
    private int dy = 5;

    //The rectangle
    private int w = 15;
    private int h = 80;
    private int dy1 = 5;
    private int y1 = ((getHeight() / 2) - (h / 2));
    private int x1 = (getWidth() - 50);

    private Timer timer = new Timer(20, new TimerListener());

    public void moveDownRect() {
        y1 += dy1;
        repaint();

    }

    public void moveUpRect() {
        y1 -= dy1;
        repaint();

    }

    public void StartNewGame() {

        timer.start();

    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.GREEN);
        g.fillOval(x, y, 2 * radius, 2 * radius);
        g.setColor(Color.BLUE);
        g.fillRect(x1, y1, w, h);
    }

    class TimerListener implements ActionListener { /*
                                                     * make the TimerListener an
                                                     * inner class of Ball,
                                                     * which allows it to access
                                                     * the variable and methods
                                                     * of Ball
                                                     */
        @Override
        public void actionPerformed(ActionEvent e) {

            if (x < 0 || x + (2 * radius) > getWidth()) {

                dx *= -1;

            }

            if (y < 0 || y + (2 * radius) > getHeight()) {

                dy *= -1;
            }

            x += dx;
            y += dy;

            repaint();
        }
    }

}

When the x1 variable is evaluated;

private int x1 = (getWidth() - 50);

getWidth will return 0 , meaning that x1 will equal -50 , rendering it off screen

A better solution might be to do a post-initialization after the main window has being made visible

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