Skip to content

Commit

Permalink
implement dynamic font size
Browse files Browse the repository at this point in the history
  • Loading branch information
creme332 committed Jul 1, 2024
1 parent a8141a2 commit bb07e30
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public void propertyChange(PropertyChangeEvent e) {
* List of property names that should result only in a canvas repaint.
*/
final Set<String> repaintProperties = Set.of("clearCanvas", "standardView", "enableGuidelines",
"cellSize", "axesVisible");
"cellSize", "axesVisible", "labelFontSize");

if (repaintProperties.contains(propertyName)) {
canvas.repaint();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public SideMenuController(AppState app, SideMenuPanel sidebar) {
// set default values in canvas settings
sidebar.getGridLinesCheckBox().setSelected(app.getCanvasModel().isGuidelinesEnabled());
sidebar.getAxesCheckBox().setSelected(app.getCanvasModel().isAxesVisible());
sidebar.getFontSizeSelector().setSelectedItem(String.format("%d", app.getCanvasModel().getLabelFontSize()));

// Initialize button listeners
initializeButtonListeners(app);
Expand Down Expand Up @@ -100,6 +101,13 @@ public void mousePressed(MouseEvent e) {
}
});

sidebar.getFontSizeSelector().addActionListener(
e -> canvasModel
.setLabelFontSize(
Integer.valueOf(
(String) sidebar.getFontSizeSelector()
.getSelectedItem())));

// Reset button
sidebar.getResetButton().addActionListener(e -> {
// Reset guidelines checkbox and model
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/com/github/creme332/model/CanvasModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ public class CanvasModel {
*/
int cellSize = Math.max(MIN_CELL_SIZE, Math.min(DEFAULT_CELL_SIZE, MAX_CELL_SIZE));

private float labelFontSizeScaleFactor = 1.4F;
/**
* Font size of labels on canvas in pixels.
*/
private int labelFontSize = 28;

// define attributes for next shape to be drawn
private LineType lineType = LineType.SOLID;
Expand Down Expand Up @@ -247,6 +250,7 @@ public void addPropertyChangeListener(PropertyChangeListener listener) {
support.addPropertyChangeListener("cellSize", listener);
support.addPropertyChangeListener("clearCanvas", listener);
support.addPropertyChangeListener("standardView", listener);
support.addPropertyChangeListener("labelFontSize", listener);
}

/**
Expand Down Expand Up @@ -283,8 +287,13 @@ public int getCellSize() {
return cellSize;
}

public float getLabelFontSizeSF() {
return labelFontSizeScaleFactor;
public void setLabelFontSize(int newFontSize) {
support.firePropertyChange("labelFontSize", this.labelFontSize, newFontSize);
labelFontSize = newFontSize;
}

public int getLabelFontSize() {
return labelFontSize;
}

public int getXZero() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/github/creme332/view/Canvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public void paintComponent(Graphics g) {
super.paintComponent(g);

Font currentFont = g.getFont();
Font newFont = currentFont.deriveFont(currentFont.getSize() * model.getLabelFontSizeSF());
Font newFont = currentFont.deriveFont((float) model.getLabelFontSize());
g.setFont(newFont);

Graphics2D g2 = (Graphics2D) g;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/github/creme332/view/SideMenuPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private JPanel createSettingsPanel() {
gbc.gridx = 1;
gbc.gridy = 2;
fontSizeSelector = new JComboBox<>(
new String[] { "12 pt", "16 pt", "18 pt", "20 pt", "24 pt", "28 pt" });
new String[] { "12", "16", "18", "20", "24", "28" });
formPanel.add(fontSizeSelector, gbc);

resetButton = new JButton("Reset");
Expand Down

0 comments on commit bb07e30

Please sign in to comment.