簡體   English   中英

無法在Java中移動對象

[英]can not make move the object in java

我試圖用java為移動的球程序編寫簡單的代碼。 我是Java的新手,我的意思是我了解基礎知識,因此這里是我的代碼,以防萬一您可以幫助我。我無法移動球形物體。 我創建了包含gui組件的類框架以及具有he ball功能的ball類

    public class Frame extends JFrame{

        private static final int width= 500;
        private static final int height=500;
        private static final Color cbw= Color.BLACK;
        private Ball ball; // the moving object
        private DrawCanvas canvas; // the custom drawing canvas

       private JPanel btnpanel; // the panel of the buttons

       private JPanel mainpanel; // the mainpanel
       private JButton button_ML; // move_Left button
       private JButton button_MR;// move_Right button

       public Frame (){
         setProperties();
            init();
            setUI();  

       }

       private void setProperties() {
            setSize(width, height);
            setTitle("MOVE THE BALL");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
        }

        private void init(){

           btnpanel = new JPanel(new FlowLayout());
           mainpanel= new JPanel(new BorderLayout());
           button_ML = new JButton("Move left");
           button_MR = new JButton("Move right");

           //creating the ball with its features
           ball= new Ball (30,30,width/2-2,height/2-10,Color.red);

           canvas = new DrawCanvas();
           // it makes possible the ball to be seen though we have a main panel.
           canvas.setPreferredSize(new Dimension(width,height));

           button_ML.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent ae) {
                  move_Left();

              }
          });

           button_MR.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent ae) {
                move_Right();

              }
          });

        }

       private void setUI(){


               mainpanel.add(btnpanel, BorderLayout.SOUTH);// adds the button panels to the main panel

               btnpanel.add(button_ML);// adds the button to the panel of buttons
                 btnpanel.add(button_MR);
              mainpanel.add(canvas, BorderLayout.CENTER); // adds the canvas to mainpanel
               add(mainpanel);  // adds the panel in the frame

           }


           class DrawCanvas extends JPanel {
          @Override
          public void paintComponent(Graphics g) {
             super.paintComponent(g);
             setBackground(cbw); // puts color to the background
             ball.paint(g);  // the ball paints itself

          } 
           }   


       private void move_Left(){

           int savedX = ball.x;
          ball.x -=10;
           canvas.repaint(savedX, ball.y, ball.WIDTH, ball.HEIGHT); 
          canvas.repaint(ball.x,ball.y,ball.WIDTH,ball.HEIGHT);    
       }    

       private void move_Right(){

           int savedX = ball.x;
         ball.x += 10;
          canvas.repaint(savedX, ball.y, ball.WIDTH, ball.HEIGHT); 
         canvas.repaint(ball.x,ball.y,ball.WIDTH,ball.HEIGHT);       
       }
           }

//球類

    public class Ball extends JFrame {

        //variables for the construction of the circle
       int x, y; // actual position of the ball
       int WIDTH, HEIGHT;// parameters for the size of the rectangle where the circle is posited. 
       Color color = Color.RED;// the color of the ball

       // the constructor of the class
        public Ball(int x, int y, int WIDTH, int HEIGHT,Color color) {
            this.x = x;// 
            this.y = y;
            this.WIDTH = WIDTH;
            this.HEIGHT = HEIGHT;
            this.color=color;      
        }

        // method for the color and drawing the ball
       public void paint(Graphics g) {
          g.setColor(Color.red);
          g.fillOval(80,70, 350,350);
       }   
    }

首先使用其中的Ball paint方法的值代替:

// method for the color and drawing the ball
public void paint(Graphics g) {
     g.setColor(Color.red);
     g.fillOval(80,70, 350,350);
}

它看起來像

// method for the color and drawing the ball
public void paint(Graphics g) {
      g.setColor(Color.red);
      g.fillOval(x,y, WIDTH, HEIGHT);
}   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM