package myprojects.paint; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import javax.swing.*; import javax.swing.colorchooser.*; import com.sun.image.codec.jpeg.*; import java.io.*; public class Paint extends JFrame implements MouseListener, ActionListener{ int initIndex; int imageWidth, imageHeight; Point startP = new Point(0, 0); Point endP = new Point(0, 0); String shape; String do_smth; BufferedImage bi_buf; Color foregroundColor, backgroundColor; JMenuBar menuBar; JMenu menu1, menu2; JMenuItem item2, item3, item4, item5, item6; JToolBar toolBar; JButton rectBtn, ovalBtn, lineBtn, frectBtn, fovalBtn, flineBtn, b4; JComboBox combo; JLabel label; JPanel panel1, panel2, panel3, panel4, panel5; public GraphicsPanel dp; String [] colorNames = {"Red", "Blue", "Green", "Yellow", "Black", "White"}; Color[] colors = {Color.red, Color.blue, Color.green, Color.yellow, Color.black, Color.white}; public Paint(){ /*try{ //UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); }catch (Exception ex){ System.out.println("Unable to change :" + ex); }*/ menuBar = new JMenuBar(); setJMenuBar(menuBar); menu1 = new JMenu("File"); menu2 = new JMenu("Edit"); menuBar.add(menu1); menuBar.add(menu2); item2 = new JMenuItem("Open"); item3 = new JMenuItem("Save"); item4 = new JMenuItem("Close"); menu1.add(item2); menu1.add(item3); menu1.addSeparator(); menu1.add(item4); item5 = new JMenuItem("Copy"); item6 = new JMenuItem("Paste"); menu2.add(item5); menu2.add(item6); item2.addActionListener(this); item3.addActionListener(this); item4.addActionListener(this); item5.addActionListener(this); item6.addActionListener(this); toolBar = new JToolBar(JToolBar.HORIZONTAL); GridLayout lm = new GridLayout(2,1); panel1 = new JPanel(lm); panel2 = new JPanel(lm); panel3 = new JPanel(lm); panel4 = new JPanel(lm); panel5 = new JPanel(lm); rectBtn = new JButton("Rectangle"); frectBtn = new JButton("Fill Rectangle"); ovalBtn = new JButton("Oval"); fovalBtn = new JButton("Fill Oval"); lineBtn = new JButton("Line"); flineBtn = new JButton("Free Line"); panel1.add(rectBtn); panel1.add(frectBtn); panel2.add(ovalBtn); panel2.add(fovalBtn); panel3.add(lineBtn); panel3.add(flineBtn); rectBtn.addActionListener(this); ovalBtn.addActionListener(this); lineBtn.addActionListener(this); frectBtn.addActionListener(this); fovalBtn.addActionListener(this); flineBtn.addActionListener(this); // comboboks taustavarvidega ja ei toota eriti ;) label = new JLabel("Set Background"); combo = new JComboBox(colorNames); combo.setSelectedIndex(initIndex); panel4.add(label); panel4.add(combo); combo.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ JComboBox box = (JComboBox)e.getSource(); Color cl = colors[box.getSelectedIndex()]; //dp.setBackground(cl); if(cl != null) backgroundColor = cl; } }); // varvitabeli nupp label = new JLabel("Set Foreground"); b4 = new JButton("Color Chooser"); panel5.add(label); panel5.add(b4); b4.addActionListener(this); Container contentPane = getContentPane(); contentPane.add(toolBar, BorderLayout.NORTH); dp = new GraphicsPanel(); dp.setBackground(Color.white); contentPane.add(dp, BorderLayout.CENTER); toolBar.add(panel1); toolBar.add(panel2); toolBar.add(panel3); toolBar.addSeparator(); toolBar.add(panel4); toolBar.add(panel5); dp.addMouseListener(this); } public void mouseMoved(MouseEvent e){} public void mouseDragged(MouseEvent e){} public void mousePressed(MouseEvent e){ startP.setLocation(e.getX(), e.getY()); } public void mouseReleased(MouseEvent e){ endP.setLocation(e.getX(), e.getY()); repaint(); } public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void actionPerformed(ActionEvent e){ if(e.getSource() == b4){ Color c = JColorChooser.showDialog( Paint.this, "Choose a color...", getForeground()); if (c != null) foregroundColor = c; } if(e.getSource() == item4){System.exit(0);} if(e.getSource() == item2){ // pildi avamine do_smth = "open_image"; } if(e.getSource() == item3){ // pildi salvestamine do_smth = "save_image"; } if(e.getSource() == item5){ // pildi kopeerimine do_smth = "copy_image"; } if(e.getSource() == item6){ // pildi pastemine do_smth = "paste_image"; } if(e.getSource() == ovalBtn){ shape = "oval"; } if(e.getSource() == rectBtn){ shape = "rect"; } if(e.getSource() == lineBtn){ shape = "line"; } if(e.getSource() == fovalBtn){ shape = "foval"; } if(e.getSource() == frectBtn){ shape = "frect"; } if(e.getSource() == flineBtn){ shape = "fline"; } } class GraphicsPanel extends JPanel{ BufferedImage bi = new BufferedImage(600, 400, BufferedImage.TYPE_INT_RGB); Graphics2D big=bi.createGraphics(); String bug = "...SEE ON BUGI..."; public void paint(Graphics g){ if(do_smth == "copy_image"){ // kopeeri bi_buf = new BufferedImage((int)endP.getX()-(int)startP.getX(), (int)endP.getY()-(int)startP.getY(), BufferedImage.TYPE_INT_RGB); Graphics2D bi_buf_g=bi_buf.createGraphics(); bi_buf_g.drawImage(bi, -(int)startP.getX(), -(int)startP.getY(), this); } if(do_smth == "paste_image"){ // paste if(bi_buf != null){ big.drawImage(bi_buf, (int)startP.getX(), (int)startP.getY(), this); repaint(); } } if(do_smth == "open_image"){ // ava pilt try{ Image image = Toolkit.getDefaultToolkit().getImage("draw.jpg"); MediaTracker tracker = new MediaTracker(this); tracker.addImage(image, 0); tracker.waitForAll(); big.drawImage(image, 0, 0, this); repaint(); }catch(Exception ex){ex.printStackTrace();} } if(do_smth == "save_image"){ // salvesta pilt try{ FileOutputStream fos = new FileOutputStream("draw.jpg"); JPEGCodec.createJPEGEncoder(fos).encode(bi); fos.close(); }catch(Exception ex){ex.printStackTrace();} } if(shape == "line"){ big.drawLine((int)startP.getX(), (int)startP.getY(), (int)endP.getX(), (int)endP.getY()); } if(shape == "fline"){ big.drawString(bug, 100, 100); } if(shape == "rect"){ big.drawRect((int)startP.getX(), (int)startP.getY(), (int)endP.getX()-(int)startP.getX(), (int)endP.getY()-(int)startP.getY()); } if(shape == "oval"){ big.drawOval((int)startP.getX(), (int)startP.getY(), (int)endP.getX()-(int)startP.getX(), (int)endP.getY()-(int)startP.getY()); } if(shape == "frect"){ big.fillRect((int)startP.getX(), (int)startP.getY(), (int)endP.getX()-(int)startP.getX(), (int)endP.getY()-(int)startP.getY()); } if(shape == "foval"){ big.fillOval((int)startP.getX(), (int)startP.getY(), (int)endP.getX()-(int)startP.getX(), (int)endP.getY()-(int)startP.getY()); } g.drawImage(bi, 0, 0, this); big.setColor(foregroundColor); //big.setBackground(backgroundColor); // ei toota nii nagu peab, viga ei anna } } // end of GraphicsPanel() public static void main(String args[]) { System.out.println("Starting Paint..."); //JFrame.setDefaultLookAndFeelDecorated(true); Paint mainFrame = new Paint(); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.setSize(600, 500); mainFrame.setTitle("Paint"); mainFrame.setVisible(true); } }