java - 无法在java中移动对象

标签 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);
       }   
    }

最佳答案

首先在 paint 方法中使用 Ball 的值而不是:

// 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);
}   

关于java - 无法在java中移动对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47072249/

相关文章:

java - 如何使 HTTP 连接在一段时间后超时/断开连接?

java - 代码没有打印任何内容

java - 安卓工作室-Gradle : Execution failed for task ':Foo:dexDebug' - but why?

java - NO_SECURITY 在 Jetty 的 ServletContextHandler 中起什么作用?

java - BufferedImage 有两种类似的方法,一种有效,一种无效。为什么?

java - Observable 由冷和热 Observable 组成

java - 为什么嵌套枚举在java中是隐式静态的?

基于 Enum 字段的 Java 类

java - 构建 Maven Java 项目时如何包含其他 Java 项目

java - 使用 Java 8 检查数组中是否存在元素