From acfff40c32981eeba4d4f8d1702d5a3beaabc381 Mon Sep 17 00:00:00 2001 From: Abhiram98 Date: Sun, 4 Aug 2024 13:45:16 -0600 Subject: [PATCH 1/5] rename gg, g2 --- .../alertintrusion/AlertIntrusion.java | 64 +++++++++---------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/plugins-dev/alert-intrusion/src/java/pt/lsts/neptus/plugins/alertintrusion/AlertIntrusion.java b/plugins-dev/alert-intrusion/src/java/pt/lsts/neptus/plugins/alertintrusion/AlertIntrusion.java index 1eb567a0eb..15f9a04ab2 100644 --- a/plugins-dev/alert-intrusion/src/java/pt/lsts/neptus/plugins/alertintrusion/AlertIntrusion.java +++ b/plugins-dev/alert-intrusion/src/java/pt/lsts/neptus/plugins/alertintrusion/AlertIntrusion.java @@ -301,17 +301,17 @@ public void paint(Graphics2D g, StateRenderer2D renderer) { boolean recreateImage = layerPainter.paintPhaseStartTestRecreateImageAndRecreate(g, renderer); if (recreateImage) { - Graphics2D g2 = layerPainter.getImageGraphics(); + Graphics2D imageGraphics = layerPainter.getImageGraphics(); // Paint what you want in the graphics if (!collisionsTree.isEmpty() && collisionsTree.get(lastMainVehicle) != null && !collisionsTree.get(lastMainVehicle).isEmpty()) { - Graphics2D gg = (Graphics2D) g2.create(); - gg.translate(20, 100); - boolean res = !gg.drawImage(colregImage, null, null); + Graphics2D graphicsContext = (Graphics2D) imageGraphics.create(); + graphicsContext.translate(20, 100); + boolean res = !graphicsContext.drawImage(colregImage, null, null); if (res) { askForLaterRepaint.compareAndSet(false, true); } - gg.dispose(); + graphicsContext.dispose(); int collisionSize = collisionsTree.get(lastMainVehicle).size(); infoLabel.setText("# " + collisionSize); @@ -320,14 +320,14 @@ public void paint(Graphics2D g, StateRenderer2D renderer) { infoLabel.setBackground(blackTransparentColor); infoLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); infoLabel.setOpaque(true); - gg = (Graphics2D) g2.create(); - gg.translate(20 + 50 + 5 + 2, 100 + 25); - FontMetrics fontMetrics = gg.getFontMetrics(); - Rectangle2D rectBounds = fontMetrics.getStringBounds(infoLabel.getText(), gg); + graphicsContext = (Graphics2D) imageGraphics.create(); + graphicsContext.translate(20 + 50 + 5 + 2, 100 + 25); + FontMetrics fontMetrics = graphicsContext.getFontMetrics(); + Rectangle2D rectBounds = fontMetrics.getStringBounds(infoLabel.getText(), graphicsContext); infoLabel.setBounds(0, 0, (int) rectBounds.getWidth() + 10, (int) rectBounds.getHeight() + 10); infoLabel.setForeground(Color.white); - infoLabel.paint(gg); - gg.dispose(); + infoLabel.paint(graphicsContext); + graphicsContext.dispose(); } AtomicReference shipClosest = new AtomicReference<>(null); @@ -352,14 +352,14 @@ public void paint(Graphics2D g, StateRenderer2D renderer) { } } if (loc != null) { - Graphics2D gg = (Graphics2D) g2.create(); + Graphics2D graphicsContext = (Graphics2D) imageGraphics.create(); Point2D spos = renderer.getScreenPosition(loc); - gg.translate(spos.getX() - 20 - 8, spos.getY()); - boolean res = !gg.drawImage(colregImageSmall, null, null); + graphicsContext.translate(spos.getX() - 20 - 8, spos.getY()); + boolean res = !graphicsContext.drawImage(colregImageSmall, null, null); if (res) { askForLaterRepaint.compareAndSet(false, true); } - gg.dispose(); + graphicsContext.dispose(); } } @@ -371,15 +371,15 @@ public void paint(Graphics2D g, StateRenderer2D renderer) { infoLabel.setText("" + line1 + "
" + line2 + ""); infoLabel.setForeground(Color.white); infoLabel.setHorizontalAlignment(JLabel.LEFT); - Graphics2D gg = (Graphics2D) g2.create(); - gg.translate(20, 100 + 50 + 5 + 5); - FontMetrics fontMetrics = gg.getFontMetrics(); - Rectangle2D rectBounds = fontMetrics.getStringBounds(line1, gg); - Rectangle2D rectBounds2 = fontMetrics.getStringBounds(line2, gg); + Graphics2D graphicsContext = (Graphics2D) imageGraphics.create(); + graphicsContext.translate(20, 100 + 50 + 5 + 5); + FontMetrics fontMetrics = graphicsContext.getFontMetrics(); + Rectangle2D rectBounds = fontMetrics.getStringBounds(line1, graphicsContext); + Rectangle2D rectBounds2 = fontMetrics.getStringBounds(line2, graphicsContext); infoLabel.setBounds(0, 0, (int) Math.max(rectBounds.getWidth(), rectBounds2.getWidth()) + 10, (int) (rectBounds.getHeight() + rectBounds2.getHeight() + 10)); - infoLabel.paint(gg); - gg.dispose(); + infoLabel.paint(graphicsContext); + graphicsContext.dispose(); } Point2D indicatorPoint = new Point2D.Double(20 + 25, 100 + 25); @@ -420,19 +420,19 @@ public void paint(Graphics2D g, StateRenderer2D renderer) { }); for (int i = 0; i < lookAngle.length; i++) { if (lookAngle[i]) { - Graphics2D gg = (Graphics2D) g2.create(); - gg.translate(20 + 25, 100 + 25); - gg.rotate(Math.PI / 2 + Math.PI / 4 * (2 * i + 1)); - gg.translate( -7,-25 - 2); + Graphics2D graphicsContext = (Graphics2D) imageGraphics.create(); + graphicsContext.translate(20 + 25, 100 + 25); + graphicsContext.rotate(Math.PI / 2 + Math.PI / 4 * (2 * i + 1)); + graphicsContext.translate( -7,-25 - 2); if (mainlookAngle.get() == i) { - gg.setColor(shapeHighColor); + graphicsContext.setColor(shapeHighColor); } else { - gg.setColor(shapeColor); + graphicsContext.setColor(shapeColor); } - gg.fill(shapeArrow); - gg.setColor(Color.black); - gg.draw(shapeArrow); - gg.dispose(); + graphicsContext.fill(shapeArrow); + graphicsContext.setColor(Color.black); + graphicsContext.draw(shapeArrow); + graphicsContext.dispose(); } } } From d76be94c193598a93107b3f9d81c1f896025171f Mon Sep 17 00:00:00 2001 From: Abhiram98 Date: Sun, 4 Aug 2024 13:47:04 -0600 Subject: [PATCH 2/5] split long method 'paint' rename method --- .../alertintrusion/AlertIntrusion.java | 142 ++++++++++-------- 1 file changed, 76 insertions(+), 66 deletions(-) diff --git a/plugins-dev/alert-intrusion/src/java/pt/lsts/neptus/plugins/alertintrusion/AlertIntrusion.java b/plugins-dev/alert-intrusion/src/java/pt/lsts/neptus/plugins/alertintrusion/AlertIntrusion.java index 15f9a04ab2..c00e374cf3 100644 --- a/plugins-dev/alert-intrusion/src/java/pt/lsts/neptus/plugins/alertintrusion/AlertIntrusion.java +++ b/plugins-dev/alert-intrusion/src/java/pt/lsts/neptus/plugins/alertintrusion/AlertIntrusion.java @@ -313,74 +313,10 @@ public void paint(Graphics2D g, StateRenderer2D renderer) { } graphicsContext.dispose(); - int collisionSize = collisionsTree.get(lastMainVehicle).size(); - infoLabel.setText("# " + collisionSize); - infoLabel.setHorizontalTextPosition(JLabel.CENTER); - infoLabel.setHorizontalAlignment(JLabel.CENTER); - infoLabel.setBackground(blackTransparentColor); - infoLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); - infoLabel.setOpaque(true); - graphicsContext = (Graphics2D) imageGraphics.create(); - graphicsContext.translate(20 + 50 + 5 + 2, 100 + 25); - FontMetrics fontMetrics = graphicsContext.getFontMetrics(); - Rectangle2D rectBounds = fontMetrics.getStringBounds(infoLabel.getText(), graphicsContext); - infoLabel.setBounds(0, 0, (int) rectBounds.getWidth() + 10, (int) rectBounds.getHeight() + 10); - infoLabel.setForeground(Color.white); - infoLabel.paint(graphicsContext); - graphicsContext.dispose(); + createInfoLabel(imageGraphics); } - AtomicReference shipClosest = new AtomicReference<>(null); - AtomicDouble distanceClosest = new AtomicDouble(Double.MAX_VALUE); - AtomicReference timeClosest = new AtomicReference<>(null); - collisionsTree.get(lastMainVehicle).forEach((time, pair) -> { - String sysName = pair.first(); - double distance = pair.second(); - if (distance < distanceClosest.get()) { - shipClosest.set(sysName); - distanceClosest.set(distance); - timeClosest.set(time); - - LocationType loc = null; - ImcSystem sys = ImcSystemsHolder.lookupSystemByName(sysName); - if (sys != null) { - loc = sys.getLocation().getNewAbsoluteLatLonDepth(); - } else { - ExternalSystem esys = ExternalSystemsHolder.lookupSystem(sysName); - if (esys != null) { - loc = esys.getLocation().getNewAbsoluteLatLonDepth(); - } - } - if (loc != null) { - Graphics2D graphicsContext = (Graphics2D) imageGraphics.create(); - Point2D spos = renderer.getScreenPosition(loc); - graphicsContext.translate(spos.getX() - 20 - 8, spos.getY()); - boolean res = !graphicsContext.drawImage(colregImageSmall, null, null); - if (res) { - askForLaterRepaint.compareAndSet(false, true); - } - graphicsContext.dispose(); - } - - } - //Point2D pt = renderer.getScreenPosition(loc); - }); - if (shipClosest.get() != null) { - String line1 = shipClosest.get(); - String line2 = Math.round(distanceClosest.get()) + "m at " + sdf.format(timeClosest.get()); - infoLabel.setText("" + line1 + "
" + line2 + ""); - infoLabel.setForeground(Color.white); - infoLabel.setHorizontalAlignment(JLabel.LEFT); - Graphics2D graphicsContext = (Graphics2D) imageGraphics.create(); - graphicsContext.translate(20, 100 + 50 + 5 + 5); - FontMetrics fontMetrics = graphicsContext.getFontMetrics(); - Rectangle2D rectBounds = fontMetrics.getStringBounds(line1, graphicsContext); - Rectangle2D rectBounds2 = fontMetrics.getStringBounds(line2, graphicsContext); - infoLabel.setBounds(0, 0, (int) Math.max(rectBounds.getWidth(), rectBounds2.getWidth()) + 10, - (int) (rectBounds.getHeight() + rectBounds2.getHeight() + 10)); - infoLabel.paint(graphicsContext); - graphicsContext.dispose(); - } + AtomicReference shipClosest = detectAndRenderClosestCollision(renderer, sdf, askForLaterRepaint, imageGraphics); Point2D indicatorPoint = new Point2D.Double(20 + 25, 100 + 25); AtomicReference mainlookAngle = new AtomicReference<>((short) 0); @@ -445,6 +381,80 @@ public void paint(Graphics2D g, StateRenderer2D renderer) { } } + private void createInfoLabel(Graphics2D imageGraphics) { + Graphics2D graphicsContext; + int collisionSize = collisionsTree.get(lastMainVehicle).size(); + infoLabel.setText("# " + collisionSize); + infoLabel.setHorizontalTextPosition(JLabel.CENTER); + infoLabel.setHorizontalAlignment(JLabel.CENTER); + infoLabel.setBackground(blackTransparentColor); + infoLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); + infoLabel.setOpaque(true); + graphicsContext = (Graphics2D) imageGraphics.create(); + graphicsContext.translate(20 + 50 + 5 + 2, 100 + 25); + FontMetrics fontMetrics = graphicsContext.getFontMetrics(); + Rectangle2D rectBounds = fontMetrics.getStringBounds(infoLabel.getText(), graphicsContext); + infoLabel.setBounds(0, 0, (int) rectBounds.getWidth() + 10, (int) rectBounds.getHeight() + 10); + infoLabel.setForeground(Color.white); + infoLabel.paint(graphicsContext); + graphicsContext.dispose(); + } + + private AtomicReference detectAndRenderClosestCollision(StateRenderer2D renderer, SimpleDateFormat sdf, AtomicBoolean askForLaterRepaint, Graphics2D imageGraphics) { + AtomicReference shipClosest = new AtomicReference<>(null); + AtomicDouble distanceClosest = new AtomicDouble(Double.MAX_VALUE); + AtomicReference timeClosest = new AtomicReference<>(null); + collisionsTree.get(lastMainVehicle).forEach((time, pair) -> { + String sysName = pair.first(); + double distance = pair.second(); + if (distance < distanceClosest.get()) { + shipClosest.set(sysName); + distanceClosest.set(distance); + timeClosest.set(time); + + LocationType loc = null; + ImcSystem sys = ImcSystemsHolder.lookupSystemByName(sysName); + if (sys != null) { + loc = sys.getLocation().getNewAbsoluteLatLonDepth(); + } else { + ExternalSystem esys = ExternalSystemsHolder.lookupSystem(sysName); + if (esys != null) { + loc = esys.getLocation().getNewAbsoluteLatLonDepth(); + } + } + if (loc != null) { + Graphics2D graphicsContext = (Graphics2D) imageGraphics.create(); + Point2D spos = renderer.getScreenPosition(loc); + graphicsContext.translate(spos.getX() - 20 - 8, spos.getY()); + boolean res = !graphicsContext.drawImage(colregImageSmall, null, null); + if (res) { + askForLaterRepaint.compareAndSet(false, true); + } + graphicsContext.dispose(); + } + + } + //Point2D pt = renderer.getScreenPosition(loc); + }); + if (shipClosest.get() != null) { + String line1 = shipClosest.get(); + String line2 = Math.round(distanceClosest.get()) + "m at " + sdf.format(timeClosest.get()); + infoLabel.setText("" + line1 + "
" + line2 + ""); + infoLabel.setForeground(Color.white); + infoLabel.setHorizontalAlignment(JLabel.LEFT); + Graphics2D graphicsContext = (Graphics2D) imageGraphics.create(); + graphicsContext.translate(20, 100 + 50 + 5 + 5); + FontMetrics fontMetrics = graphicsContext.getFontMetrics(); + Rectangle2D rectBounds = fontMetrics.getStringBounds(line1, graphicsContext); + Rectangle2D rectBounds2 = fontMetrics.getStringBounds(line2, graphicsContext); + infoLabel.setBounds(0, 0, (int) Math.max(rectBounds.getWidth(), rectBounds2.getWidth()) + 10, + (int) (rectBounds.getHeight() + rectBounds2.getHeight() + 10)); + infoLabel.paint(graphicsContext); + graphicsContext.dispose(); + } + return shipClosest; + } + private Pair calculateAngleToRotate(Point2D indicatorPoint, Point2D pointShipClosest) { double angleRad = AngleUtils.nomalizeAngleRadsPi(AngleUtils.calcAngle( indicatorPoint.getY(), indicatorPoint.getX(), From f00bc28e2ead759e22f19948adf127eceaa3037b Mon Sep 17 00:00:00 2001 From: Abhiram98 Date: Sun, 4 Aug 2024 13:47:55 -0600 Subject: [PATCH 3/5] convert for loop to foreach --- .../alertintrusion/AlertIntrusion.java | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/plugins-dev/alert-intrusion/src/java/pt/lsts/neptus/plugins/alertintrusion/AlertIntrusion.java b/plugins-dev/alert-intrusion/src/java/pt/lsts/neptus/plugins/alertintrusion/AlertIntrusion.java index c00e374cf3..4555b02c6b 100644 --- a/plugins-dev/alert-intrusion/src/java/pt/lsts/neptus/plugins/alertintrusion/AlertIntrusion.java +++ b/plugins-dev/alert-intrusion/src/java/pt/lsts/neptus/plugins/alertintrusion/AlertIntrusion.java @@ -73,6 +73,7 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.IntStream; @PluginDescription(name = "Alert Intrusion", icon = "pt/lsts/neptus/plugins/alertintrusion/colreg.png", description = "Alert Intrusion", category = PluginDescription.CATEGORY.INTERFACE, version = "0.1") @@ -354,23 +355,22 @@ public void paint(Graphics2D g, StateRenderer2D renderer) { } } }); - for (int i = 0; i < lookAngle.length; i++) { - if (lookAngle[i]) { - Graphics2D graphicsContext = (Graphics2D) imageGraphics.create(); - graphicsContext.translate(20 + 25, 100 + 25); - graphicsContext.rotate(Math.PI / 2 + Math.PI / 4 * (2 * i + 1)); - graphicsContext.translate( -7,-25 - 2); - if (mainlookAngle.get() == i) { - graphicsContext.setColor(shapeHighColor); - } else { - graphicsContext.setColor(shapeColor); - } - graphicsContext.fill(shapeArrow); - graphicsContext.setColor(Color.black); - graphicsContext.draw(shapeArrow); - graphicsContext.dispose(); + IntStream.range(0, lookAngle.length).filter(i -> lookAngle[i]).forEach(i -> { + Graphics2D graphicsContext = (Graphics2D) imageGraphics.create(); + graphicsContext.translate(20 + 25, 100 + 25); + graphicsContext.rotate(Math.PI / 2 + Math.PI / 4 * (2 * i + 1)); + graphicsContext.translate(-7, -25 - 2); + if (mainlookAngle.get() == i) { + graphicsContext.setColor(shapeHighColor); } - } + else { + graphicsContext.setColor(shapeColor); + } + graphicsContext.fill(shapeArrow); + graphicsContext.setColor(Color.black); + graphicsContext.draw(shapeArrow); + graphicsContext.dispose(); + }); } layerPainter.paintPhaseEndFinishImageRecreateAndPaintImageCacheToRenderer(g, renderer); From cbdb07f18ce12b8fb63171ae68e9dc21be15875c Mon Sep 17 00:00:00 2001 From: Abhiram98 Date: Sun, 4 Aug 2024 14:05:29 -0600 Subject: [PATCH 4/5] use ternary operator --- .../lsts/neptus/plugins/alertintrusion/AlertIntrusion.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/plugins-dev/alert-intrusion/src/java/pt/lsts/neptus/plugins/alertintrusion/AlertIntrusion.java b/plugins-dev/alert-intrusion/src/java/pt/lsts/neptus/plugins/alertintrusion/AlertIntrusion.java index 4555b02c6b..23872d4243 100644 --- a/plugins-dev/alert-intrusion/src/java/pt/lsts/neptus/plugins/alertintrusion/AlertIntrusion.java +++ b/plugins-dev/alert-intrusion/src/java/pt/lsts/neptus/plugins/alertintrusion/AlertIntrusion.java @@ -360,12 +360,7 @@ public void paint(Graphics2D g, StateRenderer2D renderer) { graphicsContext.translate(20 + 25, 100 + 25); graphicsContext.rotate(Math.PI / 2 + Math.PI / 4 * (2 * i + 1)); graphicsContext.translate(-7, -25 - 2); - if (mainlookAngle.get() == i) { - graphicsContext.setColor(shapeHighColor); - } - else { - graphicsContext.setColor(shapeColor); - } + graphicsContext.setColor(mainlookAngle.get() == i ? shapeHighColor : shapeColor); graphicsContext.fill(shapeArrow); graphicsContext.setColor(Color.black); graphicsContext.draw(shapeArrow); From 4142aeeb1800f728da4fab70efb84e7edab10efc Mon Sep 17 00:00:00 2001 From: Abhiram98 Date: Sun, 4 Aug 2024 14:05:59 -0600 Subject: [PATCH 5/5] rename askForLaterRepaint -> repaintRequest --- .../neptus/plugins/alertintrusion/AlertIntrusion.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins-dev/alert-intrusion/src/java/pt/lsts/neptus/plugins/alertintrusion/AlertIntrusion.java b/plugins-dev/alert-intrusion/src/java/pt/lsts/neptus/plugins/alertintrusion/AlertIntrusion.java index 23872d4243..f13467358c 100644 --- a/plugins-dev/alert-intrusion/src/java/pt/lsts/neptus/plugins/alertintrusion/AlertIntrusion.java +++ b/plugins-dev/alert-intrusion/src/java/pt/lsts/neptus/plugins/alertintrusion/AlertIntrusion.java @@ -298,7 +298,7 @@ public void paint(Graphics2D g, StateRenderer2D renderer) { SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); - AtomicBoolean askForLaterRepaint = new AtomicBoolean(false); + AtomicBoolean repaintRequest = new AtomicBoolean(false); boolean recreateImage = layerPainter.paintPhaseStartTestRecreateImageAndRecreate(g, renderer); if (recreateImage) { @@ -310,14 +310,14 @@ public void paint(Graphics2D g, StateRenderer2D renderer) { graphicsContext.translate(20, 100); boolean res = !graphicsContext.drawImage(colregImage, null, null); if (res) { - askForLaterRepaint.compareAndSet(false, true); + repaintRequest.compareAndSet(false, true); } graphicsContext.dispose(); createInfoLabel(imageGraphics); } - AtomicReference shipClosest = detectAndRenderClosestCollision(renderer, sdf, askForLaterRepaint, imageGraphics); + AtomicReference shipClosest = detectAndRenderClosestCollision(renderer, sdf, repaintRequest, imageGraphics); Point2D indicatorPoint = new Point2D.Double(20 + 25, 100 + 25); AtomicReference mainlookAngle = new AtomicReference<>((short) 0); @@ -369,7 +369,7 @@ public void paint(Graphics2D g, StateRenderer2D renderer) { } layerPainter.paintPhaseEndFinishImageRecreateAndPaintImageCacheToRenderer(g, renderer); - if (askForLaterRepaint.get()) { + if (repaintRequest.get()) { layerPainter.triggerImageRebuild(); renderer.invalidate(); renderer.repaint(10);