From 819ce512564c24d552745b678e0eb2e184305918 Mon Sep 17 00:00:00 2001 From: niklasBr <104077966+bretzNiklas@users.noreply.github.com> Date: Sat, 7 Jan 2023 22:32:35 +0100 Subject: [PATCH] Added mouseClick event --- Aufgabe4/Plotter.java | 45 ++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/Aufgabe4/Plotter.java b/Aufgabe4/Plotter.java index f9bedbd..eb6518b 100644 --- a/Aufgabe4/Plotter.java +++ b/Aufgabe4/Plotter.java @@ -40,28 +40,29 @@ public class Plotter { class PlotterPanel extends JPanel { + + + final int ZERO_POINT_X = 400; final int ZERO_POINT_Y = 300; ValuesToDraw valuesToDraw; + PlotterPanel(ValuesToDraw valuesToDraw) { this.valuesToDraw = valuesToDraw; - } - public Dimension getPreferredSize() { - return new Dimension(800,600); - } - - protected void paintComponent(Graphics g) { - super.paintComponent(g); - + addMouseListener(new MouseAdapter() { + public void mousePressed(MouseEvent e) { + drawNextLine(); + } + }); - g.drawLine(400, 0, 400, 600); - g.drawLine(0, 300, 800, 300); + } + private void drawNextLine() { int[] xCords = valuesToDraw.getXCords(); int[] yCords = valuesToDraw.getYCords(); @@ -80,10 +81,32 @@ class PlotterPanel extends JPanel { yCordsFromZeroPoint[i] = yCords[i] + ZERO_POINT_Y; } - + Graphics g = new Graphics() g.drawPolyline(xCordsFromZeroPoint, yCordsFromZeroPoint, 21); + } + + + + public Dimension getPreferredSize() { + return new Dimension(800,600); + } + + protected void paintComponent(Graphics g) { + super.paintComponent(g); + + + g.drawLine(400, 0, 400, 600); + g.drawLine(0, 300, 800, 300); + + int[] test = new int[]{-20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, + -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; + int[] test2 = new int[]{-20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, + -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}; + g.drawPolyline(test, test2, 21); } -- GitLab