1
+ package com .baeldung .imagecollision ;
2
+
3
+ import javax .imageio .ImageIO ;
4
+ import javax .swing .*;
5
+ import java .awt .*;
6
+ import java .awt .geom .Area ;
7
+ import java .awt .geom .Ellipse2D ;
8
+ import java .awt .image .BufferedImage ;
9
+ import java .io .File ;
10
+ import java .io .IOException ;
11
+
12
+ public class Game extends JPanel implements Runnable {
13
+ GameObject vader , luke ;
14
+ BufferedImage vaderImage , lukeImage ;
15
+ Thread gameThread ;
16
+
17
+ boolean collided = false ;
18
+
19
+ enum CollisionType {
20
+ BOUNDING_BOX ,
21
+ AREA_CIRCLE ,
22
+ CIRCLE_DISTANCE ,
23
+ POLYGON ,
24
+ PIXEL_PERFECT
25
+ }
26
+
27
+ CollisionType collisionType = CollisionType .PIXEL_PERFECT ;
28
+
29
+ public Game () {
30
+ try {
31
+ vaderImage = ImageIO .read (new File ("src/main/resources/images/vader.png" ));
32
+ lukeImage = ImageIO .read (new File ("src/main/resources/images/luke.png" ));
33
+
34
+ vader = new GameObject (170 , 370 , vaderImage );
35
+ luke = new GameObject (1600 , 370 , lukeImage );
36
+
37
+ } catch (IOException e ) {
38
+ System .err .println ("Error loading images" );
39
+ e .printStackTrace ();
40
+ }
41
+
42
+ setBackground (Color .white );
43
+ gameThread = new Thread (this );
44
+ gameThread .start ();
45
+ }
46
+
47
+ public void run () {
48
+ while (!collided ) {
49
+ vader .move (2 , 0 );
50
+ luke .move (-2 , 0 );
51
+
52
+ switch (collisionType ) {
53
+ case BOUNDING_BOX :
54
+ if (vader .getRectangleBounds ().intersects (luke .getRectangleBounds ())) {
55
+ collided = true ;
56
+ }
57
+ break ;
58
+
59
+ case AREA_CIRCLE :
60
+ Area ellipseAreaVader = vader .getEllipseAreaBounds ();
61
+ Area ellipseAreaLuke = luke .getEllipseAreaBounds ();
62
+ ellipseAreaVader .intersect (ellipseAreaLuke );
63
+
64
+ if (!ellipseAreaVader .isEmpty ()) {
65
+ collided = true ;
66
+ }
67
+ break ;
68
+
69
+ case CIRCLE_DISTANCE :
70
+ Ellipse2D circleVader = vader .getCircleBounds ();
71
+ Ellipse2D circleLuke = luke .getCircleBounds ();
72
+ double dx = circleVader .getCenterX () - circleLuke .getCenterX ();
73
+ double dy = circleVader .getCenterY () - circleLuke .getCenterY ();
74
+ double distance = Math .sqrt (dx * dx + dy * dy );
75
+ double radiusVader = circleVader .getWidth () / 2.0 ;
76
+ double radiusLuke = circleLuke .getWidth () / 2.0 ;
77
+
78
+ if (distance < radiusVader + radiusLuke ) {
79
+ collided = true ;
80
+ }
81
+ break ;
82
+
83
+ case POLYGON :
84
+ Area polygonAreaVader = vader .getPolygonBounds ();
85
+ Area polygonAreaLuke = luke .getPolygonBounds ();
86
+ polygonAreaVader .intersect (polygonAreaLuke );
87
+
88
+ if (!polygonAreaVader .isEmpty ()) {
89
+ collided = true ;
90
+ }
91
+ break ;
92
+
93
+ case PIXEL_PERFECT :
94
+ if (vader .collidesWith (luke )) {
95
+ collided = true ;
96
+ }
97
+ break ;
98
+ }
99
+
100
+ repaint ();
101
+
102
+ try {
103
+ Thread .sleep (16 );
104
+ } catch (InterruptedException e ) {
105
+ throw new RuntimeException (e );
106
+ }
107
+ }
108
+ }
109
+
110
+ protected void paintComponent (Graphics g ) {
111
+ super .paintComponent (g );
112
+ vader .draw (g );
113
+ luke .draw (g );
114
+
115
+ if (collided ) {
116
+ g .setColor (Color .RED );
117
+ g .setFont (new Font ("SansSerif" , Font .BOLD , 50 ));
118
+ g .drawString ("COLLISION!" , getWidth () / 2 - 100 , getHeight () / 2 );
119
+ }
120
+ }
121
+
122
+ public static void main (String [] args ) {
123
+ SwingUtilities .invokeLater (() -> {
124
+ JFrame frame = new JFrame ("Epic Duel" );
125
+ frame .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
126
+ frame .setSize (1920 , 1080 );
127
+ frame .add (new Game ());
128
+ frame .setVisible (true );
129
+ });
130
+ }
131
+ }
0 commit comments