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(); } } |
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.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment