-
Notifications
You must be signed in to change notification settings - Fork 173
/
AlgoFrame.java
120 lines (93 loc) · 3.72 KB
/
AlgoFrame.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
113
114
115
116
117
118
119
120
import java.awt.Graphics2D;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.RenderingHints;
import java.util.*;
import javax.swing.*;
public class AlgoFrame extends JFrame{
private int canvasWidth;
private int canvasHeight;
public AlgoFrame(String title, int canvasWidth, int canvasHeight){
super(title);
this.canvasWidth = canvasWidth;
this.canvasHeight = canvasHeight;
AlgoCanvas canvas = new AlgoCanvas();
setContentPane(canvas);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public AlgoFrame(String title){
this(title, 1024, 768);
}
public int getCanvasWidth(){return canvasWidth;}
public int getCanvasHeight(){return canvasHeight;}
// data
private GameData data;
public void render(GameData data){
this.data = data;
repaint();
}
private class AlgoCanvas extends JPanel{
private ArrayList<Color> colorList;
private HashMap<Character, Color> colorMap;
public AlgoCanvas(){
// 双缓存
super(true);
colorList = new ArrayList<Color>();
colorList.add(AlgoVisHelper.Red);
colorList.add(AlgoVisHelper.Purple);
colorList.add(AlgoVisHelper.Blue);
colorList.add(AlgoVisHelper.Teal);
colorList.add(AlgoVisHelper.LightGreen);
colorList.add(AlgoVisHelper.Lime);
colorList.add(AlgoVisHelper.Amber);
colorList.add(AlgoVisHelper.DeepOrange);
colorList.add(AlgoVisHelper.Brown);
colorList.add(AlgoVisHelper.BlueGrey);
colorMap = new HashMap<Character, Color>();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
// 抗锯齿
// RenderingHints hints = new RenderingHints(
// RenderingHints.KEY_ANTIALIASING,
// RenderingHints.VALUE_ANTIALIAS_ON);
// hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
// g2d.addRenderingHints(hints);
// 具体绘制
int w = canvasWidth/data.M();
int h = canvasHeight/data.N();
Board showBoard = data.getShowBoard();
for(int i = 0 ; i < showBoard.N() ; i ++)
for(int j = 0 ; j < showBoard.M() ; j++){
char c = showBoard.getData(i, j);
if(c != Board.EMPTY){
if(!colorMap.containsKey(c)){
int sz = colorMap.size();
colorMap.put(c, colorList.get(sz));
}
Color color = colorMap.get(c);
AlgoVisHelper.setColor(g2d, color);
AlgoVisHelper.fillRectangle(g2d, j*w+2, i*h+2, w-4, h-4);
AlgoVisHelper.setColor(g2d, AlgoVisHelper.White);
String text = String.format("( %d , %d )", i, j);
AlgoVisHelper.drawText(g2d, text, j*h + h/2, i*w + w/2);
}
if( i == data.clickx && j == data.clicky) {
AlgoVisHelper.setColor(g2d, AlgoVisHelper.LightBlue);
AlgoVisHelper.setStrokeWidth(g2d, 4);
AlgoVisHelper.strokeRectangle(g2d, j * w + 2, i * h + 2, w - 4, h - 4);
}
}
}
@Override
public Dimension getPreferredSize(){
return new Dimension(canvasWidth, canvasHeight);
}
}
}