Reputation: 690
Basically, i'm writing my own sudoku app, and for the view part (of MVC), I have a single JPanel, that overrides paintComponent and draws the sudoku cells, grids and selected cell etc. But my issue is that I need two different font sizes (e.g. one for when the user puts a note in a cell, and one for actual numbers in the cell), and somehow every time I call setFont, ALL the text that's already in any cells also change sizes..
As far as I understand, the setFont applies the font to all the text in a single component i guess ? And in my case, I just have Cell[][] table that stores the numbers and notes for each cell.
class Cell {
private Point coords;
private boolean[] notes;
private int number;
// Constructor + Accessors + Getters
}
So in paintComponent I loop over all of the cells and draw the numbers accordingly, but any call to setFont changes all the text that's already inside my grid.
class SudokuPanel extends JPanel {
private static final int SUDOKU_SIZE = 9;
private Cell[][] cells;
private static final Font BIG_FONT = new Font("Verdana", Font.BOLD, 50),
SMALL_FONT = new Font("Verdana", Font.PLAIN, 12);
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
//drawGrid(g);
//drawSelected(g);
drawCells(g);
}
private void drawCells(Graphics g) {
for (int x=0; x<SUDOKU_SIZE; x++)
for (int y=0; y<SUDOKU_SIZE; y++)
drawCell(g, cells[x][y]);
}
private void drawCell(Graphics g, Cell cell) {
if (cell.number == 0 && cell.hasNotes())
drawNotes(g, cell);
else if (cell.number != 0)
writeNumber(g, cell.number, cell);
}
private void drawNotes(Graphics g, Cell cell) {
for (int i=0; I<cell.notes.length; i++)
if (cell.notes[i])
writeNote(g, i, cell);
}
private void writeNote(Graphics g, int i, Cell cell) {
setFont(SMALL_FONT); // for small size
writeStr(g, ""+i, cell.coords.x, cell.coords.y); // definitely wrong drawing coords but not the topic of this post
}
private void writeNumber(Graphics g, int i, Cell cell) {
setFont(BIG_FONT); // for big font size
writeStr(g, ""+i, cell.coords.x, cell.coords.y);
}
}
so basically the setFont size only takes into account the last call to setFont, so should i make my Cell POJO extends JComponent ? and have each of them draw themselves by overriding their own paintComponent methods where each of them will the set a different font size accordingly ? Like so :
class Cell extends JComponent {
private static final Font BIG_FONT = new Font("Verdana", Font.BOLD, 50),
SMALL_FONT = new Font("Verdana", Font.PLAIN, 12);
//cell code
@Override
public void paintComponent(Graphics g) {
if (hasNotes()) {
setFont(SMALL_FONT);
// write notes
} else {
setFont(BIG_FONT);
// write number
}
}
}
but then i have no clue how to use the cells as components and merge them with my JPanel's paintComponent..
All in all, I basically just want some cells to have the Big number for actual values written, and others to have multiple small numbers in a single cell showing the notes for this cell. Thanks for any help involved
Upvotes: 0
Views: 142
Reputation: 9648
Don't call the component's setFont
, call the Graphics
' before you draw the text.
Upvotes: 1