3 PELOTITAS REBOTANDO EN LA PANTALLA
--------------------------------------------------------------------------------------------------------------------------
package pelotas.rebotando;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Game2 extends JPanel { //TRAE EL LIENZO
Ball ball = new Ball(this,0,0,4,5);
Ball ball2 = new Ball(this,100,100,5,4);
Ball ball3 = new Ball(this,150,150,4,7);
private void move() {
Movimiento();
ball.move();
ball2.move();
ball3.move();
}
@Override //PINTADO DE PANTALLA
public void paint(Graphics g) {
super.paint(g); //BORRA LA PANTALLA
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Graphics2D g3d = (Graphics2D) g;
g3d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Graphics2D g4d = (Graphics2D) g;
g4d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
ball.paint(g2d); // OBJETO A PINTAR
ball2.paint(g3d);
ball3.paint(g4d);
}
public void Movimiento() {
if (ball.colision()) {
switch (ball.colision2()){
case 1:
ball.xa=-1*ball.getXa();
ball.ya=-1*ball.getYa();
ball2.xa=-1*ball2.getXa();
ball2.ya=-1*ball2.getYa();
break;
case 2:
ball.xa=-1*ball.getXa();
ball.ya=-1*ball.getYa();
ball3.xa=-1*ball3.getXa();
ball3.ya=-1*ball3.getYa();
break;
case 3:
ball2.xa=-1*ball2.getXa();
ball2.ya=-1*ball2.getYa();
ball3.xa=-1*ball3.getXa();
ball3.ya=-1*ball3.getYa();
break;
}
}
}
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Pelotas Rebotando"); //TRAE EL MARCO
Game2 game = new Game2();
frame.add(game);
frame.setSize(500, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// LLAMA AL METODO PAINT PARA ACTUALIZARZ LA PANTALLA
while (true) {
game.move();// PRIMERO MUEVE
game.repaint(); // DESPUES PINTA
Thread.sleep(10); // TIEMPO DE ESPERA EN MILISEGUNDOS
}
}
}
--------------------------------------------------------------------------------------------------------------------------
package pelotas.rebotando;
import java.awt.Graphics2D;
import java.awt.Rectangle;
public class Ball {
private static final int DIAMETER = 30;
int x ;
int y;
int xa;
int ya;
private Game2 game;
Ball(Game2 game,int x, int y, int xa, int ya) {
this.game= game;
this.x= x;
this.xa=xa;
this.y=y;
this.ya=ya;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getXa() {
return xa;
}
public int getYa() {
return ya;
}
public void move() { //METODO DE MOVIMIENTO
int c=1,d=1;
if (Ball.this.getXa() < 0) {
c=-1;
}
if (Ball.this.getYa()<0 p=""> d=-1;
}
if (Ball.this.getX() + Ball.this.getXa() < 0){
Ball.this.xa = c*Ball.this.getXa();
}
if (Ball.this.getX() + Ball.this.xa > game.getWidth() - 30) {
Ball.this.xa = -1*Ball.this.getXa();
}
if (Ball.this.getY() + Ball.this.getYa() < 0) {
Ball.this.ya= d*Ball.this.getYa();
}
if (Ball.this.getY() + Ball.this.getYa() > game.getHeight() - 30) {
Ball.this.ya= -1*Ball.this.getYa();
}
Ball.this.x = Ball.this.getX() + Ball.this.xa;
Ball.this.y = Ball.this.getY() + Ball.this.ya;
}
public void paint(Graphics2D g) {
g.fillOval(Ball.this.x, Ball.this.y, DIAMETER, DIAMETER);
}
public Rectangle getRECTANGULO() {
return new Rectangle(Ball.this.getX(), Ball.this.getY(), DIAMETER, DIAMETER);
}
public boolean colision() {
if (game.ball.getRECTANGULO().intersects(game.ball2.getRECTANGULO())){
return true;
} else {
if (game.ball.getRECTANGULO().intersects(game.ball3.getRECTANGULO())){
return true;
} else {
if(game.ball2.getRECTANGULO().intersects(game.ball3.getRECTANGULO())){
return true;
}else{
return false;
}}}
}
public int colision2() {
if (game.ball.getRECTANGULO().intersects(game.ball2.getRECTANGULO())){
return 1;
} else {
if (game.ball.getRECTANGULO().intersects(game.ball3.getRECTANGULO())){
return 2;
} else {
if(game.ball2.getRECTANGULO().intersects(game.ball3.getRECTANGULO())){
return 3;
}else{
return 0;
}}}
}}0>
No hay comentarios:
Publicar un comentario