|
On clicking the start button |
|
Changed the ball's color to hgb |
|
Using stop button |
Using java swings ,I tried to make the zig zag motion of a ball when it strikes the boundaries of the rectangle on clicking the "start" button It has a "stop" button and another button to change the ball's color to HGB.
The code is quite simple .First of all create a frame using JFrame as follow
JFrame a = new JFrame("Zig Zag");
After declaring its attributes ,create a class say zigzag which inherits JPanel and another class say bounce which inherits JPanel and an interface ActionListener.Now declare an object of class bounce say bounce b;
After that declare the constructor zigzag() as follow
|
Before clicking the start button |
b = new bounce();
JButton startButton = new JButton("Start");
JButton hgbButton = new JButton("Change color to HGB");
JButton stopButton = new JButton("Stop");
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
buttonPanel.add(startButton);
buttonPanel.add(hgbButton);
buttonPanel.add(stopButton);
stopButton.setText("Stop now !");
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.add(buttonPanel);
this.add(b);
Now,for the start button set animation to true and for stop button set animation to false .To change the color to hgb use :
b.setBallColor(Color.getHSBColor(TOP_ALIGNMENT, TOP_ALIGNMENT, TOP_ALIGNMENT));
Now,inside the class bounce define the radius,diameter ,velocity across Xaxis and Yaxis , paint()..