Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-09-2008, 04:45 PM
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
}
};
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-24-2008, 06:32 PM
Senior Member
 
Join Date: Jul 2007
Posts: 910
hardwired is on a distinguished road
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 } }; */ }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-24-2008, 09:35 PM
Member
 
Join Date: Apr 2008
Posts: 2
Algar is on a distinguished road
Fixed it
Big thanx to the reply. It realy solved it. Im just gonna have to do my homework on List's. Big thanx
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
bouncing ball issue adam405 New To Java 1 03-18-2008 04:48 AM
Movement of balls BlitzA New To Java 8 01-09-2008 04:30 PM
Problem deleting ball from bouncing ball app adlb1300 New To Java 2 12-03-2007 10:08 PM
Bouncing Ball Just Suddenly Stops Mid Bounce adlb1300 Java 2D 1 12-03-2007 03:58 PM
multiple databases varunthecool Database 2 07-09-2007 09:06 AM


All times are GMT +3. The time now is 11:11 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org