//Ball.java
import java.awt.*;
import javax.swing.*;
public class Ball
{
private int x, y, r, dx, dy, rBound, bBound;
private Color c;
public Ball(int x1, int y1, int r1, Color c1, int dx1, int dy1)
{
x =x1; y=y1; r=r1; c=c1; dx = dx1; dy = dy1;
}
public void Draw (Graphics g)
{
g.setColor(c);
g.fillOval(x,y,r,r);
}
public void Move ()
{
x = x + dx;
if (x > rBound - r ) {dx=-dx;}
if( x<0) {dx=-dx;}
y = y + dy;
if (y> bBound - r ){dy=-dy;}
if( y<0 ){ dy = -dy;}
}
public void setBound(int w, int l)
{
rBound = w;
bBound = l;
}
public void setData(int DX, int DY, int R)
{
if (dx<0)
dx = -1 * DX;
else
dx = DX;
if (dy<0)
dy = -1 * DY;
else
dy = DY;
r = R;
}
}
//BallPanel.java
import java.awt.*;
import javax.swing.*;
public class BallPanel extends JPanel
{
private Game G;
public Timer timer = new Timer(50, (new TimerHandler()));
public BallPanel thisBallPanel = this;
private int Dx,Dy;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
G.B.setBound(this.getWidth(), this.getHeight());
G.B.Move();
G.B.Draw(g);
}
public BallPanel(Game G)
{
this.G=G;
setLayout(new FlowLayout());
setBackground(Color.ORANGE);
timer.start();
JLabel label = new JLabel(" BALL FIELD ");
add(label);
this.G = G;
label.setForeground(Color.MAGENTA);
label.setFont(new Font("Tahoma Small Cap", Font.ITALIC, 26)); // set font size of a label
label.setOpaque(true);
}
//this is called threading
private class TimerHandler implements java.awt.event.ActionListener
{
public void actionPerformed (java.awt.event.ActionEvent ae)
{
G.B.setData(G.BtP.Dx,G.BtP.Dy,G.BtP.R);
thisBallPanel.repaint();
}
}
}
//ButtonPanel.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ButtonPanel extends JPanel
{
//static can be used anywhere with the same value
private Game G;
private static JLabel L1,L2,L3,L4;
private static JButton increase, decrease, quit;
private static JRadioButton dx, dy,r, n;
private static ButtonGroup group;
private ButtonHandler bh;
public int Dx=30,Dy=30, R=30, max=100, min=0, nn=1;
public ButtonPanel() // Define the constructor.
{
super();
bh=new ButtonHandler();
setLayout(new GridLayout(11,1));
setBackground(Color.BLUE);
L1=new JLabel("CONTROL PANEL",L1.CENTER);
add(L1);
L1.setForeground(Color.orange);
dx = new JRadioButton("Dx");
add(dx);
dx.addActionListener(bh);
dy = new JRadioButton("Dy");
add(dy);
dy.addActionListener(bh);
r = new JRadioButton("Radius");
add(r);
r.addActionListener(bh);
n = new JRadioButton("N");
add(n);
n.addActionListener(bh);
group = new ButtonGroup();
group.add(dx);
group.add(dy);
group.add(r);
group.add(n);
L2=new JLabel("INCREASE",L2.CENTER);
add(L2);
L2.setForeground(Color.orange);
increase = new JButton("+");
add(increase);
increase.addActionListener(bh);
L3=new JLabel("DECREASE",L3.CENTER);
add(L3);
L3.setForeground(Color.orange);
decrease = new JButton("-");
add(decrease);
decrease.addActionListener(bh);
L4=new JLabel("QUIT",L4.CENTER);
add(L4);
L4.setForeground(Color.orange);
quit = new JButton("quit");
add(quit);
quit.addActionListener(bh);
}
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == quit) System.exit(0);
if (ae.getSource() == increase && dx.isSelected())
{
Dx = Dx + 5;
if (Dx > max ) Dx = max;
dx.setText("dx=" + Dx );
}
if (ae.getSource() == decrease && dx.isSelected())
{
Dx = Dx - 5;
if (Dx < min ) Dx = min;
dx.setText("dx=" + Dx );
}
if (ae.getSource() == increase && dy.isSelected())
{
Dy = Dy + 5;
if (Dy > max ) Dy = max;
dy.setText("dy=" + Dy );
}
if (ae.getSource() == decrease && dy.isSelected())
{
Dy = Dy - 5;
if (Dy < min ) Dy = min;
dy.setText("dy=" + Dy );
}
if (ae.getSource() == increase && r.isSelected())
{
R = R + 5;
if (R > max ) R = max;
r.setText("r=" + R );
}
if (ae.getSource() == decrease && r.isSelected())
{
R = R - 5;
if (R < min ) R = min;
r.setText("r=" + R );
}
if (ae.getSource() == increase && n.isSelected())
{
nn = nn + 1;
if(nn>=10) nn=10;
repaint();
n.setText("N="+nn);
}
}
}
}
//Game.java
import java.awt.*;
import javax.swing.*;
public class Game extends JFrame
{
public BallPanel BlP=new BallPanel(this);
public ButtonPanel BtP=new ButtonPanel();
public Ball B = new Ball(100,100,30,Color.blue, 5, 5);
public Game()
{
super("Game");
setLayout(new BorderLayout());
add( BlP, BorderLayout.CENTER);
add( BtP, BorderLayout.EAST);
setSize(800,500);
setVisible(true);
}
public static void main(String args[])
{
Game G=new Game();
G.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Can someone help with program. I don't know what is wrong with it. I am supposed to add one radio button to increase the number of balls. I added One RadioButton in ButtonPanel and also I added other modifications but it's not working. It doesn't increase the number of balls.
Can someone tell me what's wrong with the code.
Thank you.

New Topic/Question
Reply



MultiQuote



|