1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.File; import java.util.*; import javax.imageio.ImageIO; import javax.swing.*; import javax.swing.border.Border; public class ShortestDistance extends JFrame{ private JLabel lresult=null; private JPanel pmain=null; private JButton bcalculate=null; private ArrayList<Point> pall=null; private ArrayList<Point> pall2=null; private ArrayList<Integer> porder=null; private ArrayList<String> routes=null; //all possibilities private ArrayList<String> routes2=null; //start point = point 1 private ArrayList<Integer> routes2dist=null; public ShortestDistance(){ super("Shortest Distance Finder"); setSize(500,450); lresult=new JLabel(" "); lresult.setBackground(Color.WHITE); pmain=new PanelDraw(); pmain.setBackground(Color.WHITE); pmain.addMouseListener(new MouseHandler()); pmain.addMouseMotionListener(new MouseHandler()); bcalculate=new JButton("Calculate"); bcalculate.addActionListener(new ButtonHandler()); setLayout(new BorderLayout()); add("North",lresult); add("Center",pmain); add("South",bcalculate); pall=new ArrayList<Point>(); pall2=new ArrayList<Point>(); porder=new ArrayList<Integer>(); routes=new ArrayList<String>(); routes2=new ArrayList<String>(); routes2dist=new ArrayList<Integer>(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); show(); } class PanelDraw extends JPanel{ public void paintComponent(Graphics g){ super.paintComponent(g); if(!pall.isEmpty()){ for(int i=0;i<pall.size();i++){ g.fillOval(pall.get(i).x,pall.get(i).y,5,5); g.drawString(porder.get(i)+"",(pall.get(i).x)+2,(pall.get(i).y)+2); } } } } class MouseHandler implements MouseMotionListener, MouseListener{ public void mouseMoved(MouseEvent e){ } public void mouseDragged(MouseEvent e){ } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } public void mouseReleased(MouseEvent e){ } public void mousePressed(MouseEvent e){ } public void mouseClicked(MouseEvent e){ pall.add(e.getPoint()); pall2.add(e.getPoint()); if(porder.isEmpty()){ porder.add(0,1); } if(!porder.isEmpty()){ //System.out.println(porder.get(porder.size()-1)); porder.add(porder.get(porder.size()-1)+1); } repaint(); } } class ButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ int counter=pall.size()*pall.size(); String[] pstart=new String[counter]; String[] pend=new String[counter]; Integer[] distance=new Integer[counter]; counter=0; for(int i=0;i<pall.size();i++){ for(int j=0;j<pall2.size();j++){ int x1=pall.get(i).x; int y1=pall.get(i).y; int x2=pall2.get(j).x; int y2=pall2.get(j).y; int x=Math.abs(x2-x1); int y=Math.abs(y2-y1); pstart[counter]=""+(i+1); pend[counter]=""+(j+1); distance[counter]=((int)Math.sqrt((x*x)+(y*y))); counter++; } } ArrayList<String> pname=new ArrayList<String>(); counter=0; for(int i=0;i<pstart.length;i++){ if(pname.contains(pstart[i])){ continue; } else{ pname.add(counter,pstart[i]); counter++; } } String allpoints=""; for(int i=0;i<pname.size();i++){ allpoints=allpoints+pname.get(i); } funcPermutation("",allpoints); for(int i=0;i<routes.size();i++){ char c=routes.get(i).charAt(0); if(c=='1'){ routes2.add(routes.get(i)); } } int totaldistance=0; for(int i=0;i<routes2.size();i++){ String temproute=routes2.get(i); for(int j=0;j<temproute.length()-1;j++){ String routeleg=temproute.substring(j,j+2); //System.out.println(routeleg); for(int k=0;k<pstart.length;k++){ if(routeleg.equals(pstart[k]+pend[k])){ totaldistance=totaldistance+distance[k]; } } } //System.out.println(temproute+":"+totaldistance); routes2dist.add(totaldistance); totaldistance=0; } int indexshortest=0; int shortest=routes2dist.get(0); for(int i=1;i<routes2.size();i++){ //System.out.println(routes2.get(i)+":"+routes2dist.get(i)); if(routes2dist.get(i)<shortest){ shortest=routes2dist.get(i); indexshortest=i; } } String stresult=""; for(int i=0;i<routes2.get(indexshortest).length();i++){ stresult=stresult+routes2.get(indexshortest).charAt(i); if(i<routes2.get(indexshortest).length()-1){ stresult=stresult+" - "; } } lresult.setText(stresult); } public void funcPermutation(String st1,String st2){ if(st2.length()<=1){ routes.add(st1+st2); } else for (int i= 0;i<st2.length();i++) { try { String stresult=st2.substring(0,i)+st2.substring(i+1); funcPermutation(st1+st2.charAt(i),stresult); }catch(Exception ex) { } } } } public static void main(String[] args){ new ShortestDistance(); } } |
Sunday, April 10, 2016
Finding Shortest Route
This program calculates the shortest route between multiple points. The user inputs all the points. The first one becomes the starting point. Permutation is used here to explore every possibility.
Tuesday, April 5, 2016
Automatic Presentation Maker
Utilizing POI API which is designed to manipulate various file formats, this his short program creates a very simple presentation file automatically. The number of slides depends on the number of sentences entered by the user plus one for title.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.StringTokenizer; import javax.swing.*; import org.apache.poi.xslf.usermodel.SlideLayout; import org.apache.poi.xslf.usermodel.XMLSlideShow; import org.apache.poi.xslf.usermodel.XSLFSlide; import org.apache.poi.xslf.usermodel.XSLFSlideLayout; import org.apache.poi.xslf.usermodel.XSLFSlideMaster; import org.apache.poi.xslf.usermodel.XSLFTextParagraph; import org.apache.poi.xslf.usermodel.XSLFTextRun; import org.apache.poi.xslf.usermodel.XSLFTextShape; public class PresentationMaker extends JFrame{ private JTextField tftitle=null; private JTextArea tacontent=null; private JButton bcreate=null; public PresentationMaker(){ setSize(400,400); tftitle=new JTextField(); tftitle.setColumns(20); tacontent=new JTextArea(20,20); tacontent.setLineWrap(true); bcreate=new JButton("Create"); bcreate.addActionListener(new ButtonHandler()); setLayout(new BorderLayout()); add("North",tftitle); add("Center",tacontent); add("South",bcreate); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); show(); } class ButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ String stitle=tftitle.getText(); String stemp=tacontent.getText(); StringTokenizer stoken=new StringTokenizer(stemp); ArrayList<String> al=new ArrayList<String>(); int counter=0; for(int i=0;i<stemp.length();i++){ if(stemp.charAt(i)=='.'){ counter++; } } al.add(0,stitle.trim()); counter++; for(int i=1;i<counter;i++){ al.add(i,stoken.nextToken(".").trim()+"."); System.out.println(al.get(i)); } XMLSlideShow ppt=new XMLSlideShow(); XSLFSlideMaster slideMaster=ppt.getSlideMasters().get(0); XSLFSlideLayout titleLayout=slideMaster.getLayout(SlideLayout.TITLE_ONLY); XSLFSlide slide[]=new XSLFSlide[counter+1]; for(int i=0;i<counter;i++){ slide[i]=ppt.createSlide(titleLayout); XSLFTextShape title=slide[i].getPlaceholder(0); title.setText(al.get(i)); } File file=new File("C://Users/MARIO/Documents/NetBeansProjects/presentation1.pptx"); try{ FileOutputStream out=new FileOutputStream(file); ppt.write(out); out.close(); }catch(Exception ex){ } } } public static void main(String[] args) throws IOException{ new PresentationMaker(); } } |
Sunday, April 3, 2016
Image Clipping Tool
This small app removes unwanted background from any image. Two methods in the Graphics class, drawPolyline and drawPolygon are used in this program. This enables the user to clip an arbitrary shape out of an image.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.File; import java.util.ArrayList; import javax.imageio.ImageIO; import javax.swing.*; public class ImageClipper extends JFrame{ private JButton bopen=null; private JPanel pmain=null; private String fname=null; private JButton bremove=null; private BufferedImage bufimage=null; private Color pixcolor[][]=null; private ArrayList<Point> allpoint=null; private boolean mclicked=false; private Point tempoint=null; private boolean mmoved=false; private MouseHandler mh=null; private Polygon tempoly=null; public ImageClipper(){ super("Background Remover"); setSize(800,600); setLayout(new BorderLayout()); bopen=new JButton("Open"); bopen.addActionListener(new ButtonHandler()); pmain=new WallImage(); //pmain.setPreferredSize(new Dimension(800,500)); mh=new MouseHandler(); pmain.addMouseListener(mh); pmain.addMouseMotionListener(mh); bremove=new JButton("Remove"); bremove.addActionListener(new ButtonHandler()); add("North",bopen); add("Center",pmain); add("South",bremove); allpoint=new ArrayList<Point>(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); show(); } class WallImage extends JPanel{ public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2=(Graphics2D)g; if(bufimage!=null){ g2.drawImage(bufimage,0,0,pmain); } int px[]=null; int py[]=null; if(allpoint.size()>0){ px=new int[allpoint.size()]; py=new int[allpoint.size()]; for(int i=0;i<allpoint.size();i++){ px[i]=(allpoint.get(i)).x; py[i]=(allpoint.get(i)).y; } g2.setColor(Color.YELLOW); g2.drawPolyline(px,py,allpoint.size()); } if(allpoint.size()>0&&mmoved==true){ g2.setColor(Color.YELLOW); g2.drawLine((allpoint.get(allpoint.size()-1)).x,(allpoint.get(allpoint.size()-1)).y,tempoint.x,tempoint.y); } if(allpoint.size()>0&&mclicked==true){ if(allpoint.size()>2&& tempoint.x<=(allpoint.get(0).x)+5&& tempoint.x>=(allpoint.get(0).x)-5&& tempoint.y<=(allpoint.get(0).y)+5&& tempoint.y>=(allpoint.get(0).y)-5){ (allpoint.get(allpoint.size()-1)).x=allpoint.get(0).x; (allpoint.get(allpoint.size()-1)).y=allpoint.get(0).y; g2.drawImage(bufimage,0,0,pmain); px=new int[allpoint.size()]; py=new int[allpoint.size()]; for(int i=0;i<allpoint.size();i++){ px[i]=(allpoint.get(i)).x; py[i]=(allpoint.get(i)).y; } g2.setColor(Color.ORANGE); g2.drawPolygon(px,py,allpoint.size()); tempoly=new Polygon(px,py,allpoint.size()); pmain.removeMouseListener(mh); pmain.removeMouseMotionListener(mh); } else{ g2.drawPolyline(px,py,allpoint.size()); mclicked=false; } } } } class ButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getActionCommand()=="Open"){ FileDialog fd=new FileDialog(ImageClipper.this,"Open Image File",FileDialog.LOAD); fd.setDirectory("C:\\"); fd.setVisible(true); fname=fd.getDirectory()+fd.getFile(); try{ bufimage=ImageIO.read(new File(fname)); }catch(Exception ex){ } allpoint.clear(); tempoint=new Point(); mclicked=false; mmoved=false; pmain.addMouseListener(mh); pmain.addMouseMotionListener(mh); pmain.repaint(); } if(e.getActionCommand()=="Remove"){ Graphics2D g2=(Graphics2D)pmain.getGraphics(); g2.setBackground(Color.WHITE); g2.clearRect(0,0,pmain.getWidth(),pmain.getHeight()); g2.setClip(tempoly); g2.drawImage(bufimage,pmain.getX(),pmain.getY(),pmain); } } } class MouseHandler implements MouseMotionListener, MouseListener{ public void mouseMoved(MouseEvent e){ if(!allpoint.isEmpty()){ tempoint=e.getPoint(); mmoved=true; repaint(); } } public void mouseDragged(MouseEvent e){ } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } public void mouseReleased(MouseEvent e){ } public void mousePressed(MouseEvent e){ } public void mouseClicked(MouseEvent e){ allpoint.add(e.getPoint()); mclicked=true; repaint(); } } public static void main(String[] args){ new ImageClipper(); } } |
Saturday, April 2, 2016
Small Drawing App
This small drawing app lets the user to create freehand drawings. It works by detecting both mouse events and mouse-motion events. Drawing a point is accomplished by utilizing the drawLine() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import javax.swing.*; import javax.swing.border.Border; public class DrawingApp extends JFrame{ private JPanel pmain=null; private JPanel pright=null; private JLabel lmain=null; private BufferedImage bufimage=null; private Point pstart=null; private Point pend=null; private JLabel lpen=null; private JLabel lerase=null; private boolean penpicked=false; private boolean erasepicked=false; private JLabel lcolor[]=null; private Color cpicked=null; public DrawingApp(){ super("Free Drawing"); setSize(500,450); pmain=new PanelDraw(); pmain.addMouseListener(new MouseHandler()); pmain.addMouseMotionListener(new MouseHandler()); BufferedImage bufimagetemp=null; try{ bufimagetemp=ImageIO.read(new File("pencil.jpg")); }catch(Exception e){ } ImageIcon icon=new ImageIcon(bufimagetemp); lpen=new JLabel(); lpen.setIcon(icon); lpen.setBorder(BorderFactory.createLineBorder(Color.black)); lpen.addMouseListener(new MouseHandler()); try{ bufimagetemp=ImageIO.read(new File("eraser.jpg")); }catch(Exception e){ } icon=new ImageIcon(bufimagetemp); lerase=new JLabel(); lerase.setIcon(icon); lerase.setBorder(BorderFactory.createLineBorder(Color.black)); lerase.addMouseListener(new MouseHandler()); try{ bufimagetemp=ImageIO.read(new File("black.jpg")); }catch(Exception e){ } icon=new ImageIcon(bufimagetemp); lcolor=new JLabel[4]; lcolor[0]=new JLabel(); lcolor[0].setIcon(icon); lcolor[0].setBorder(BorderFactory.createLineBorder(Color.black)); lcolor[0].addMouseListener(new MouseHandler()); try{ bufimagetemp=ImageIO.read(new File("red.jpg")); }catch(Exception e){ } icon=new ImageIcon(bufimagetemp); lcolor[1]=new JLabel(); lcolor[1].setIcon(icon); lcolor[1].setBorder(BorderFactory.createLineBorder(Color.black)); lcolor[1].addMouseListener(new MouseHandler()); try{ bufimagetemp=ImageIO.read(new File("green.jpg")); }catch(Exception e){ } icon=new ImageIcon(bufimagetemp); lcolor[2]=new JLabel(); lcolor[2].setIcon(icon); lcolor[2].setBorder(BorderFactory.createLineBorder(Color.black)); lcolor[2].addMouseListener(new MouseHandler()); try{ bufimagetemp=ImageIO.read(new File("blue.jpg")); }catch(Exception e){ } icon=new ImageIcon(bufimagetemp); lcolor[3]=new JLabel(); lcolor[3].setIcon(icon); lcolor[3].setBorder(BorderFactory.createLineBorder(Color.black)); lcolor[3].addMouseListener(new MouseHandler()); pright=new JPanel(); pright.setLayout(new GridBagLayout()); GridBagConstraints gbc=new GridBagConstraints(); gbc.gridx=0; gbc.gridy=0; gbc.insets=new Insets(5,5,0,5); gbc.fill=GridBagConstraints.HORIZONTAL; pright.add(lpen,gbc); gbc.gridy=1; pright.add(lerase,gbc); gbc.gridy=2; pright.add(lcolor[0],gbc); gbc.gridy=3; pright.add(lcolor[1],gbc); gbc.gridy=4; pright.add(lcolor[2],gbc); gbc.gridy=5; pright.add(lcolor[3],gbc); setLayout(new BorderLayout()); add("Center",pmain); add("East",pright); try{ bufimage=ImageIO.read(new File("blank.png")); }catch(Exception e){ } lmain=new JLabel(); lmain.setIcon(new ImageIcon(bufimage)); pmain.add(lmain); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); show(); } class PanelDraw extends JPanel{ public void paintComponent(Graphics g){ super.paintComponent(g); g=bufimage.getGraphics(); if(pstart!=null&&pend!=null&&penpicked==true){ if(cpicked==null){ g.setColor(Color.BLACK); } else{ g.setColor(cpicked); } g.drawLine(pstart.x,pstart.y,pend.x,pend.y); pstart.x=pend.x; pstart.y=pend.y; } if(pstart!=null&&erasepicked==true){ g.setColor(Color.WHITE); g.fillRect(pstart.x,pstart.y,10,10); pstart.x=pend.x; pstart.y=pend.y; } } } class MouseHandler implements MouseMotionListener, MouseListener{ public void mouseMoved(MouseEvent e){ } public void mouseDragged(MouseEvent e){ pend = e.getPoint(); repaint(); } public void mouseEntered(MouseEvent e){ } public void mouseExited(MouseEvent e){ } public void mouseReleased(MouseEvent e){ pstart=null; pend=null; repaint(); } public void mousePressed(MouseEvent e){ pstart=e.getPoint(); } public void mouseClicked(MouseEvent e){ if(e.getSource()==lpen){ penpicked=true; erasepicked=false; } if(e.getSource()==lerase){ erasepicked=true; penpicked=false; } if(e.getSource()==lcolor[0]){ cpicked=Color.BLACK; } if(e.getSource()==lcolor[1]){ cpicked=Color.RED; } if(e.getSource()==lcolor[2]){ cpicked=Color.GREEN; } if(e.getSource()==lcolor[3]){ cpicked=Color.BLUE; } } } public static void main(String[] args){ new DrawingApp(); } } |
Subscribe to:
Posts (Atom)




