Есть такой код:
CiViL.java(основной):
Java |
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
| package drmgc.CiViL.client;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public final class CiViL extends Canvas implements Runnable {
public final static String VERSION = "InDev";
private ContentLoader loader__;
private Sprite spriteHero__;
private int heroPosX = 0;
private int heroPosY = 0;
protected void init() {
loader__ = new ContentLoader("assets/");
spriteHero__ = loader__.loadSprite("img/building_barrack.x2.png");
}
protected void update(long _delta) {
if(KeyInputMan.kbArrowLeft)
heroPosX--;
else if(KeyInputMan.kbArrowRight)
heroPosX++;
if(KeyInputMan.kbArrowDown)
heroPosY--;
else if(KeyInputMan.kbArrowUp)
heroPosY++;
}
public void draw(Graphics _gfx) {
spriteHero__.draw(_gfx, heroPosX, heroPosY);
}
private BufferStrategy bs__;
private Graphics gfx__;
protected void render() {
this.bs__ = getBufferStrategy();
if(this.bs__ == null) {
createBufferStrategy(2);
requestFocus();
return;
}
this.gfx__ = this.bs__.getDrawGraphics();
this.gfx__.setColor(Color.black);
this.gfx__.fillRect(0, 0, getWidth(), getHeight());
this.draw(this.gfx__);
this.gfx__.dispose();
this.bs__.show();
}
private boolean running__ = true;
@Override
public void run() {
long _lastTime_ = System.currentTimeMillis();
long _delta_;
init();
while(running__) {
_delta_ = System.currentTimeMillis() - _lastTime_;
_lastTime_ = System.currentTimeMillis();
render();
update(_delta_);
}
}
public void start() {
running__ = true;
new Thread(this).start();
}
public static final int WIDTH = 1024;
public static final int HEIGHT = 576;
public static final String TITLE = "CiViL " + VERSION;
public static void main(String[] _args) {
System.out.println("CiViL " + VERSION);
CiViL _game_ = new CiViL();
_game_.setPreferredSize(new Dimension(WIDTH, HEIGHT));
JFrame _window_ = new JFrame(TITLE);
_window_.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
_window_.setLayout(new BorderLayout());
_window_.add(_game_, BorderLayout.CENTER);
_window_.addKeyListener(new KeyInputMan());
_window_.pack();
_window_.setResizable(false);
_window_.setLocation((EnvConfig.SCREEN.width / 2) - (WIDTH / 2),
(EnvConfig.SCREEN.height / 2) - (HEIGHT / 2));
_window_.setVisible(true);
_game_.start();
while(true);
}
} |
|
B KeyInputMan.java:
Java |
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
| package drmgc.CiViL.client;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class KeyInputMan extends KeyAdapter {
public static boolean kbArrowLeft = false;
public static boolean kbArrowRight = false;
public static boolean kbArrowUp = false;
public static boolean kbArrowDown = false;
@Override
public void keyPressed(KeyEvent _event) {
if(_event.getKeyCode() == KeyEvent.VK_LEFT)
kbArrowLeft = true;
if(_event.getKeyCode() == KeyEvent.VK_RIGHT)
kbArrowRight = true;
if(_event.getKeyCode() == KeyEvent.VK_UP)
kbArrowUp = true;
if(_event.getKeyCode() == KeyEvent.VK_DOWN)
kbArrowDown = true;
System.out.println("Key pressed");
}
@Override
public void keyReleased(KeyEvent _event) {
if(_event.getKeyCode() == KeyEvent.VK_LEFT)
kbArrowLeft = false;
if(_event.getKeyCode() == KeyEvent.VK_RIGHT)
kbArrowRight = false;
if(_event.getKeyCode() == KeyEvent.VK_UP)
kbArrowUp = false;
if(_event.getKeyCode() == KeyEvent.VK_DOWN)
kbArrowDown = false;
System.out.println("Key released");
}
} |
|
Дак вот, при запуске всё рисуется нормально (даже если сделать движений hero), но окно не отвечает, т.е. если его свернуть, оно свёрнутым и останется, а также не считывает клавиши.
PS если закоментить _game_.start();, то всё работает на ура