11package Main .Display ;
22
3+ import Main .Main ;
34import Main .Msc .ObjectHandler ;
45import Main .Objects .Collision .CircleCollider ;
56import Main .Objects .Collision .Collider ;
910import java .awt .*;
1011import java .awt .event .KeyAdapter ;
1112import java .awt .event .KeyEvent ;
12- import java .awt . event . KeyListener ;
13+ import java .util . ArrayList ;
1314
1415public class Map extends JPanel {
1516
1617 private boolean holding = false ;
1718 private KeyEvent keyEvent ;
19+ private static ArrayList <JComponent > jComponents = new ArrayList <>();
20+
1821 public Map () {
1922
20- setBackground (new Color (44 , 157 , 228 ));
23+ for (JComponent c : jComponents )
24+ {
25+ add (c );
26+
27+ }
28+ setBackground (Color .GREEN );
2129
2230 addKeyListener (new KeyAdapter () {
2331 @ Override
@@ -45,6 +53,20 @@ public void keyReleased(KeyEvent e) {
4553 }
4654 int x = 0 ;
4755
56+ public static void addJComponent (JComponent jComponent )
57+ {
58+ jComponents .add (jComponent );
59+ }
60+ public static void removeJComponent (JComponent jComponent )
61+ {
62+ jComponents .remove (jComponent );
63+
64+ }
65+
66+ public static ArrayList <JComponent > getJComponents () {
67+ return jComponents ;
68+ }
69+
4870 private void UpdateObjects ()
4971 {
5072 for (Object obj :ObjectHandler .getObjects ())
@@ -71,8 +93,21 @@ private void checkCollisions()
7193 }
7294 }
7395 }
96+ /**Check if a Jcomponent has been removed in the Jcomponents list and if so removes it in the panel**/
97+ public void UpdateSwingComponents ()
98+ {
99+ for (Component c : getComponents ())
100+ {
101+ if (!jComponents .contains (c ))
102+ {
103+ remove (c );
104+ }
105+ }
106+ }
107+
74108 public void Update ()
75109 {
110+ UpdateSwingComponents ();
76111 long start = System .nanoTime ();
77112
78113 if (holding )
@@ -87,23 +122,23 @@ public void Update()
87122 repaint ();
88123 Toolkit .getDefaultToolkit ().sync ();
89124 long end = System .nanoTime ();
90- //System.out.println((start- end)/1000000 );
125+ //System.out.println((end-start)/100000 );
91126 }
92127
93128
94129 @ Override
95130 protected void paintComponent (Graphics g ) {
96131 super .paintComponent (g );
97132
98- for ( Object animal : ObjectHandler . getObjects () )
133+ if ( Main . isPlaying )
99134 {
100- g .drawImage ((Image ) animal .Display (), (int ) animal .getSpritePosition ().getX (), (int ) animal .getSpritePosition ().getY (),null );
101- g .drawRect ((int ) ((int ) animal .getPosition ().getX ()), (int ) ((int ) animal .getPosition ().getY ()), 10 ,10 );
135+ for (Object animal : ObjectHandler .getObjects ())
136+ {
137+ g .drawImage ((Image ) animal .Display (), (int ) animal .getSpritePosition ().getX (), (int ) animal .getSpritePosition ().getY (),null );
102138
139+ }
140+ DrawColliders (g );
103141 }
104- DrawColliders (g );
105-
106-
107142 }
108143 private void DrawColliders (Graphics g )
109144 {
@@ -114,7 +149,6 @@ private void DrawColliders(Graphics g)
114149 if (c instanceof CircleCollider &&c .isVisible ())
115150 {
116151 g .drawOval ((int ) (c .getPosition ().getX ()-(c .getScale ().getX ()/2 )), (int ) (c .getPosition ().getY ()-(c .getScale ().getY ()/2 )), (int ) c .getScale ().getX (), (int ) c .getScale ().getY ());
117- g .drawRect ((int ) c .getPosition ().getX (), (int ) c .getPosition ().getY (),10 ,10 );
118152 }
119153 }
120154 }
0 commit comments