-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVizPanel.java
More file actions
122 lines (108 loc) · 3.35 KB
/
VizPanel.java
File metadata and controls
122 lines (108 loc) · 3.35 KB
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
113
114
115
116
117
118
119
120
121
122
// Copyright 2010 owners of the AI Challenge project
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy
// of the License at http://www.apache.org/licenses/LICENSE-2.0 . Unless
// required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
//
// Author: Patrick Paskaris
//
// This class is for the Vizualizer Swing component.
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.*;
import java.awt.event.*;
public class VizPanel extends JPanel {
private Game gameCache;
private double frameCache = 0;
private JLabel loader;
private BufferedImage img;
private Graphics2D _g;
private ArrayList<Color> colors = new ArrayList<Color>();
private boolean renderInit = false;
private BufferedImage bgImage;
// HACKY FIX BECAUSE JAVA BLOWS
private int oldW = 0, oldH = 0;
// END HACKY FIX
public void addColor(Color c) {
colors.add(c);
}
// Creates the color 'theme' used for the game
private void initRenderer() {
try {
bgImage = ImageIO.read(getClass().getResource("img/space.jpg"));
} catch (IOException Err) {
bgImage = null;
}
_ginit();
renderInit = true;
}
private void _ginit() {
// Set up the graphics context.
if (img != null) {
_g.dispose();
img.flush();
}
GraphicsConfiguration gc = GraphicsEnvironment
.getLocalGraphicsEnvironment().getDefaultScreenDevice()
.getDefaultConfiguration();
img = gc.createCompatibleImage(getWidth(), getHeight());
_g = img.createGraphics();
// Turn on AA/Speed
_g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
_g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_SPEED);
_g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
}
public VizPanel() {
try {
ImageIcon _loader = new ImageIcon("img/loader.gif");
loader = new JLabel(_loader);
_loader.setImageObserver(loader);
setLayout(new BorderLayout());
add(loader, BorderLayout.CENTER);
}
catch (Exception err) {
loader = null;
}
}
// Prepare to render a game state.
public void prepare(Game game, double turn) {
if (loader != null) {
initRenderer();
remove(loader);
loader = null;
}
gameCache = game;
frameCache = Math.IEEEremainder(turn, 1.0);
}
public void paint(Graphics g){
int width = getWidth(), height = getHeight();
if (!renderInit || gameCache == null) {
if (loader != null) {
loader.paint(g);
}
} else {
if (width != oldW || height != oldH) {
_ginit();
oldW = width;
oldH = height;
}
gameCache.Render(width,
height,
frameCache,
bgImage,
colors,
(Graphics2D)_g);
g.drawImage(img, 0, 0, null);
}
}
}