0

I'm trying to make a java GUI game version of the game called " jump it" (http://www.crazygames.com/game/jump-it). I am currently on the part of drawing randomized rectangles for my character to jump on. However, I don't know how to continuously draw more rectangles since I'm fairly new to GUI. So my code is below and I hope you guys can help me!

class BtnActPanel extends JPanel implements 
ActionListener{
private int x = 0,  
x1 = 650,  
y2 = (int)(Math.random()*100)+40,  
y1 = (int)(Math.random()*100)+450,  
x2 = (int)(Math.random()*600)+200;
.
.
.
}
          public void actionPerformed(ActionEvent e) {
          .
          .
          .

          else  if (e.getSource() == b3){
            JOptionPane.showMessageDialog(null, "This is an exit button, hope you enjoyed the game! :)", "Exit message",JOptionPane.WARNING_MESSAGE ); //shows exit message
            System.exit(0);//exits program
          } 

          else if (e.getSource() == t){
              if (index == 0){
                  index = 1;
                  c = arrImage[1];
              }
              else{
                  index = 0;
                  c = arrImage[0];
              }
              x = x-10;
              x1 = x1-10;
              repaint();
          }   
}

public void paintComponent(Graphics g){//this method draws and paints images and icons based on the user decisions
    super.paintComponent(g);
    if(check1 == 0)
        g.drawImage(icon.getImage(),0,0,null);

    if(check1 == 1){
        g.drawImage(b.getImage(),0,0,null);
        g.setColor(Color.black);
        g.fillRect(x,495, 500, 35);
        g.fillRect(x1, y1, x2, y2);
        g.drawImage(c.getImage(), 100, 460, null);
    }

    if(check1 == 2)
        g.drawImage(instruct.getImage(),0,0,null);
    b1.setBounds(320, 350, 100, 100);
    b2.setBounds(420, 350, 100, 100);
    b3.setBounds(520, 350, 100, 100);
}

}//end of class

Christy
  • 1
  • 1
  • In most games, you will have a "main loop". This loop inspects the current inputs, modifies the various states of the game and schedules a paint pass. Depending on the underlying framework you're using, this will be implemented differently. For example, in Swing, you can get away with a Swing `Timer`, as it ticks within the context of the Event Dispatching Thread, making it easier and safer to generally use – MadProgrammer Jan 01 '20 at 00:50
  • A other consideration you might need to think about is the creation of short lived objects. Short lived objects can put a strain on the system, as the GC mechanism will need to take more time to hunt down and collect those objects and object creation itself has an over head. A better idea might be to "pool" the objects and re-use them, modifying the parameters (location and size) as needed for your needs. It's a more complex idea, but will help maintain a smooth experience - [for example](https://stackoverflow.com/questions/14886232/swing-animation-running-extremely-slow/14902184#14902184) – MadProgrammer Jan 01 '20 at 00:53
  • Here are 2 examples of randomly spawning short-lived shapes using swing `Timer`: [example 1](https://stackoverflow.com/a/54158677/3992939) and [example 2](https://stackoverflow.com/a/43706965/3992939) – c0der Jan 01 '20 at 06:20

1 Answers1

0

You can set a timer or generate some in a loop.

You can adjust the thresholds as you see fit.

    static Random r = new Random();
    static int upperX = 100;
    static int lowerX = 20;
    static int upperY = 100;
    static int lowerY = 50;
    static int minWidth = 100;
    static int maxWidth = 300;
    static int minHeight = 50;
    static int maxHeight = 200;

    public static Rectangle newRect() {
        // All ranges inclusive of thresholds
        int x = r.nextInt(upperX-lowerX + 1) + lowerX; // from 20 to 100
        int y = r.nextInt(upperY-lowerY + 1) + lowerY; // from 50 to 100
        int w = r.nextInt(maxWidth-minWidth + 1) + minWidth; // from 100 to 300
        int h = r.nextInt(maxHeight - minHeight + 1) + minHeight; // from 50 to 200
        return new Rectangle(x,y,w,h);
    }
WJS
  • 36,363
  • 4
  • 24
  • 39