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?
how to make a rectangle bounce in java
Page 1 of 111 Replies - 269 Views - Last Post: 19 January 2013 - 10:24 AM
Replies To: how to make a rectangle bounce in java
#2
Re: how to make a rectangle bounce in java
Posted 19 January 2013 - 06:38 AM
Post code please
#3
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
the second one is
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();
}
}
#4
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'.
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'.
#5
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
#6
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.
#7
Re: how to make a rectangle bounce in java
Posted 19 January 2013 - 07:45 AM
sorry i dont get you
#8
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'
#10
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.
#11
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
#12
Re: how to make a rectangle bounce in java
Posted 19 January 2013 - 10:24 AM
instead of simply do
define 2 variables deltaX et deltaY int your instance variables.
In your constructor initialize them to +1
in your actionPerformed() then
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();
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|