11 Replies - 681 Views - Last Post: 19 January 2013 - 10:24 AM Rate Topic: -----

#1 amjadabdallah  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 18-January 13

how to make a rectangle bounce in java

Posted 19 January 2013 - 06:35 AM

hey guys i made a code that let the rectangle move move in y coordinate and x coordinate but when it touches the JFrame it stopes moving, so can any one tell me how to let it bounce?
Is This A Good Question/Topic? 0
  • +

Replies To: how to make a rectangle bounce in java

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,305
  • Joined: 20-September 08

Re: how to make a rectangle bounce in java

Posted 19 January 2013 - 06:38 AM

Post code please
Was This Post Helpful? 0
  • +
  • -

#3 amjadabdallah  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 18-January 13

Re: how to make a rectangle bounce in java

Posted 19 January 2013 - 07:10 AM

ok but there is two codes the first one
package tutorial;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;

public class DrawingBoard extends JPanel implements ActionListener {

    private Timer timer;
    private final int DELAY = 30;
    private int rectX;
    private int rectY;

    DrawingBoard() {
        rectX = 0;
        rectY = 0;

        timer = new Timer(DELAY, this);
        timer.start();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.BLACK); 
        g.fillRect(0, 0, 400, 400);
        g.setColor(Color.RED);
        g.fillRect(rectX, rectY, 100, 100);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        rectX++; 
        rectY++;
       repaint();
    }


the second one is
package tutorial;

import javax.swing.JFrame;

public class Tutorial extends JFrame {

    public Tutorial() {
        super("Tutorial");

        add(new DrawingBoard()); // here I've added the JPanel to the JFrame
        
        setSize(400, 400);
        setResizable(false);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        Tutorial t = new Tutorial();
    }
}

Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: how to make a rectangle bounce in java

Posted 19 January 2013 - 07:21 AM

Instead of just incrementing the x and y each time, you should maintain a velocity variables for the x and y. Then on each actionPerformed, you add on the corresponding velocity to the x and y rectangle position.

To make it bounce you need to test whether the current x position is the same as the length of the jframe. You can use the .getSize() method or equivalent to accomplish that. When the x coordinate is the same as the length, invert the sign of the x velocity. Do the same for the y with the height of the jframe.

You will also need to offset the size of the rectangle in your tests to avoid it 'going through the walls'.
Was This Post Helpful? 1
  • +
  • -

#5 amjadabdallah  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 18-January 13

Re: how to make a rectangle bounce in java

Posted 19 January 2013 - 07:31 AM

srr i didnt get anything of what u said, i am new at programming so can u explain more please
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: how to make a rectangle bounce in java

Posted 19 January 2013 - 07:41 AM

So, not your code? Having to explain the code you have - wherever you got it - and then how to change it is different than just explaining how to change code you wrote yourself and are familiar with.
Was This Post Helpful? 0
  • +
  • -

#7 amjadabdallah  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 18-January 13

Re: how to make a rectangle bounce in java

Posted 19 January 2013 - 07:45 AM

sorry i dont get you
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,305
  • Joined: 20-September 08

Re: how to make a rectangle bounce in java

Posted 19 January 2013 - 08:02 AM

What you need to do is use a mathematical function to calculate the bounce height at each Timer tick. Google on something like 'math bounce formula'
Was This Post Helpful? 0
  • +
  • -

#9 amjadabdallah  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 18-January 13

Re: how to make a rectangle bounce in java

Posted 19 January 2013 - 08:07 AM

ok
Was This Post Helpful? 0
  • +
  • -

#10 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,533
  • Joined: 29-July 11

Re: how to make a rectangle bounce in java

Posted 19 January 2013 - 08:15 AM

If you waqnt a realistic physics based bounce, similar to how a ball bounces in real life, then there are equations of motion and gravity that you need to implement. Google and youtube are your friend for that. Af for simple making a ball continually bounce back and forth, Ryano121's answer is perfect.
Was This Post Helpful? 0
  • +
  • -

#11 amjadabdallah  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 8
  • Joined: 18-January 13

Re: how to make a rectangle bounce in java

Posted 19 January 2013 - 08:39 AM

the problem is that i dont know what are you guys talking about i am soooo new to programming
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: how to make a rectangle bounce in java

Posted 19 January 2013 - 10:24 AM

instead of simply do

    rectY++;
    rectY++;
    repaint();



define 2 variables deltaX et deltaY int your instance variables.
In your constructor initialize them to +1

in your actionPerformed() then

    rectX += deltaX;
    if(rectX < 0)
       deltaX = +1;
    if(rectX > getWidth())
       deltaX = -1;

    rectY += deltaY;
    if(rectY < 0)
       deltaY = +1;
    if(rectY > getHeigth())
       deltaY = -1;

    repaint();


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1