-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudokuPanel.java
143 lines (125 loc) · 4.35 KB
/
SudokuPanel.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package sudoku;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.font.FontRenderContext;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.event.MouseInputAdapter;
@SuppressWarnings("serial")
public class SudokuPanel extends JPanel {
private SudokuPuzzle puzzle;
private int currentlySelectedCol;
private int currentlySelectedRow;
private int usedWidth;
private int usedHeight;
private int fontSize;
public SudokuPanel() {
this.setPreferredSize(new Dimension(540,450));
this.addMouseListener(new SudokuPanelMouseAdapter());
this.puzzle = new SudokuGenerator().generateRandomSudoku(SudokuPuzzleType.NINEBYNINE);
currentlySelectedCol = -1;
currentlySelectedRow = -1;
usedWidth = 0;
usedHeight = 0;
fontSize = 26;
}
public SudokuPanel(SudokuPuzzle puzzle) {
this.setPreferredSize(new Dimension(540,450));
this.addMouseListener(new SudokuPanelMouseAdapter());
this.puzzle = puzzle;
currentlySelectedCol = -1;
currentlySelectedRow = -1;
usedWidth = 0;
usedHeight = 0;
fontSize = 26;
}
public void newSudokuPuzzle(SudokuPuzzle puzzle) {
this.puzzle = puzzle;
}
public void setFontSize(int fontSize) {
this.fontSize = fontSize;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(new Color(1.0f,1.0f,1.0f));
int slotWidth = this.getWidth()/puzzle.getNumColumns();
int slotHeight = this.getHeight()/puzzle.getNumRows();
usedWidth = (this.getWidth()/puzzle.getNumColumns())*puzzle.getNumColumns();
usedHeight = (this.getHeight()/puzzle.getNumRows())*puzzle.getNumRows();
g2d.fillRect(0, 0,usedWidth,usedHeight);
g2d.setColor(new Color(0.0f,0.0f,0.0f));
for(int x = 0;x <= usedWidth;x+=slotWidth) {
if((x/slotWidth) % puzzle.getBoxWidth() == 0) {
g2d.setStroke(new BasicStroke(2));
g2d.drawLine(x, 0, x, usedHeight);
}
else {
g2d.setStroke(new BasicStroke(1));
g2d.drawLine(x, 0, x, usedHeight);
}
}
//this will draw the right most line
//g2d.drawLine(usedWidth - 1, 0, usedWidth - 1,usedHeight);
for(int y = 0;y <= usedHeight;y+=slotHeight) {
if((y/slotHeight) % puzzle.getBoxHeight() == 0) {
g2d.setStroke(new BasicStroke(2));
g2d.drawLine(0, y, usedWidth, y);
}
else {
g2d.setStroke(new BasicStroke(1));
g2d.drawLine(0, y, usedWidth, y);
}
}
//this will draw the bottom line
//g2d.drawLine(0, usedHeight - 1, usedWidth, usedHeight - 1);
Font f = new Font("Times New Roman", Font.PLAIN, fontSize);
g2d.setFont(f);
FontRenderContext fContext = g2d.getFontRenderContext();
for(int row=0;row < puzzle.getNumRows();row++) {
for(int col=0;col < puzzle.getNumColumns();col++) {
if(!puzzle.isSlotAvailable(row, col)) {
int textWidth = (int) f.getStringBounds(puzzle.getValue(row, col), fContext).getWidth();
int textHeight = (int) f.getStringBounds(puzzle.getValue(row, col), fContext).getHeight();
g2d.drawString(puzzle.getValue(row, col), (col*slotWidth)+((slotWidth/2)-(textWidth/2)), (row*slotHeight)+((slotHeight/2)+(textHeight/2)));
}
}
}
if(currentlySelectedCol != -1 && currentlySelectedRow != -1) {
g2d.setColor(new Color(0.0f,0.0f,1.0f,0.3f));
g2d.fillRect(currentlySelectedCol * slotWidth,currentlySelectedRow * slotHeight,slotWidth,slotHeight);
}
}
public void messageFromNumActionListener(String buttonValue) {
if(currentlySelectedCol != -1 && currentlySelectedRow != -1) {
puzzle.makeMove(currentlySelectedRow, currentlySelectedCol, buttonValue, true);
repaint();
}
}
public class NumActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
messageFromNumActionListener(((JButton) e.getSource()).getText());
}
}
private class SudokuPanelMouseAdapter extends MouseInputAdapter {
@Override
public void mouseClicked(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON1) {
int slotWidth = usedWidth/puzzle.getNumColumns();
int slotHeight = usedHeight/puzzle.getNumRows();
currentlySelectedRow = e.getY() / slotHeight;
currentlySelectedCol = e.getX() / slotWidth;
e.getComponent().repaint();
}
}
}
}