ObjetoGrafico.java
1
2 import java.awt.Graphics;
3 import java.awt.Rectangle;
4
5
6
7
8
9
10 public abstract class ObjetoGrafico {
11 private boolean visible=false;
12 protected int posX;
13 protected int posY;
14 protected int ancho;
15 protected int alto;
16 public ObjetoGrafico(int x,int y,int an,int al){
17 posX = x;
18 posY = y;
19 ancho= an;
20 alto= al;
21 }
22
23 public abstract void paint(Graphics g);
24
25 public void setAncho (int an){
26 ancho = an;
27 }
28 public void setAlto (int al){
29 alto = al;
30 }
31 public void moverArriba(int dy){
32 posY -= dy;
33 }
34 public void moverAbajo(int dy){
35 posY += dy;
36 }
37 public void moverIzquierda(int dx){
38 posX -= dx;
39 }
40 public void moverDerecha(int dx){
41 posX += dx;
42 }
43
44 public boolean getVisible(){
45 return visible;
46 }
47
48 public void setVisible(boolean v) {
49 visible = v;
50 }
51
52 public int getPosX(){
53 return posX;
54 }
55
56 public int getAncho(){
57 return ancho;
58 }
59
60 public int getAlto(){
61 return alto;
62 }
63
64 public int getPosY(){
65 return posY;
66 }
67
68
69 public void setPosX(int x) {
70 posX = x;
71 }
72
73 public void setPosY(int y) {
74 posY = y;
75 }
76
77 public boolean colisiona(ObjetoGrafico otro) {
78 Rectangle r1=new Rectangle(posX,posY,ancho,alto);
79 Rectangle r2=new Rectangle(otro.posX,otro.posY,otro.ancho,otro.alto);
80 return r1.intersects(r2) && getVisible() && otro.getVisible();
81 }
82
83 }
84
85
86
CategoryJava | CategoryProgramacion