HolonCanvas.java 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package ui.view.main;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.RenderingHints;
  7. import javax.swing.JLabel;
  8. import javax.swing.JPanel;
  9. import javax.swing.JScrollPane;
  10. import classes.Vector2d;
  11. public class HolonCanvas extends JScrollPane {
  12. Canvas canvas = new Canvas();
  13. private Vector2d pos1 = new Vector2d(100, 100);
  14. private Vector2d pos2 = new Vector2d(200, 100);
  15. private int width = 1000;
  16. private int minWidth = 100;
  17. private int maxWidth = 4000;
  18. public HolonCanvas() {
  19. canvas.setPreferredSize(new Dimension(1000,1000));
  20. this.removeMouseWheelListener(this.getMouseWheelListeners()[0]);
  21. this.addMouseWheelListener(scroll -> {
  22. int scrollValue = -scroll.getWheelRotation() * scroll.getScrollAmount();
  23. //clamp value
  24. width = Math.max(Math.min(4000, width + 50 * scrollValue), 100);
  25. canvas.setPreferredSize(new Dimension(width, width));
  26. canvas.revalidate();
  27. this.repaint();
  28. });
  29. this.setViewportView(canvas);
  30. }
  31. private void exampleBuildings() {
  32. }
  33. private class Canvas extends JPanel{
  34. Canvas(){
  35. setBackground(Color.white);
  36. add(new JLabel("HolonCanvas"));
  37. }
  38. public void paintComponent(Graphics g) {
  39. super.paintComponent(g);
  40. System.out.println("paintComponent");
  41. Graphics2D g2d = (Graphics2D) g;
  42. g2d.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING,
  43. RenderingHints.VALUE_ANTIALIAS_ON));
  44. g2d.setColor(Color.blue);
  45. double scale = ScaleValue();
  46. g2d.fillRect((int)(pos1.getX() * scale), (int)(pos1.getY() * scale), (int) (50 * scale), (int)(50 * scale));
  47. g2d.fillRect((int)(pos2.getX() * scale), (int)(pos2.getY() * scale), (int) (50 * scale), (int)(50 * scale));
  48. g2d.drawRect(0, 0, this.getPreferredSize().width, this.getPreferredSize().height);
  49. }
  50. public double ScaleValue(){
  51. return width / 1000.0;
  52. }
  53. }
  54. }