package ui.view.main; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import classes.Vector2d; public class HolonCanvas extends JScrollPane { Canvas canvas = new Canvas(); private Vector2d pos1 = new Vector2d(100, 100); private Vector2d pos2 = new Vector2d(200, 100); private int width = 1000; private int minWidth = 100; private int maxWidth = 4000; public HolonCanvas() { canvas.setPreferredSize(new Dimension(1000,1000)); this.removeMouseWheelListener(this.getMouseWheelListeners()[0]); this.addMouseWheelListener(scroll -> { int scrollValue = -scroll.getWheelRotation() * scroll.getScrollAmount(); //clamp value width = Math.max(Math.min(4000, width + 50 * scrollValue), 100); canvas.setPreferredSize(new Dimension(width, width)); canvas.revalidate(); this.repaint(); }); this.setViewportView(canvas); } private void exampleBuildings() { } private class Canvas extends JPanel{ Canvas(){ setBackground(Color.white); add(new JLabel("HolonCanvas")); } public void paintComponent(Graphics g) { super.paintComponent(g); System.out.println("paintComponent"); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)); g2d.setColor(Color.blue); double scale = ScaleValue(); g2d.fillRect((int)(pos1.getX() * scale), (int)(pos1.getY() * scale), (int) (50 * scale), (int)(50 * scale)); g2d.fillRect((int)(pos2.getX() * scale), (int)(pos2.getY() * scale), (int) (50 * scale), (int)(50 * scale)); g2d.drawRect(0, 0, this.getPreferredSize().width, this.getPreferredSize().height); } public double ScaleValue(){ return width / 1000.0; } } }