Skip to content

Commit 65d07af

Browse files
committed
simple ui
1 parent c4f9e73 commit 65d07af

6 files changed

Lines changed: 166 additions & 21 deletions

File tree

src/Main/Display/Map.java

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package Main.Display;
22

3+
import Main.Main;
34
import Main.Msc.ObjectHandler;
45
import Main.Objects.Collision.CircleCollider;
56
import Main.Objects.Collision.Collider;
@@ -9,15 +10,22 @@
910
import java.awt.*;
1011
import java.awt.event.KeyAdapter;
1112
import java.awt.event.KeyEvent;
12-
import java.awt.event.KeyListener;
13+
import java.util.ArrayList;
1314

1415
public 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
}

src/Main/Main.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
import Main.Display.Map;
44

55
import javax.swing.*;
6+
import java.awt.*;
67

78
public class Main {
89

910
public static int DELAY = 10;
11+
public static Color background;
12+
public static boolean isPlaying = false;
1013

1114
public static void Start(JFrame frame)
1215
{
1316

1417
Map m = new Map();
1518
m.setFocusable(true);
16-
19+
m.setBackground(background);
1720
frame.add(m);
1821
frame.setVisible(true);
1922

@@ -25,7 +28,10 @@ public static void Start(JFrame frame)
2528
e.printStackTrace();
2629

2730
}
28-
m.Update();
31+
if(isPlaying)
32+
{
33+
m.Update();
34+
}
2935
}
3036

3137

src/Testing/GameScreen.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Testing;
2+
3+
import Main.Display.Map;
4+
import Main.Main;
5+
6+
import javax.swing.*;
7+
import java.awt.*;
8+
import java.awt.event.ActionEvent;
9+
10+
public class GameScreen extends JPanel {
11+
12+
public GameScreen() {
13+
JPanel s = this;
14+
add(new JLabel("HELLO BOYS"));
15+
Button b = new Button("Start");
16+
b.addActionListener(new AbstractAction() {
17+
@Override
18+
public void actionPerformed(ActionEvent e) {
19+
Main.isPlaying=true;
20+
Map.removeJComponent(s);
21+
}
22+
});
23+
add(b);
24+
setBackground(new Color(0,0,0,0));
25+
}
26+
}

src/Testing/Lion.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// Source code recreated from a .class file by IntelliJ IDEA
3+
// (powered by FernFlower decompiler)
4+
//
5+
6+
package Testing;
7+
8+
import Main.Msc.Vector2;
9+
import Main.Objects.Animation;
10+
import Main.Objects.Collision.CircleCollider;
11+
import Main.Objects.Object;
12+
import java.awt.event.KeyEvent;
13+
14+
public class Lion extends Object {
15+
private float speed = 2.0F;
16+
private float gravity = 9.82F;
17+
18+
public Lion(Vector2 position) {
19+
super(position);
20+
setAnimation(new Animation());
21+
getAnimation().setPath("/spritesheet.png");
22+
setScale(new Vector2(200,200));
23+
getAnimation().loadAnimation(new Vector2[]{new Vector2(0,0),new Vector2(0,1)});
24+
CircleCollider c = new CircleCollider();
25+
c.setVisible(true);
26+
c.setParent(this);
27+
c.setScale(new Vector2(200,200));
28+
addCollider(c);
29+
30+
31+
}
32+
33+
@Override
34+
public void onCollision(Object collision) {
35+
super.onCollision(collision);
36+
//System.out.println(collision.getPosition());
37+
}
38+
39+
public void keyPressed(KeyEvent e) {
40+
super.keyPressed(e);
41+
}
42+
43+
public void keyDown(KeyEvent e) {
44+
if (e.getKeyCode() == 87) {
45+
this.setDirection(new Vector2(0.0F, -1.0F));
46+
this.setAngle(this.getDirection().getAngle());
47+
this.setPosition(this.getPosition().add(this.getDirection().multiply(this.speed)));
48+
}
49+
50+
if (e.getKeyCode() == 83) {
51+
this.setDirection(new Vector2(0.0F, 1.0F));
52+
this.setAngle(this.getDirection().getAngle());
53+
this.setPosition(this.getPosition().add(this.getDirection().multiply(this.speed)));
54+
}
55+
56+
if (e.getKeyCode() == 65) {
57+
this.setDirection(new Vector2(-1.0F, 0.0F));
58+
this.setAngle(this.getDirection().getAngle());
59+
this.setPosition(this.getPosition().add(this.getDirection().multiply(this.speed)));
60+
}
61+
62+
if (e.getKeyCode() == 68) {
63+
this.setDirection(new Vector2(1.0F, 0.0F));
64+
this.setAngle(this.getDirection().getAngle());
65+
this.setPosition(this.getPosition().add(this.getDirection().multiply(this.speed)));
66+
}
67+
68+
}
69+
70+
}

src/Testing/Main2.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
package Testing;
22

3+
import Main.Display.Map;
34
import Main.Main;
45
import Main.Msc.ObjectHandler;
56
import Main.Msc.Vector2;
67
import Testing.*;
78

89
import javax.swing.*;
10+
import java.awt.*;
911

1012
public class Main2 {
1113

1214
public static void main(String[] args)
1315
{
1416
JFrame frame = new JFrame();
1517
frame.setSize(600,600);
16-
frame.setTitle("Test");
18+
frame.setTitle("Testing window");
1719
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
20+
Map.addJComponent(new GameScreen());
21+
1822
Main.DELAY=10;
23+
Main.background = new Color(44, 157, 228);
24+
25+
ObjectHandler.AddObject(new Lion(new Vector2(200,200)));
1926
ObjectHandler.AddObject(new Player(new Vector2(200,200)));
20-
ObjectHandler.AddObject(new pillor(new Vector2(700,150)));
21-
ObjectHandler.AddObject(new pillor(new Vector2(400,500)));
27+
//ObjectHandler.AddObject(new pillor(new Vector2(700,150)));
28+
//ObjectHandler.AddObject(new pillor(new Vector2(400,500)));
2229

2330
Main.Start(frame);
2431
}
@@ -27,9 +34,9 @@ public static void restart()
2734
{
2835
//Set random y on pillars
2936

30-
ObjectHandler.getObjects().get(0).setPosition(new Vector2(200,200));
31-
ObjectHandler.getObjects().get(1).setPosition(new Vector2(700,150));
32-
ObjectHandler.getObjects().get(2).setPosition(new Vector2(700,500));
37+
//ObjectHandler.getObjects().get(0).setPosition(new Vector2(200,200));
38+
//ObjectHandler.getObjects().get(1).setPosition(new Vector2(700,150));
39+
//ObjectHandler.getObjects().get(2).setPosition(new Vector2(700,500));
3340

3441
}
3542

src/Testing/Player.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,23 @@ public Player(Vector2 position) {
2727
@Override
2828
public void onCollision(Object collision) {
2929
super.onCollision(collision);
30-
Main2.restart();
30+
//Main2.restart();
3131
}
3232

3333
@Override
3434
public void keyPressed(KeyEvent e) {
3535
super.keyPressed(e);
3636
if(e.getKeyCode()==Keys.W)
3737
{
38-
setPosition(getPosition().add(new Vector2(0,-1).multiply(70)));
38+
//setPosition(getPosition().add(new Vector2(0,-1).multiply(50)));
39+
//setAngle(50);
40+
3941
}
4042
}
4143

4244
@Override
4345
public void Update() {
4446
super.Update();
45-
setPosition(getPosition().add(new Vector2(0,1).multiply(2))); // down
47+
//setPosition(getPosition().add(new Vector2(0,1).multiply(2))); // down
4648
}
4749
}

0 commit comments

Comments
 (0)