Aster_a
Aster_a

Reputation: 1

can not make move the object in java

I am trying to write simple code in java for a moving ball program. im new to java, i mean I know the basics one so here it is my code, in case you can help me.I cant move the ball object. I created the class frame which have the gui component and also the ball class with the features of he 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);       
       }
           }

// the ball class

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

Upvotes: 0

Views: 51

Answers (1)

AxelH
AxelH

Reputation: 14572

Start by using the values of Ball in it paint methods instead of :

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

It should look like

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

Upvotes: 1

Related Questions