|
| 1 | +import Model.Line; |
| 2 | +import Model.Point; |
| 3 | +import Model.Polygon; |
| 4 | +import Rasterize.LineRasterizer; |
| 5 | +import Rasterize.LineRasterizerDotted; |
| 6 | +import Rasterize.PolygonRasterizer; |
| 7 | +import Rasterize.Trivial.LineRasterizerDottedTrivial; |
| 8 | +import Rasterize.Trivial.LineRasterizerTrivial; |
| 9 | +import Services.RBIService; |
| 10 | + |
| 11 | +import javax.swing.*; |
| 12 | +import java.awt.*; |
| 13 | +import java.awt.event.KeyAdapter; |
| 14 | +import java.awt.event.KeyEvent; |
| 15 | +import java.awt.event.MouseAdapter; |
| 16 | +import java.awt.event.MouseEvent; |
| 17 | + |
| 18 | +public class App { |
| 19 | + //region VARIABLES |
| 20 | + private final JPanel canvas; |
| 21 | + private int x, y; |
| 22 | + private final RBIService RBIRaster; |
| 23 | + private final LineRasterizer lineRasterizer; |
| 24 | + private final LineRasterizerDotted lineRasterizerDotted; |
| 25 | + private final PolygonRasterizer polygonRasterizer; |
| 26 | + |
| 27 | + private Point startPoint; |
| 28 | + private Point currentPoint; |
| 29 | + Polygon polygon = new Polygon(); |
| 30 | + private int selection = 1; |
| 31 | + private int dotSpacing = 5; |
| 32 | + private boolean isHorizontal = true; |
| 33 | + private boolean allowAlignment = false; |
| 34 | + //endregion |
| 35 | + |
| 36 | + public App(int width, int height) { |
| 37 | + JFrame frame = new JFrame(); |
| 38 | + |
| 39 | + frame.setLayout(new BorderLayout()); |
| 40 | + frame.setTitle("UHK FIM PGRF - Task 1"); |
| 41 | + frame.setResizable(true); |
| 42 | + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); |
| 43 | + |
| 44 | + RBIRaster = new RBIService(width, height); |
| 45 | + lineRasterizer = new LineRasterizerTrivial(RBIRaster); |
| 46 | + lineRasterizerDotted = new LineRasterizerDottedTrivial(RBIRaster); |
| 47 | + polygonRasterizer = new PolygonRasterizer(lineRasterizer); |
| 48 | + |
| 49 | + canvas = new JPanel() { |
| 50 | + private static final long serialVersionUID = 1L; |
| 51 | + |
| 52 | + @Override |
| 53 | + public void paintComponent(Graphics g) { |
| 54 | + super.paintComponent(g); |
| 55 | + present(g); |
| 56 | + } |
| 57 | + }; |
| 58 | + canvas.setPreferredSize(new Dimension(width, height)); |
| 59 | + |
| 60 | + frame.add(canvas, BorderLayout.CENTER); |
| 61 | + frame.pack(); |
| 62 | + frame.setVisible(true); |
| 63 | + |
| 64 | + canvas.requestFocus(); |
| 65 | + canvas.requestFocusInWindow(); |
| 66 | + |
| 67 | + canvas.addKeyListener(new KeyAdapter() { |
| 68 | + @Override |
| 69 | + public void keyPressed(KeyEvent e) { |
| 70 | + if (e.getKeyCode() == KeyEvent.VK_C) { |
| 71 | + clearEverything(); |
| 72 | + allowAlignment = false; |
| 73 | + } else if (e.getKeyCode() == KeyEvent.VK_Z) { |
| 74 | + selection = 1; |
| 75 | + } else if (e.getKeyCode() == KeyEvent.VK_X) { |
| 76 | + selection = 2; |
| 77 | + } else if (e.getKeyCode() == KeyEvent.VK_A) { |
| 78 | + clearEverything(); |
| 79 | + selection = 3; |
| 80 | + allowAlignment = false; |
| 81 | + } else if (e.getKeyCode() == KeyEvent.VK_SHIFT) { |
| 82 | + allowAlignment = true; |
| 83 | + isHorizontal = !isHorizontal; |
| 84 | + } |
| 85 | + |
| 86 | + if (e.getKeyCode() == KeyEvent.VK_P) { |
| 87 | + dotSpacing += 5; |
| 88 | + } |
| 89 | + if (e.getKeyCode() == KeyEvent.VK_O) { |
| 90 | + if (dotSpacing >= 10) dotSpacing -= 5; |
| 91 | + } |
| 92 | + } |
| 93 | + }); |
| 94 | + canvas.addMouseListener(new MouseAdapter() { |
| 95 | + @Override |
| 96 | + public void mousePressed(MouseEvent e) { |
| 97 | + if (selection == 1 || selection == 2) { |
| 98 | + startPoint = new Point(e.getX(), e.getY()); |
| 99 | + currentPoint = startPoint; |
| 100 | + } else if (selection == 3) { |
| 101 | + Point point = new Point(e.getX(), e.getY()); |
| 102 | + polygon.addPoint(point); |
| 103 | + polygonRasterizer.rasterize(polygon); |
| 104 | + canvas.repaint(); |
| 105 | + } |
| 106 | + |
| 107 | + } |
| 108 | + }); |
| 109 | + canvas.addMouseMotionListener(new MouseAdapter() { |
| 110 | + @Override |
| 111 | + public void mouseDragged(MouseEvent e) { |
| 112 | + if (selection == 1 || selection == 2) { |
| 113 | + clear(0x000000); |
| 114 | + currentPoint = new Point(e.getX(), e.getY()); |
| 115 | + Line line = new Line(startPoint, currentPoint, 0xff0000); |
| 116 | + if (selection == 1) { |
| 117 | + if (allowAlignment) { |
| 118 | + lineRasterizer.rasterize(line, isHorizontal); |
| 119 | + } else lineRasterizer.rasterize(line); |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + if (selection == 2) { |
| 124 | + if (allowAlignment) { |
| 125 | + lineRasterizerDotted.rasterize(line, dotSpacing, isHorizontal); |
| 126 | + } else lineRasterizerDotted.rasterize(line, dotSpacing); |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + canvas.repaint(); |
| 131 | + } |
| 132 | + |
| 133 | + } |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + public static void main(String[] args) { |
| 138 | + SwingUtilities.invokeLater(() -> new App(800, 600).start()); |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + public void clear(int color) { |
| 143 | + RBIRaster.setClearColor(color); |
| 144 | + RBIRaster.clear(); |
| 145 | + } |
| 146 | + |
| 147 | + public void present(Graphics graphics) { |
| 148 | + RBIRaster.repaint(graphics); |
| 149 | + } |
| 150 | + |
| 151 | + private void start() { |
| 152 | + clear(0x000000); // black |
| 153 | + canvas.repaint(); |
| 154 | + } |
| 155 | + |
| 156 | + private void clearEverything() { |
| 157 | + RBIRaster.clear(); |
| 158 | + polygon.clear(); |
| 159 | + canvas.repaint(); |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments