Links
Download Test Images (Note: In order for the program to run correctly, they must be out of the folder in the same folder as the program class.)
Source code
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
import javax.swing.*;
import java.lang.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.Timer;
class laserDrawing
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
public static void createAndShowGUI()
{
final JFrame frame = new JFrame();
frame.setTitle("Laser Drawing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BufferedImage defaultImage = new BufferedImage(660,500,BufferedImage.TYPE_INT_ARGB);
BufferedImage img = null;
final Point points[] = new Point[9];
try
{
for(int i=1;i<=9;i++)
{
img = ImageIO.read(new File("TLapse PNG 00"+i+".png"));
//System.out.println("i: " + i);
points[i-1]= findColor(img);
}
}
catch (IOException e)
{
}
BufferedImage img2 = new BufferedImage(660,500,BufferedImage.TYPE_INT_ARGB);
BufferedImage attImage = new BufferedImage(100,500,BufferedImage.TYPE_INT_ARGB);
final Panel panel = new Panel(img2,points,0);
final Attributes attPanel = new Attributes(attImage, panel);
panel.type = 0;
JLabel label = new JLabel("Paint");
frame.getContentPane().add(panel,BorderLayout.CENTER);
frame.getContentPane().add(label,BorderLayout.SOUTH);
frame.getContentPane().add(attPanel,BorderLayout.EAST);
frame.pack();
JMenu fileMenu = new JMenu("File");
JMenuItem saveItem = new JMenuItem("Save Image");
saveItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
String input = JOptionPane.showInputDialog("Enter the name of the output file: ");
File outputFile = new File(input);
try
{
javax.imageio.ImageIO.write(panel.image, "jpg", outputFile );
}
catch ( IOException e )
{
JOptionPane.showMessageDialog(frame,"Error saving file","oops!",JOptionPane.ERROR_MESSAGE );
}
}
});
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener( new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
JMenu typeMenu = new JMenu("Type");
JMenuItem paintItem = new JMenuItem("Paint");
paintItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
frame.getContentPane().removeAll();
BufferedImage img2 = new BufferedImage(660,500,BufferedImage.TYPE_INT_ARGB);
BufferedImage attImage = new BufferedImage(100,500,BufferedImage.TYPE_INT_ARGB);
final Panel panel = new Panel(img2,points,0);
final Attributes attPanel = new Attributes(attImage, panel);
panel.type = 0;
JLabel label2 = new JLabel("Paint");
frame.getContentPane().add(panel,BorderLayout.CENTER);
frame.getContentPane().add(label2,BorderLayout.SOUTH);
frame.getContentPane().add(attPanel,BorderLayout.EAST);
frame.pack();
}
});
JMenuItem fireworksItem = new JMenuItem("Fireworks");
fireworksItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
frame.getContentPane().removeAll();
BufferedImage img2 = new BufferedImage(660,500,BufferedImage.TYPE_INT_ARGB);
BufferedImage attImage = new BufferedImage(100,500,BufferedImage.TYPE_INT_ARGB);
final Panel panel = new Panel(img2,points,1);
final Attributes attPanel = new Attributes(attImage,panel);
panel.type=1;
JLabel label2 = new JLabel("Fireworks");
frame.getContentPane().add(panel,BorderLayout.CENTER);
frame.getContentPane().add(label2,BorderLayout.SOUTH);
frame.getContentPane().add(attPanel,BorderLayout.EAST);
frame.pack();
}
});
JMenuItem dotsItem = new JMenuItem("Dots");
dotsItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
frame.getContentPane().removeAll();
BufferedImage img2 = new BufferedImage(660,500,BufferedImage.TYPE_INT_ARGB);
BufferedImage attImage = new BufferedImage(100,500,BufferedImage.TYPE_INT_ARGB);
final Panel panel = new Panel(img2,points,2);
final Attributes attPanel = new Attributes(attImage,panel);
panel.type=2;
JLabel label2 = new JLabel("Dots");
frame.getContentPane().add(panel,BorderLayout.CENTER);
frame.getContentPane().add(label2,BorderLayout.SOUTH);
frame.getContentPane().add(attPanel,BorderLayout.EAST);
frame.pack();
}
});
fileMenu.add(saveItem);
fileMenu.add(exitItem);
typeMenu.add(paintItem);
typeMenu.add(dotsItem);
typeMenu.add(fireworksItem);
JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
menuBar.add(typeMenu);
frame.setJMenuBar(menuBar);
frame.pack();
frame.setVisible(true);
}
Color color;
int colorInt;
public static Point findColor(BufferedImage image)
{
int width = image.getWidth();
int height = image.getHeight();
int array[][]= new int[2][10000];
int counter =0;
for (int i=0;i<width;i++)
{
for(int j=0;j<height;j++)
{
int eachColorInt = image.getRGB(i,j);
Color eachColor = new Color(eachColorInt);
if(eachColorInt <= -7271887 && eachColorInt >=-15859712)
{
array[0][counter]=i;
array[1][counter]=j;
//System.out.println("changed");
//System.out.println(counter);
counter++;
}
}
}
Point point;
if(counter !=0)
{
int sumI = array[0][0];
int sumJ = array[1][0];
for (int j=0;j<9999;j++)
{
sumI = sumI + array[0][j+1];
sumJ = sumJ + array[1][j+1];
}
int averageX = sumI/counter;
int averageY = sumJ/counter;
point = new Point(averageX,averageY);
}
else
{
point = new Point (-1,-1);
System.out.println("point");
}
return point;
}
}
class Panel extends JPanel
{
static private final Color outline_color = Color.BLACK;
private final int width, max_x;
private final int height, max_y;
BufferedImage image;
Point points[];
private Graphics2D drawing;
int type=0;
ActionListener taskPerformerTemp = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
}
};
Timer timerTemp= new Timer(0,taskPerformerTemp);
public Panel(final BufferedImage image, Point points[], int type)
{
this.image = image;
this.points = points;
drawing = image.createGraphics();
width = image.getWidth();
height = image.getHeight();
Dimension size = new Dimension(width,height);
setMinimumSize(size);
setMaximumSize(size);
setPreferredSize(size);
max_x = width-1;
max_y = height-1;
if(type==0)
drawLaser();
if(type==1)
drawFireworks();
if(type==2)
drawDots();
}
public void drawLaser()
{
timerTemp.stop();
drawing.setColor(Color.WHITE);
drawing.fillRect(0,0,width,height);
drawing.setColor(Attributes.drawingColor);
drawing.setStroke(Attributes.drawingStroke);
for (int i=0; i<points.length;i++)
{
int comp_x = (int)points[i].getX();
int comp_y = (int)points[i].getY();
if ((comp_x == -1) && (comp_y == -1))
{
points[i]=points[i-1];
for(int j=0;j<points.length;j++)
{
System.out.println(points[j]);
}
}
for(int k=0;k<(points.length)-2;k++)
{
int x1 = (int)points[k].getX();
int y1 = (int)points[k].getY();
int x2 = (int)points[k+1].getX();
int y2 = (int)points[k+1].getY();
Line2D.Double line = new Line2D.Double();
line.setLine(x1,y1,x2,y2);
drawing.draw(line);
}
}
}
public void drawFireworks()
{
timerTemp.stop();
for (int i=0; i<points.length;i++)
{
int comp_x = (int)points[i].getX();
int comp_y = (int)points[i].getY();
if ((comp_x == -1) && (comp_y == -1))
points[i]=points[i-1];
}
ActionListener taskPerformer = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
drawing.setColor(Color.BLACK);
drawing.fillRect(0,0,width,height);
drawing.setColor(Attributes.drawingColor);
drawing.setStroke(Attributes.drawingStroke);
double xInit=0;
double yInit=0;
for (int j=0;j<points.length;j++)
{
xInit = points[j].getX();
yInit = points[j].getY();
Ellipse2D.Double fire = new Ellipse2D.Double(xInit,yInit,1,1);
drawing.draw(fire);
}
double x = xInit;
double y = yInit;
Random rand = new Random();
double angle = rand.nextDouble();
for (int m=0;m<20;m++)
{
for (int j=0;j<points.length;j++)
{
x = points[j].getX();
y = points[j].getY();
x = x+ (2*Math.cos(angle)*Attributes.k);
y = y+ (2*Math.sin(angle)*Attributes.k)-(.5*-9.8*(Attributes.k*Attributes.k));
if(y>499)
{
x = xInit;
y = yInit;
}
Ellipse2D.Double fire = new Ellipse2D.Double(x,y,1,1);
drawing.draw(fire);
drawing.setColor(Attributes.drawingColor);
repaint();
}
}
Attributes.k++;
}
};
Timer timer = new Timer(500,taskPerformer);
timerTemp=timer;
timer.restart();
}
public void fireworks(final Point point)
{
int t=0;
while(t!=10)
{
double x = point.getX();
double y = point.getY();
Ellipse2D.Double fire[] = new Ellipse2D.Double[10];
for(int i=0;i<fire.length;i++)
{
fire[i] = new Ellipse2D.Double(x+(i*2),y+(i*2),3,3);
drawing.draw(fire[i]);
}
t++;
}
}
public void drawDots()
{
timerTemp.stop();
ActionListener taskPerformer = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
drawing.setColor(Color.WHITE);
drawing.fillRect(0,0,width,height);
drawing.setColor(Attributes.drawingColor);
drawing.setStroke(Attributes.drawingStroke);
final Rectangle lineBounds[] = new Rectangle[7];
for (int i=0; i<points.length;i++)
{
int comp_x = (int)points[i].getX();
int comp_y = (int)points[i].getY();
if ((comp_x == -1) && (comp_y == -1))
points[i]=points[i-1];
for(int k=0;k<(points.length)-2;k++)
{
int x1 = (int)points[k].getX();
int y1 = (int)points[k].getY();
int x2 = (int)points[k+1].getX();
int y2 = (int)points[k+1].getY();
Line2D.Double line = new Line2D.Double();
line.setLine(x1,y1,x2,y2);
lineBounds[k]=line.getBounds();
drawing.draw(line);
}
}
for(int j=0;j<1000;j++)
{
Random rand = new Random();
int xRand = rand.nextInt(660);
int yRand = rand.nextInt(500);
Ellipse2D.Double circle = new Ellipse2D.Double(xRand,yRand,1,1);
//System.out.println(lineBounds[0]);
for(int m=0;m<lineBounds.length;m++)
{
int wBounds = (int)lineBounds[m].getWidth();
int hBounds = (int)lineBounds[m].getHeight();
int xRect = (int)lineBounds[m].getX();
int yRect = (int)lineBounds[m].getY();
/*Rectangle rectTemp = new Rectangle(xRect,yRect,wBounds+10,hBounds+10);
drawing.draw(rectTemp);*/
if(circle.intersects(xRect-10,yRect-10,wBounds+20,hBounds+20))
{
circle.setFrame(-3,-3,1,1);
}
}
drawing.draw(circle);
repaint();
}
}
};
Timer timer = new Timer(1000,taskPerformer);
timer.start();
}
public void setImage(BufferedImage src)
{
drawing.setPaintMode();
drawing.drawImage(src,0,0,max_x,max_y,0,0,(src.getWidth()-1),(src.getHeight()-1),outline_color,null);
drawing.setXORMode(outline_color);
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image,0,0,null);
}
}
class Attributes extends JPanel
{
public BufferedImage attImage;
public Graphics2D attDrawing;
int attWidth, attMax_x;
int attHeight, attMax_y;
Rectangle rects[]= new Rectangle[14];
Rectangle lineRects[] = new Rectangle[6];
BasicStroke strokes[] = new BasicStroke[6];
static Color drawingColor = Color.DARK_GRAY;
static BasicStroke drawingStroke= new BasicStroke(1f);
Color colors[]= getColors();
Panel panelTemp;
static int k =0;
public Attributes(BufferedImage attImage,Panel panel)
{
panelTemp = panel;
attBkgrd(attImage);
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent event)
{
}
public void mouseReleased(MouseEvent event)
{
Point point = event.getPoint();
int pointX = (int)point.getX();
int pointY = (int)point.getY();
int finder=0;
int lineFinder=0;
System.out.println("point: "+ point);
Rectangle colorBox = new Rectangle(25,60,50,140);
Rectangle strokeBox = new Rectangle (25,210,50,120);
boolean colorBoxBoolean = colorBox.contains(pointX,pointY);
boolean strokeBoxBoolean = strokeBox.contains(pointX,pointY);
if (colorBoxBoolean == true)
{
for(int i=0;i<rects.length;i++)
{
boolean colorBoolean = rects[i].contains(pointX,pointY);
if(colorBoolean==true)
finder=i;
drawingColor = colors[finder];
attDrawing.setColor(drawingColor);
attDrawing.fillRect(38,30,25,25);
repaint();
if(panelTemp.type==0)
panelTemp.drawLaser();
else if (panelTemp.type==1)
panelTemp.drawFireworks();
panelTemp.repaint();
}
}
if (strokeBoxBoolean == true)
{
for (int j=0;j<lineRects.length;j++)
{
boolean strokeBoolean = lineRects[j].contains(pointX,pointY);
if (strokeBoolean == true)
lineFinder =j;
drawingStroke = strokes[lineFinder];
attDrawing.setStroke(drawingStroke);
repaint();
if(panelTemp.type==0)
panelTemp.drawLaser();
else if (panelTemp.type==1)
panelTemp.drawFireworks();
panelTemp.repaint();
}
}
}
});
}
public void attBkgrd(BufferedImage attImage)
{
this.attImage = attImage;
attDrawing = attImage.createGraphics();
attWidth = attImage.getWidth();
attHeight = attImage.getHeight();
Dimension attSize = new Dimension(attWidth,attHeight);
setMinimumSize(attSize);
setMaximumSize(attSize);
setPreferredSize(attSize);
attMax_x = attWidth-1;
attMax_y = attHeight-1;
attDrawing = attImage.createGraphics();
attWidth = attImage.getWidth();
attHeight = attImage.getHeight();
for (int k=1;k<=6;k++)
{
float kFloat = (float)k;
strokes[k-1] = new BasicStroke(kFloat);
}
attDrawing.setColor(Color.WHITE);
attDrawing.fillRect(0,0,attWidth,attHeight);
attDrawing.setColor(Color.LIGHT_GRAY);
attDrawing.setStroke(strokes[5]);
attDrawing.drawRect(0,0,attWidth,attHeight);
attDrawing.setColor(Color.BLACK);
attDrawing.setStroke(strokes[0]);
attDrawing.drawRect(25,60,50,140);
attDrawing.drawLine(50,60,50,200);
attDrawing.drawRect(36,28,28,28);
for(int i=60;i<210;i=i+20)
{
attDrawing.drawLine(25,i,75,i);
}
int rectCounter1 = 0;
int rectCounter2 =7;
for(int j=62;j<200;j=j+20)
{
attDrawing.setColor(colors[rectCounter1]);
attDrawing.fillRect(27,j,22,17);
Rectangle rect1 = new Rectangle(27-2,j-2,22+3,17+3);
rects[rectCounter1]= rect1;
attDrawing.setColor(colors[rectCounter2]);
attDrawing.fillRect(52,j,22,17);
Rectangle rect2 = new Rectangle(52-2,j-2,22+3,17+3);
rects[rectCounter2]=rect2;
rectCounter1 = rectCounter1+1;
rectCounter2 = rectCounter2+1;
}
Line2D.Double lines[] = new Line2D.Double[6];
int lineY = 220;
for (int k=0;k<6;k++)
{
lines[k] = new Line2D.Double(25,lineY,75,lineY);
lineY = lineY + 20;
lineRects[k] = new Rectangle(25,lineY-30,50,20);
}
attDrawing.setColor(Color.BLACK);
for (int h=0;h<6;h++)
{
attDrawing.setStroke(strokes[h]);
attDrawing.draw(lines[h]);
}
}
public Color[] getColors()
{
Color red = new Color(12582912);
Color redOrange = new Color(15937536);
Color orange = new Color(16146432);
Color yellowOrange = new Color(16750848);
Color yellowGreen = new Color(6736896);
Color green = new Color(26112);
Color blueGreen = new Color(18022);
Color blueViolet = new Color(3342489);
Color violet = new Color(6684876);
Color redViolet = new Color(10027110);
Color temp[] = new Color[14];
temp[0]=Color.DARK_GRAY;
temp[1]=red;
temp[2]=redOrange;
temp[3]=orange;
temp[4]=yellowOrange;
temp[5]=Color.YELLOW;
temp[6]=yellowGreen;
temp[7]=Color.LIGHT_GRAY;
temp[8]=green;
temp[9]=blueGreen;
temp[10]=Color.BLUE;
temp[11]=blueViolet;
temp[12]=violet;
temp[13]=redViolet;
return temp;
}
public void setAttImage(BufferedImage src)
{
attDrawing.drawImage(src,0,0,attMax_x,attMax_y,0,0,(src.getWidth()-1),(src.getHeight()-1),Color.WHITE,null);
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(attImage,0,0,null);
}
}