-2

I have obtained from somewhere else a Java Swing code for a bouncing Ball. The code uses a class "Ball" which extends a JPanel.

Can Anyone help me converting this code to extends JFrame instead.

I want to do that so I could be able to call it from another frame class. Here is the code:

public class Ball extends JPanel{ 
    int x=0, y=0;
    int angleX = 1, angleY = 1;

    public void move(){
        if (x + angleX <0) {
            angleX =1;
        } else if (x + angleX >getWidth()-50){
            angleX =-1;
        } else if (y + angleY <0) {
            angleY =1;
        } else if (y + angleY >getHeight()-50){
            angleY =-1;
        }
        x = x + angleX;
        y = y + angleY;
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillOval(x, y, 50, 50);
    }

    public static void main(String[] args){
        JFrame jfrm= new JFrame("BounceBall");
        jfrm.setSize(400,400);
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jfrm.setVisible(true); 

        Ball bl = new Ball();
        Component add = jfrm.add(bl);

        while (true){
            bl.move();
            bl.repaint();
            try{
               Thread.sleep(10);
            }catch(InterruptedException e){   
            }
        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
moh19814
  • 133
  • 1
  • 1
  • 14
  • 3
    *"Can Anyone help me converting this code to extends JFrame instead."* 1) Just **add** the panel to a frame! There is absolutely no reason to extend a frame. 2) SO is for specific technical questions, whereas you seem to be trying to source a tutor. Voting to close as 'too broad'.. – Andrew Thompson Jul 16 '16 at 17:25
  • 3
    *"I have obtained from somewhere else a Java Swing code.."* The author of that code did not know what they were doing well enough to know they were overriding the wrong paint method, so the best advice we could offer is to put it back where you found it, and go through the [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/) lesson of the Java Tutorial. – Andrew Thompson Jul 16 '16 at 17:27
  • 1
    *"I want to do that so I could be able to call it from another JFrame class."* Wow! So many poor ideas in such a short question. See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Jul 16 '16 at 17:28
  • What do you mean *"call it from another JFrame class"*? As in make some sort of pop up after e.g. button press? If so, you should use JDialog, and add this panel to it. – Coderino Javarino Jul 16 '16 at 18:02

1 Answers1

0

Just extend JFrame and make some constructor for Ball class.

  • You can make an instance from Ball class from any other JFrame classes.
    • Ball bl = new Ball();/*then call methods*/
  • Or just call Balls main method in two ways:
    • Ball.main(null);
    • String args[]={/*some arguments*/}; Ball.main(args);

Here is your Ball class which now extends JFrame

import java.awt.Graphics;
import javax.swing.JFrame;

public class Ball extends JFrame {

    public Ball() {
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    int x = 0, y = 0;
    int angleX = 1, angleY = 1;

    public void move() {
        if (x + angleX < 0) {
            angleX = 1;
        } else if (x + angleX > getWidth() - 50) {
            angleX = -1;
        } else if (y + angleY < 0) {
            angleY = 1;
        } else if (y + angleY > getHeight() - 50) {
            angleY = -1;
        }
        x = x + angleX;
        y = y + angleY;
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillOval(x, y, 50, 50);
    }

    public static void main(String[] args) {

        Ball bl = new Ball();

        while (true) {
            bl.move();
            bl.repaint();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
            }
        }
    }
}
Rahmat Waisi
  • 1,293
  • 1
  • 15
  • 36
  • 1
    Your answer is encouraging bad practices by replicating the problems in the original source code. Added to that, it extends frame which, although that is what the OP asked for, is completely unnecessary. – Andrew Thompson Jul 17 '16 at 06:20
  • 1
    `Anyone help me converting this code to extends JFrame instead.` i just helped, and i changed some part of original code. where i replicated the problems? to solve problem at last we must change original source code. is there another way to solve problem? – Rahmat Waisi Jul 17 '16 at 06:24
  • *"i just helped"* That (lack of quality of) code is not helping. *"where i replicated the problems?"* The first is mentioned in my 2nd comment. The second is using a `while (true){` loop instead of using a Swing `Timer` to trigger repaint.. – Andrew Thompson Jul 17 '16 at 06:28
  • @AndrewThompson i will use your advice in my next activities on `stackoverflow`. and also in my codes. thank you. – Rahmat Waisi Jul 17 '16 at 06:36