4 Replies - 2015 Views - Last Post: 23 November 2008 - 09:35 PM Rate Topic: -----

#1 kakakoko   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 15-November 08

increase the number of balls

Post icon  Posted 22 November 2008 - 07:09 PM

//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.

Is This A Good Question/Topic? 0
  • +

Replies To: increase the number of balls

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: increase the number of balls

Posted 22 November 2008 - 09:08 PM

One thing for sure
The comments in the code a really usefull :)
Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: increase the number of balls

Posted 22 November 2008 - 09:45 PM

Actually a very cute nice little application
There is a blue ball that moves inside a yellow panel and bounced when it hits the sides of the panel
Another panel, called the control panel, has buttons that allow to increase/decrease the radius of the ball and the displacement of it in pixels on the X and Y axis
Should be eay to make an array of balls if all of them have the same radius and sped
Was This Post Helpful? 0
  • +
  • -

#4 kakakoko   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 15-November 08

Re: increase the number of balls

Posted 23 November 2008 - 04:56 PM

yes it's ok to use the array to create more than one ball, but I want to create a button, when I press the button, I will be able to increase and decrease the number of balls (ActionEvent).

Thanks.
Was This Post Helpful? 0
  • +
  • -

#5 kakakoko   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 15-November 08

Re: increase the number of balls

Posted 23 November 2008 - 09:35 PM

This is how I modified : ButtonPanel.java, BallPanel.java and Game.java but I still have only one ball.

//ButtonPanel.java

import java.awt.*;
import javax.swing.*;
import java.util.Arrays;
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, n;
	private static JRadioButton dx, dy,r;
	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 JButton("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;
					G.B.n=nn;
					n.setText("N="+nn);
			}
	
			}
			
																	
		} 
	
	
}	  


//BallPanel.java

import java.awt.*;
import javax.swing.*;
import java.util.Arrays;
import java.awt.event.*;


public class BallPanel extends JPanel
{
	public Game G;
	public Timer timer = new Timer(50, (new TimerHandler()));
	public BallPanel thisBallPanel = this;
	private int Dx,Dy;
	public int n=1;

	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		for(int i=0; i<n; i++)
		{
		
		G.B[i].setBound(this.getWidth(), this.getHeight());
		G.B[i].Move();
		G.B[i].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)
		{
			for(int i=0; i<n; i++)
		{
			G.B[i].setData(G.BtP.Dx,G.BtP.Dy,G.BtP.R);
			thisBallPanel.repaint();
		}
	}
}
}

//Game.java

import java.awt.*;
import javax.swing.*;
import java.util.Arrays;
import java.awt.event.*;

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 Ball B[]={ 
		new Ball(100,100,30,Color.blue, 5, 5),
		new Ball(200,200,30,Color.black, 5, 5),
		new Ball(300,300,30,Color.magenta, 5, 5),
		new Ball(400,400,30,Color.yellow, 5, 5),
		new Ball(500,500,30,Color.blue, 5, 5),
		new Ball(600,600,30,Color.blue, 5, 5),
		new Ball(700,700,30,Color.blue, 5, 5),
		new Ball(800,800,30,Color.blue, 5, 5),
		new Ball(900,900,30,Color.blue, 5, 5),
		new Ball(1000,1000,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);
	}
	
}




Could someone tell me what's wrong with the code.

Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1