-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrick.java
More file actions
42 lines (41 loc) · 1.36 KB
/
Brick.java
File metadata and controls
42 lines (41 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
class Brick extends JLabel {
Color color[] = {Color.RED, Color.GREEN, Color.blue, Color.orange, Color.yellow};
Random random = new Random();
Position tr = null;
Position tl = null;
Position br = null;
Position bl = null;
private class Position{
int x =0;
int y = 0;
Position(int x, int y){
this.x = x;
this.y = y;
}
}
Brick(int x, int y, int width, int height){
setBounds(x, y, width, height);
setOpaque(true);
setBackground(color[random.nextInt(5)]);
tl = new Position(x, y);
tr = new Position(x+width, y);
bl = new Position(x, y+height);
br = new Position(x+width, y+height);
}
public boolean overlap(Brick brick){
return (check(brick.getX(), brick.getY()) || check(brick.getX()+brick.getWidth(), brick.getY()) || check(brick.getX(), brick.getY()+brick.getHeight()) || check(brick.getX()+brick.getWidth(), brick.getY()+brick.getHeight()));
}
private boolean check(int bx, int by){
if(bx >= tl.x && bx <= tr.x && by >= tl.y && by <= bl.y){
return true;
}
return false;
}
}