View Single Post
  #1 (permalink)  
Old 04-09-2008, 04:45 PM
Algar Algar is offline
Member
 
Join Date: Apr 2008
Posts: 2
Algar is on a distinguished road
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
}
};
}
Reply With Quote
Sponsored Links