Generating random sprites

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

  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…

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.

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?

Maybe someone of the dukes could move this post to the right forum? :slight_smile:

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:


public class FallingBallTest {

      private int[] balls; // Array of x values for the various 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 :).

The only thing I want to point out in your code so that the OP understands is that

xCoords[]

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 :).

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.

public class fallingball extends JPanel {
   
   // Make a list of balls
   List<Ball> balls = new ArrayList<Ball>();

   // separate the coordinates and move() function into its own Ball class
   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);

      // go through every Ball in the list of balls and paint each one.
      for (int i = 0; i < balls.size(); i++) {
         Ball b = balls.get(i);
         g2d.fillOval(b.x, b.y, 30, 30);
      }
   }

   public void doGameLoop() {
      // go through every Ball in the list of balls and update each one.
      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);
      
      // Create a new random generator
      Random r = new Random();

      // create 10 balls at random places
      for (int i = 0; i < 10; i++) {
         int x = r.nextInt(100);
         int y = 0;

         // Create the ball
         Ball ball = new Ball(x,y);

         // add the newly created ball to the list of balls.
         balls.add(ball);
      }

      while (true) {
         doGameLoop();
         Thread.sleep(10);
      }
   }

}

I’d say the OP would need some more basic java, because that is what you learn when learning the basics of the language…

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.

Kon, First, lets make things easier.

Make a Ball Class.

The ball class must contain some things ;

int positionX,positionY;

and two methods at least

update()
render()

Then to make random Numbers , you can use Math.Random(…);

But since you probably isnt so familiar with java,
i recommend two channels :

and

Hope it helps :stuck_out_tongue:

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. :stuck_out_tongue: