Results 1 to 3 of 3
Thread: Multiple bouncing balls
- 04-09-2008, 03:45 PM #1
Member
- Join Date
- Apr 2008
- Posts
- 2
- Rep Power
- 0
Multiple bouncing balls
Hi. My goal with this app. is to make several balls bounce on each other and on the walls, obviously.. Problem is that i can only get it to work with one ball.
Any idea how to do it by my coding? Im from sweden so allot is written in swedish.. hope it doesnt annoy one..
I would like to add the new ball by just doing the red-lighted-line.
I hade the init(); method as a konstructor for Boll at first, but that was a bad idea. Didnt work..
A lot of text but, plz?
package studsboll;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Fönster extends JFrame implements ActionListener {
private Boll boll= new Boll();
// private Boll boll2= new Boll();
public Fönster(){
setTitle("Studdis");
boll.setPreferredSize(new Dimension(350,250));
boll.setBackground(Color.white);
add(boll, BorderLayout.CENTER);
pack();
boll.init(200,10);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
boll.requestFocus();
}
public static void main(String[] args) {
Fönster a= new Fönster();
}
}
class Boll extends JPanel implements ActionListener{
private Timer timer = new Timer(100, this);
private int xMax, yMax; //Storlek på boll (Ball size)
private int r, x0, y0; //Bollens radie, och mittpunkt (Ball radius and midpoint)
private int xSteg, ySteg; //Bollens steglängd (Ball step-length)
private int v, v0= 5; //Bollens hastighet (Ball velocity)
public void init(int storlek, int hastighet){ (arg: size and velocity)
xMax= getSize().width-1; //bollstorlek x-led
yMax= getSize().height-1; //bollstorlek y-led
r= storlek/10; //Bollens radie
xSteg= ySteg= v= v0= hastighet; //Utgångshastighet (Start velocity)
x0= r; //Placera bollen (Place the ball)
y0= yMax; // --||--
addComponentListener(xyPlan); //Ändringar av spelplanens storlek (If changing the frame-size)
timer.start();
}
public void actionPerformed(ActionEvent e){ //Hit kommer man var 100:e millisekund, anropas av timern
if (x0-r <= 0){ //Är bollen i vänsterkanten? (Left border?)
v++; //Öka hastigheten
xSteg= v; //Flytta åt höger nästa gång (Move right next time)
}
else if (x0+r >= xMax){ //Är bollen i högerkant? (Right border?)
v++; //Öka hastigheten
xSteg= -v; //Flytta åt vänster nästa gång
}
if (y0-r <= 0 || y0+r >= yMax) //I över- eller underkant?
ySteg= -ySteg; //Byt vertikal riktning
x0+= xSteg; //Flyttar bollen horisontellt
y0+= ySteg; //Flyttar bollen vertikalt
if (x0 < r) //Om bollen hamna för långt å vänster
x0= r;
else if (x0 > xMax-r) //Hamnade bollen för långt åt höger?
x0= xMax-r+1;
if (y0 < r) //Hamnade bollen för långt upp?
y0= r;
else if (y0 > yMax-r) //Hamnade bollen för lånt ner?
y0= yMax-r+1;
repaint();
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.getHSBColor(60, 4, 100));
g.fillOval(x0-r, y0-r, 2*r, 2*r); //Rita bollen
}
ComponentListener xyPlan= new ComponentAdapter() { //Om spelplanens storlek ändras
@Override
public void componentResized(ComponentEvent e){ //In i metoden..
xMax= e.getComponent().getSize().width-1; //Ändra storleken
yMax= e.getComponent().getSize().height-1;
e.getComponent().requestFocus();
repaint(); //Måla upp det nya fönstret
}
};
}
- 04-24-2008, 05:32 PM #2
Java Code:import java.awt.*; import java.awt.event.*; import java.util.*; import java.util.List; import javax.swing.*; import javax.swing.Timer; class Funster extends JFrame { BollPanel bollPanel; public Funster(){ setTitle("Studdis"); bollPanel = new BollPanel(); bollPanel.setBackground(Color.white);n bollPanel.setPreferredSize(new Dimension(350,250)); bollPanel.addBoll(new Boll(200, 10, Color.red)); bollPanel.addBoll(new Boll(100, 20, Color.blue)); add(bollPanel, BorderLayout.CENTER); pack(); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { new Funster(); } } class Boll { private int xMax, yMax; //Storlek på boll (Ball size) private int r, x0, y0; //Bollens radie, och mittpunkt // (Ball radius and midpoint) private int xSteg, ySteg; //Bollens steglängd (Ball step-length) private int v, v0= 5; //Bollens hastighet (Ball velocity) Color color; public Boll(int storlek, int hastighet, Color color){ // (arg: size and velocity) this.color = color; r= storlek/10; //Bollens radie xSteg= ySteg= v= v0= hastighet; //Utgångshastighet (Start velocity) x0= r; //Placera bollen (Place the ball) y0= yMax; // --||-- } public void step(int width, int height) { xMax= width-1; //bollstorlek x-led yMax= height-1; //bollstorlek y-led //Hit kommer man var 100:e millisekund, anropas av timern if (x0-r <= 0){ //Är bollen i vänsterkanten? (Left border?) v++; //Öka hastigheten xSteg= v; //Flytta åt höger nästa gång (Move right next time) } else if (x0+r >= xMax){ //Är bollen i högerkant? (Right border?) v++; //Öka hastigheten xSteg= -v; //Flytta åt vänster nästa gång } if (y0-r <= 0 || y0+r >= yMax) //I över- eller underkant? ySteg= -ySteg; //Byt vertikal riktning x0+= xSteg; //Flyttar bollen horisontellt y0+= ySteg; //Flyttar bollen vertikalt if (x0 < r) //Om bollen hamna för långt å vänster x0= r; else if (x0 > xMax-r) //Hamnade bollen för långt åt höger? x0= xMax-r+1; if (y0 < r) //Hamnade bollen för långt upp? y0= r; else if (y0 > yMax-r) //Hamnade bollen för lånt ner? y0= yMax-r+1; } public void draw(Graphics g) { g.setColor(//Color.getHSBColor(60, 4, 100)); color); g.fillOval(x0-r, y0-r, 2*r, 2*r); //Rita bollen } } class BollPanel extends JPanel implements ActionListener{ List<Boll> bolls = new ArrayList<Boll>(); private Timer timer = new Timer(100, this); public BollPanel() { // addComponentListener(xyPlan); // //Ändringar av spelplanens storlek (If changing the frame-size) timer.start(); } public void actionPerformed(ActionEvent e){ int w = getWidth(); int h = getHeight(); for(Boll boll : bolls) { boll.step(w, h); } repaint(); } @Override public void paintComponent(Graphics g){ super.paintComponent(g); for(Boll boll : bolls) { boll.draw(g); } } public void addBoll(Boll boll) { bolls.add(boll); } /* ComponentListener xyPlan= new ComponentAdapter() { //Om spelplanens storlek ändras @Override public void componentResized(ComponentEvent e){ //In i metoden.. xMax= e.getComponent().getSize().width-1; //Ändra storleken yMax= e.getComponent().getSize().height-1; e.getComponent().requestFocus(); repaint(); //Måla upp det nya fönstret } }; */ }
- 04-24-2008, 08:35 PM #3
Member
- Join Date
- Apr 2008
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
bouncing ball issue
By adam405 in forum New To JavaReplies: 1Last Post: 03-18-2008, 03:48 AM -
Movement of balls
By BlitzA in forum New To JavaReplies: 8Last Post: 01-09-2008, 03:30 PM -
Problem deleting ball from bouncing ball app
By adlb1300 in forum New To JavaReplies: 2Last Post: 12-03-2007, 09:08 PM -
Bouncing Ball Just Suddenly Stops Mid Bounce
By adlb1300 in forum Java 2DReplies: 1Last Post: 12-03-2007, 02:58 PM -
multiple databases
By varunthecool in forum JDBCReplies: 2Last Post: 07-09-2007, 08:06 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks