Skip to content

Commit

Permalink
sonarlint refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
creme332 committed Jun 8, 2024
1 parent 22a8aa4 commit ce4714d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 55 deletions.
48 changes: 21 additions & 27 deletions src/main/java/com/github/creme332/controller/CanvasController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,68 +12,69 @@ public class CanvasController {
private Canvas canvas;
private Point initialClick;

public final int MAX_CELL_SIZE = 500;
public final int MIN_CELL_SIZE = 30;
public static final int MAX_CELL_SIZE = 500;
public static final int DEFAULT_CELL_SIZE = 100;
public static final int MIN_CELL_SIZE = 30;
public static final int ZOOM_INCREMENT = 10;

public CanvasController(Canvas canvas) {
this.canvas = canvas;

canvas.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
handleCanvasResize();
}
});

canvas.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
handleMousePressed(e);
}
});

canvas.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
// System.out.format("Mouse moved: %d, %d\n", e.getX(), e.getY());
// TODO document why this method is empty
}
});

canvas.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
handleMouseDragged(e);
}
});

canvas.addMouseWheelListener(new MouseAdapter() {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
handleCanvasZoom(e);
}
});

// Add action listeners for the zoom panel buttons
canvas.getHomeButton().addActionListener(e -> handleHomeButton());
canvas.getZoomInButton().addActionListener(e -> handleZoomInButton());
canvas.getZoomOutButton().addActionListener(e -> handleZoomOutButton());
}

private void handleCanvasZoom(MouseWheelEvent e) {
// System.out.println("Mouse wheel moved " + e.getScrollAmount() + " " +
// e.getWheelRotation());

if (e.getWheelRotation() == 1) {
// zoom out
canvas.setCellSize(Math.max(MIN_CELL_SIZE, canvas.getCellSize() - 10));
canvas.setCellSize(Math.max(MIN_CELL_SIZE, canvas.getCellSize() - ZOOM_INCREMENT));

} else {
// zoom in
canvas.setCellSize(Math.min(MAX_CELL_SIZE, canvas.getCellSize() + 10));
canvas.setCellSize(Math.min(MAX_CELL_SIZE, canvas.getCellSize() + ZOOM_INCREMENT));
}

canvas.repaint();
}

private void handleMouseDragged(MouseEvent e) {
// System.out.format("Mouse dragged: %d, %d\n", e.getX(), e.getY());

if (initialClick == null) {
initialClick = e.getPoint();
return;
Expand All @@ -82,7 +83,6 @@ private void handleMouseDragged(MouseEvent e) {
Point currentDrag = e.getPoint();
int deltaX = currentDrag.x - initialClick.x;
int deltaY = currentDrag.y - initialClick.y;
// System.out.format("Mouse dragged by: %d, %d\n", deltaX, deltaY);

canvas.setYZero(canvas.getYZero() + deltaY);
canvas.setXZero(canvas.getXZero() + deltaX);
Expand All @@ -103,31 +103,25 @@ private void handleCanvasResize() {
canvas.positionZoomPanel();
canvas.positionToolbar();

System.out.println("Canvas size: " + width + " x " + height);

canvas.repaint();
}

private void handleMousePressed(MouseEvent e) {
initialClick = e.getPoint();

System.out.format("Mouse pressed: %d, %d\n", e.getX(), e.getY());
}
// Logic for Home button

private void handleHomeButton() {
canvas.setCellSize(100); // Set the zoom level to 1
canvas.setCellSize(DEFAULT_CELL_SIZE);
canvas.repaint();
}

// Logic for Zoom In button
private void handleZoomInButton() {
canvas.setCellSize(Math.min(MAX_CELL_SIZE, canvas.getCellSize() + 10));
canvas.setCellSize(Math.min(MAX_CELL_SIZE, canvas.getCellSize() + ZOOM_INCREMENT));
canvas.repaint();
}

// Logic for Zoom Out button
private void handleZoomOutButton() {
canvas.setCellSize(Math.max(MIN_CELL_SIZE, canvas.getCellSize() - 10));
canvas.setCellSize(Math.max(MIN_CELL_SIZE, canvas.getCellSize() - ZOOM_INCREMENT));
canvas.repaint();
}
}
19 changes: 6 additions & 13 deletions src/main/java/com/github/creme332/controller/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,26 @@ public class Controller {
private Toolbar toolbar;

private FrameController frameController;
private CanvasController canvasController;
private MenuBarController menuController;
private SideMenuController sideController;
private ToolBarController toolbarController;

public Controller() {
try {
toolbar = new Toolbar();
toolbarController = new ToolBarController(toolbar);
new ToolBarController(toolbar);

canvas = new Canvas(toolbar);
canvasController = new CanvasController(canvas);
new CanvasController(canvas);

frame = new Frame(canvas);
frameController = new FrameController(app, frame);

menuBar = frame.getMyMenuBar();
menuController = new MenuBarController(app, menuBar);
new MenuBarController(app, menuBar);

sideController = new SideMenuController(app, frame.getSideMenuPanel());
} catch (InvalidIconSizeException e) {
new SideMenuController(app, frame.getSideMenuPanel());
} catch (InvalidIconSizeException | InvalidPathException e) {
System.err.println("Error: " + e.getMessage());
System.exit(1);
} catch (InvalidPathException e) {
System.err.println("Error: " + e.getMessage());
System.exit(1);
} catch (Exception e) {
} catch (Exception e) {
System.err.println("Unexpected error: " + e.getMessage());
e.printStackTrace();
System.exit(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public void componentResized(ComponentEvent e) {
int menuHeight = frame.getMyMenuBar().getHeight();

int sideWidth = Math.min(400, width / 3);
System.out.println("Frame size: " + width + " x " + height);
frame.getMainPanel().setPreferredSize(new Dimension(menuWidth, height - menuHeight));
frame.setPreferredSize(new Dimension(sideWidth, height - menuHeight));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class MenuBarController {

private int activeMenuIndex = 0;

final private MatteBorder VISIBLE_BORDER = BorderFactory.createMatteBorder(
private static final MatteBorder VISIBLE_BORDER = BorderFactory.createMatteBorder(
2, 2, 2, 2, new Color(97, 97, 255));

Mode[][] modes = {
Expand Down Expand Up @@ -60,6 +60,7 @@ public MenuBarController(AppState app, MenuBar menu) {

// when user clicks on a menu
jMenu.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {

// remove border from previously active menu
Expand All @@ -82,6 +83,7 @@ public void mousePressed(MouseEvent e) {
final int menuItemIndex = j;
// add mouse listener to menu item
jMenuItem.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
// change menu icon to icon of menu item
jMenu.setIcon(jMenuItem.getIcon());
Expand All @@ -93,6 +95,7 @@ public void mousePressed(MouseEvent e) {
}

menu.getSideBarButton().addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
app.setSideBarVisibility(!app.getSideBarVisibility());
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/github/creme332/model/Mode.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public enum Mode {
DELETE("DELETE"),
MOVE_GRAPHICS_VIEW("MOVE_GRAPHICS_VIEW");

private final String Mode;
private final String mode;

Mode(String Mode) {
this.Mode = Mode;
Mode(String mode) {
this.mode = mode;
}
}
13 changes: 5 additions & 8 deletions src/main/java/com/github/creme332/view/Frame.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
Expand All @@ -15,9 +13,8 @@
* Frame of the GUI application.
*/
public class Frame extends JFrame {
// initial frame properties
private final int frameWidth = 1600; // initial width
private final int frameHeight = 1000; // initial height
private static final int INITIAL_FRAME_WIDTH = 1600;
private static final int INITIAL_FRAME_HEIGHT = 1000;

/**
* Layout used for screenContainer for swapping screens
Expand Down Expand Up @@ -47,19 +44,19 @@ public Frame(Canvas canvas) throws InvalidPathException {
this.setTitle("polydraw");

// set frame size
this.setSize(frameWidth, frameHeight);
this.setSize(INITIAL_FRAME_WIDTH, INITIAL_FRAME_HEIGHT);

// make frame resizable
this.setResizable(true);

// add close button to frame
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);

// set application icon
this.setIconImage(new IconLoader().loadIcon("/icons/icosahedron.png").getImage());

// center frame on startup if frame is not maximized
if (this.getExtendedState() != JFrame.MAXIMIZED_BOTH) {
if (this.getExtendedState() != MAXIMIZED_BOTH) {
this.setLocationRelativeTo(null);
}

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/github/creme332/view/SideMenuPanel.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.github.creme332.view;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
Expand Down

0 comments on commit ce4714d

Please sign in to comment.