konvictboy85
Junior Newbie
|
 |
«
Posted
2013-08-16 11:12:23 » |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| package minigames;
import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints;
import javax.swing.JFrame; import javax.swing.JPanel;
public class fallingball extends JPanel { int x = 250; int y = 0; private void moveBall() { y = y + 1; }
public void paint(Graphics g){ super.paint(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setColor(Color.black); g2d.fillOval(x, y, 30, 30); } public static void main(String[] args) throws InterruptedException{ JFrame f = new JFrame("Falling Ball"); fallingball game = new fallingball(); f.add(game); f.setSize(500, 500); f.setResizable(false); f.setLocationRelativeTo(null); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); while (true) { game.moveBall(); game.repaint(); Thread.sleep(10); } }
} |
only 1 ball is generated and it gets fallen down. i wanted to generate balls falling randomly and from different x co-ordinate in the frame how can i do that
|
|
|
|
sirkarpfen
|
 |
«
Reply #1 - Posted
2013-08-16 12:27:58 » |
|
1. This is kind of the wrong forum to post this kind of question.
2. You have to create a loop if you want more balls to fall down.
3. You have to randomly set the x-coordinate using maybe the java.util.Random class.
And last but not least: You don't begin a question with a bunch of code. Maybe next time start with a little introduction on what you try to accomplish...
|
|
|
|
konvictboy85
Junior Newbie
|
 |
«
Reply #2 - Posted
2013-08-16 13:02:21 » |
|
oh m sorry but can u tell me where can i get hep for those types of problems. and m sorry for unusual topic in the incorrect place.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Opiop
|
 |
«
Reply #3 - Posted
2013-08-16 13:15:02 » |
|
Google Java Random and click on the link for the java documents site. Read up on it. Then learn the basic structure of a for loop. This is simple stuff, why did you get into graphics before learning the basics of the language?
|
|
|
|
sirkarpfen
|
 |
«
Reply #4 - Posted
2013-08-16 13:18:02 » |
|
Maybe someone of the dukes could move this post to the right forum?  I allready answered your questions. But it seems you kinda lack some java basics. Maybe you should start with that before trying to write games. Here is an example for creating a random amount of balls with random x-coordinates: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| public class FallingBallTest {
private int[] balls; private int y; private int width; public boolean running = false;
public FallingBallTest() { JFrame f = new JFrame("Falling Ball Test"); f.add(this); f.setSize(500, 500); f.setResizable(false); f.setLocationRelativeTo(null); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); width = this.getWidth(); Random rand = new Random(); balls = new int[rand.nextInt(10)]; for(int i = 0; i < xCoords.length; i++) balls[i] = rand.nextInt(width); this.startGame(); }
public void startGame() { running = true; while(running) { this.update(); this.repaint(); } }
public void update() { y++ }
public void paintComponent(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setColor(Color.black); for(int x : balls) { g2d.fillOval(x, y, 30, 30); } }
public void main(String[] args) { new FallingBallTest(); } } |
I give no guarantee that it actually works, was just scribbling it here before i have to go  .
|
|
|
|
Opiop
|
 |
«
Reply #5 - Posted
2013-08-16 13:25:03 » |
|
The only thing I want to point out in your code so that the OP understands is that is actually the array of balls. Sorry but it seemed like a very vague and not related name for an array of balls!
|
|
|
|
sirkarpfen
|
 |
«
Reply #6 - Posted
2013-08-16 17:13:21 » |
|
The only thing I want to point out in your code so that the OP understands is that is actually the array of balls. Sorry but it seemed like a very vague and not related name for an array of balls! Thanks for pointing that out, i changed it  .
|
|
|
|
jonjava
|
 |
«
Reply #7 - Posted
2013-08-16 19:31:46 » |
|
I'd say you're missing a few layers of useful abstraction. In particular a separate ball object and a rough entity system which is just a fancy term of "a list of sprites" basically. Also you should separate some of your logic into their own functions, your game loop for instance. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| public class fallingball extends JPanel { List<Ball> balls = new ArrayList<Ball>();
class Ball { int x; int y;
Ball(int x, int y) { this.x = x; this.y = y; } void move() { y = y + 1; } }
public void paint(Graphics g){ super.paint(g); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setColor(Color.black);
for (int i = 0; i < balls.size(); i++) { Ball b = balls.get(i); g2d.fillOval(b.x, b.y, 30, 30); } }
public void doGameLoop() { for (int i = 0; i < balls.size(); i++) { Ball b = balls.get(i); b.move(); } game.repaint(); } public static void main(String[] args) throws InterruptedException{ JFrame f = new JFrame("Falling Ball"); fallingball game = new fallingball(); f.add(game); f.setSize(500, 500); f.setResizable(false); f.setLocationRelativeTo(null); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Random r = new Random();
for (int i = 0; i < 10; i++) { int x = r.nextInt(100); int y = 0;
Ball ball = new Ball(x,y);
balls.add(ball); }
while (true) { doGameLoop(); Thread.sleep(10); } }
} |
|
|
|
|
sirkarpfen
|
 |
«
Reply #8 - Posted
2013-08-16 20:04:47 » |
|
I'd say you're missing a few layers of useful abstraction. In particular a separate ball object and a rough entity system which is just a fancy term of "a list of sprites" basically. Also you should separate some of your logic into their own functions, your game loop for instance.
I'd say the OP would need some more basic java, because that is what you learn when learning the basics of the language...
|
|
|
|
Opiop
|
 |
«
Reply #9 - Posted
2013-08-16 21:51:22 » |
|
I agree. I almost would go as far to say that he doesn't know what classes are. Besides, for such a small project that extra ball class really isn't needed.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
|
Opiop
|
 |
«
Reply #11 - Posted
2013-08-17 02:01:14 » |
|
OP really isn't advanced enough to probably understand what you're talking about. Plus, if you would've read the other posts, this question has already been answered a few times.
|
|
|
|
Andre Lopes
|
 |
«
Reply #12 - Posted
2013-08-17 02:24:48 » |
|
OP really isn't advanced enough to probably understand what you're talking about. Plus, if you would've read the other posts, this question has already been answered a few times.
I was just trying to help in a simple"r" way. Plus i put the video tutorials if you would've read my post fully.
|
|
|
|
|