diff --git a/file.jpg b/file.jpg new file mode 100644 index 00000000..bd67c959 Binary files /dev/null and b/file.jpg differ diff --git a/src/main/ArrayVisualizer.java b/src/main/ArrayVisualizer.java index f463a546..c882a76a 100644 --- a/src/main/ArrayVisualizer.java +++ b/src/main/ArrayVisualizer.java @@ -30,7 +30,7 @@ import utils.Writes; import visuals.Bars; import visuals.Circular; -import visuals.Hoops; +import visuals.CustomImage; import visuals.Mesh; import visuals.Pixels; import visuals.VisualStyles; @@ -269,7 +269,12 @@ public void run() { ArrayVisualizer.this.visualClasses = new Visual[5]; ArrayVisualizer.this.visualClasses[0] = new Bars(ArrayVisualizer.this); ArrayVisualizer.this.visualClasses[1] = new Circular(ArrayVisualizer.this); - ArrayVisualizer.this.visualClasses[2] = new Hoops(ArrayVisualizer.this); + //Image file name is set here (file will be in the main folder) + try { + ArrayVisualizer.this.visualClasses[2] = new CustomImage(ArrayVisualizer.this, "file.jpg"); + } catch(Exception e) { + e.printStackTrace(); + } ArrayVisualizer.this.visualClasses[3] = new Mesh(ArrayVisualizer.this); ArrayVisualizer.this.visualClasses[4] = new Pixels(ArrayVisualizer.this); @@ -714,4 +719,4 @@ private void drawWindows() { public static void main(String[] args) { new ArrayVisualizer(); } -} \ No newline at end of file +} diff --git a/src/prompts/ViewPrompt.java b/src/prompts/ViewPrompt.java index 92c6043d..9e817de9 100644 --- a/src/prompts/ViewPrompt.java +++ b/src/prompts/ViewPrompt.java @@ -84,7 +84,7 @@ private void initComponents() { this.disparityDots = new javax.swing.JButton(); this.spiralDots= new javax.swing.JButton(); this.rainbow = new javax.swing.JButton(); - this.hoops = new javax.swing.JButton(); + this.customImage = new javax.swing.JButton(); this.sineWave = new javax.swing.JButton(); this.waveDots = new javax.swing.JButton(); @@ -149,11 +149,11 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { } }); - hoops.setText("Hoops"); - hoops.addActionListener(new java.awt.event.ActionListener() { + customImage.setText("Custom Image"); + customImage.addActionListener(new java.awt.event.ActionListener() { @Override public void actionPerformed(java.awt.event.ActionEvent evt) { - hoopsActionPerformed(evt); + customImageActionPerformed(evt); } }); @@ -207,7 +207,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { .addComponent(dotGraph, GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE) .addComponent(triangleMesh, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(spiral, GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE) - .addComponent(hoops, GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE) + .addComponent(customImage, GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE) .addComponent(spiralDots, GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)) .addGap(18)) ); @@ -227,7 +227,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) { .addPreferredGap(ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(Alignment.BASELINE) .addComponent(colorCircle) - .addComponent(hoops)) + .addComponent(customImage)) .addPreferredGap(ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(Alignment.BASELINE) .addComponent(disparity) @@ -308,9 +308,9 @@ private void disparityActionPerformed(java.awt.event.ActionEvent evt) { UtilFrame.jButton2ResetText(); dispose(); } - private void hoopsActionPerformed(java.awt.event.ActionEvent evt) { + private void customImageActionPerformed(java.awt.event.ActionEvent evt) { setAllFieldsFalse(); - ArrayVisualizer.setVisual(VisualStyles.HOOPS); + ArrayVisualizer.setVisual(VisualStyles.IMAGE); UtilFrame.jButton2ResetText(); dispose(); } @@ -358,8 +358,8 @@ private void waveDotsActionPerformed(java.awt.event.ActionEvent evt) { private javax.swing.JButton disparity; private javax.swing.JButton disparityDots; private javax.swing.JButton rainbow; - private javax.swing.JButton hoops; + private javax.swing.JButton customImage; private javax.swing.JButton sineWave; private javax.swing.JButton waveDots; private javax.swing.JLabel jLabel1; -} \ No newline at end of file +} diff --git a/src/visuals/CustomImage.java b/src/visuals/CustomImage.java new file mode 100644 index 00000000..a6c57ca2 --- /dev/null +++ b/src/visuals/CustomImage.java @@ -0,0 +1,83 @@ +package visuals; + +import java.awt.Color; +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.io.File; +import javax.imageio.ImageIO; + +import main.ArrayVisualizer; +import templates.Visual; +import utils.Highlights; +import utils.Renderer; + +/* + * +MIT License + +Copyright (c) 2019 w0rthy + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + * + */ + +final public class CustomImage extends Visual { + private BufferedImage img; + + public CustomImage(ArrayVisualizer ArrayVisualizer, String f) throws IOException { + super(ArrayVisualizer); + this.setImage(f); + } + + public void setImage(String f) throws IOException { + this.img = ImageIO.read(new File(f)); + } + + public void drawVisual(int[] array, ArrayVisualizer ArrayVisualizer, Renderer Renderer, Highlights Highlights) { + for(int i = 0; i < ArrayVisualizer.getCurrentLength(); i++) { + int width = (int) (Renderer.getXScale() * (i + 1)) - Renderer.getOffset(); + + if(i < Highlights.getFancyFinishPosition() || Highlights.containsPosition(i)) { + if(ArrayVisualizer.analysisEnabled()) mainRender.setColor(Color.WHITE); + else mainRender.setColor(Color.BLACK); + + mainRender.fillRect(Renderer.getOffset() + 20, 0, width, ArrayVisualizer.windowHeight()); + } else { + //Cuts the image in respect to each item in the array + mainRender.drawImage( + this.img, + + Renderer.getOffset() + 20, + 0, + Renderer.getOffset() + 20 + width, + ArrayVisualizer.windowHeight(), + + (int) ((double) this.img.getWidth() / ArrayVisualizer.getCurrentLength() * array[i]), + 0, + (int) Math.ceil((double) this.img.getWidth() / ArrayVisualizer.getCurrentLength() * (array[i] + 1)), + this.img.getHeight(), + + null + ); + } + + Renderer.setOffset(Renderer.getOffset() + width); + } + } +} \ No newline at end of file diff --git a/src/visuals/Hoops.java b/src/visuals/Hoops.java deleted file mode 100644 index 9041e6e2..00000000 --- a/src/visuals/Hoops.java +++ /dev/null @@ -1,121 +0,0 @@ -package visuals; - -import java.awt.BasicStroke; -import java.awt.Color; -import java.awt.Graphics2D; - -import main.ArrayVisualizer; -import templates.Visual; -import utils.Highlights; -import utils.Renderer; - -/* - * -MIT License - -Copyright (c) 2019 w0rthy - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - * - */ - -final public class Hoops extends Visual { - public Hoops(ArrayVisualizer ArrayVisualizer) { - super(ArrayVisualizer); - } - - @SuppressWarnings("fallthrough") - public static void markHoops(int logOfLen, int index, Highlights Highlights, Graphics2D mainRender) { - switch(logOfLen) { - case 14: if(Highlights.containsPosition(index - 13)) mainRender.setColor(Color.BLACK); - case 13: if(Highlights.containsPosition(index - 12)) mainRender.setColor(Color.BLACK); - case 12: if(Highlights.containsPosition(index - 11)) mainRender.setColor(Color.BLACK); - case 11: if(Highlights.containsPosition(index - 10)) mainRender.setColor(Color.BLACK); - case 10: if(Highlights.containsPosition(index - 9)) mainRender.setColor(Color.BLACK); - case 9: if(Highlights.containsPosition(index - 8)) mainRender.setColor(Color.BLACK); - case 8: if(Highlights.containsPosition(index - 7)) mainRender.setColor(Color.BLACK); - case 7: if(Highlights.containsPosition(index - 6)) mainRender.setColor(Color.BLACK); - case 6: if(Highlights.containsPosition(index - 5)) mainRender.setColor(Color.BLACK); - case 5: if(Highlights.containsPosition(index - 4)) mainRender.setColor(Color.BLACK); - case 4: if(Highlights.containsPosition(index - 3)) mainRender.setColor(Color.BLACK); - case 3: if(Highlights.containsPosition(index - 2)) mainRender.setColor(Color.BLACK); - case 2: if(Highlights.containsPosition(index - 1)) mainRender.setColor(Color.BLACK); - default: if(Highlights.containsPosition(index)) mainRender.setColor(Color.BLACK); - } - } - - @SuppressWarnings("fallthrough") - public static void drawFancyFinishHoops(int logOfLen, int index, int position, Graphics2D mainRender) { - switch(logOfLen) { - case 14: if(index == position - 13) mainRender.setColor(Color.BLACK); - case 13: if(index == position - 12) mainRender.setColor(Color.BLACK); - case 12: if(index == position - 11) mainRender.setColor(Color.BLACK); - case 11: if(index == position - 10) mainRender.setColor(Color.BLACK); - case 10: if(index == position - 9) mainRender.setColor(Color.BLACK); - case 9: if(index == position - 8) mainRender.setColor(Color.BLACK); - case 8: if(index == position - 7) mainRender.setColor(Color.BLACK); - case 7: if(index == position - 6) mainRender.setColor(Color.BLACK); - case 6: if(index == position - 5) mainRender.setColor(Color.BLACK); - case 5: if(index == position - 4) mainRender.setColor(Color.BLACK); - case 4: if(index == position - 3) mainRender.setColor(Color.BLACK); - case 3: if(index == position - 2) mainRender.setColor(Color.BLACK); - case 2: if(index == position - 1) mainRender.setColor(Color.BLACK); - default: if(index == position) mainRender.setColor(Color.BLACK); - } - } - - //TODO: Fix scaling to ensure Hoops close at the center - //TODO: Too many rings highlighted at once!! - @Override - public void drawVisual(int[] array, ArrayVisualizer ArrayVisualizer, Renderer Renderer, Highlights Highlights) { - this.mainRender.setStroke(new BasicStroke(1.0f)); //thin strokes significantly increased performance - - //This StackOverflow thread may be related: https://stackoverflow.com/questions/47102734/performances-issue-when-drawing-dashed-line-in-java - - double diameter = 2.0; - double diamstep = Math.min(Renderer.getXScale(), Renderer.getYScale()); - - for(int i = 0; i < ArrayVisualizer.getCurrentLength(); i++) { - if(Highlights.fancyFinishActive()) { - if(i < Highlights.getFancyFinishPosition()) { - this.mainRender.setColor(Color.GREEN); - } - else this.mainRender.setColor(getIntColor(array[i], ArrayVisualizer.getCurrentLength())); - - drawFancyFinishHoops(ArrayVisualizer.getLogBaseTwoOfLength(), i, Highlights.getFancyFinishPosition(), this.mainRender); - } - else { - this.mainRender.setColor(getIntColor(array[i], ArrayVisualizer.getCurrentLength())); - } - - if(ArrayVisualizer.getCurrentLength() != 2) { - markHoops(ArrayVisualizer.getLogBaseTwoOfLength(), i, Highlights, this.mainRender); - } - - int radius = (int) (diameter / 2.0); - - this.mainRender.drawOval(ArrayVisualizer.windowHalfWidth() - radius, - ArrayVisualizer.windowHalfHeight() - radius + 12, - (int) diameter, - (int) diameter); - - diameter += diamstep; - } - } -} \ No newline at end of file diff --git a/src/visuals/VisualStyles.java b/src/visuals/VisualStyles.java index d7b8f683..21afa3f8 100644 --- a/src/visuals/VisualStyles.java +++ b/src/visuals/VisualStyles.java @@ -43,7 +43,7 @@ public void drawVisual(int[] array, ArrayVisualizer ArrayVisualizer, Renderer Re ArrayVisualizer.getVisuals()[1].drawVisual(array, ArrayVisualizer, Renderer, Highlights); } }, - HOOPS { + IMAGE { @Override public void drawVisual(int[] array, ArrayVisualizer ArrayVisualizer, Renderer Renderer, Highlights Highlights) { ArrayVisualizer.getVisuals()[2].drawVisual(array, ArrayVisualizer, Renderer, Highlights); @@ -67,4 +67,4 @@ public VisualStyles getCurrentVisual() { } public abstract void drawVisual(int[] array, ArrayVisualizer ArrayVisualizer, Renderer Renderer, Highlights Highlights); -} \ No newline at end of file +}