-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.java
104 lines (85 loc) · 3.26 KB
/
Game.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
package asteroidymodyfikacja;
//martapalka
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.Timer;
/*
CLASS: Game
OPIS: Płótno w okienku gry
Klasa abstrakcyjna, którą rozszerza klasa Asteroids
W tej klasie dochodzi do odświerzania grafiki, a także tu zaimplementowane jest zachowanie okna z płótnem(canvasem) przy rozciąganiu
*/
public abstract class Game extends Canvas implements ComponentListener, ActionListener{
protected boolean on = true;
protected int width, height;
protected BufferedImage buffer;
protected Graphics2D buffGraphics;
protected Frame frame;
//skala w dwóch wymiarach okienka
protected double scaleW = 1.0;
protected double scaleH = 1.0;
public Game(String name, int inWidth, int inHeight) {
width = inWidth;
height = inHeight;
frame = new Frame(name);
frame.add(this);
frame.setSize(width,height);
frame.setVisible(true);
frame.setResizable(true);
frame.setFocusable(true);
frame.requestFocus();
frame.addComponentListener(this);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {
Frame frame = (Frame) e.getSource();
frame.dispose();}
});
//30 Hz
Timer timer = new Timer(1000/30, this);
timer.setInitialDelay(0);
timer.start();
buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
buffGraphics = new GraphicsScallable(buffer.createGraphics(),scaleW,scaleH);
}
abstract public void paintAll(Graphics brush);
Boolean paintAllow = false;
/**
* DZIAŁANIE:Update maluje do bufora, potem na ekran; następnie czeka 1/10 sekundy zanim się powtórzy, oczywiście o ile gra jest włączona
* @param brush
*/
@Override
public void update(Graphics brush)
{
Graphics2D gS = (Graphics2D)brush;
paintAll(buffGraphics);
gS.drawImage(buffer,0,0,frame);
}
@Override
public void actionPerformed(ActionEvent e)
{
repaint();
}
@Override
public void componentResized(ComponentEvent e) {
double w = e.getComponent().getWidth();
double h = e.getComponent().getHeight();
scaleW = w/width;
scaleH = h/height;
buffer = new BufferedImage((int)w,(int)h, BufferedImage.TYPE_INT_ARGB);
buffGraphics = new GraphicsScallable(buffer.createGraphics(),scaleW,scaleH);
}
@Override
public void componentMoved(ComponentEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void componentShown(ComponentEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void componentHidden(ComponentEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}