Here is the code:
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 | import java.awt.*; import java.awt.image.*; import java.awt.geom.*; import javax.swing.*; import java.io.*; import javax.imageio.ImageIO; import java.awt.event.*; public class ImageArranger extends JFrame{ JLabel ltitle=null; JTextField tftitle=null; JButton btitle=null; JButton baddimg1=null; JButton baddimg2=null; JButton baddimg3=null; DrawPanel p2=null; String filename1=null; String filename2=null; String filename3=null; public static void main(String[] args){ ImageArranger ap=new ImageArranger(); } public ImageArranger(){ super("Image Arranger"); setSize(600,720); JPanel p1=new JPanel(); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.insets=new Insets(5,0,0,0); p1.setLayout(new GridBagLayout()); ltitle=new JLabel("Title:"); p1.add(ltitle); tftitle=new JTextField(10); p1.add(tftitle); gbc.gridy++; btitle=new JButton("Add Title"); btitle.addActionListener(new ButtonHandler()); p1.add(btitle,gbc); gbc.gridy++; baddimg1=new JButton("Add 1st Image"); baddimg1.addActionListener(new ButtonHandler()); p1.add(baddimg1,gbc); gbc.gridy++; baddimg2=new JButton("Add 2nd Image"); baddimg2.addActionListener(new ButtonHandler()); p1.add(baddimg2,gbc); gbc.gridy++; baddimg3=new JButton("Add 3rd Image"); baddimg3.addActionListener(new ButtonHandler()); p1.add(baddimg3,gbc); gbc.gridy++; JButton bsave=new JButton("Save"); bsave.addActionListener(new ButtonHandler()); p1.add(bsave,gbc); add("West",p1); p2=new DrawPanel(); p2.setPreferredSize(new Dimension(300,720)); p2.setBackground(Color.BLACK); add("East",p2); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); show(); } class DrawPanel extends JPanel{ public void paintComponent(Graphics g){ super.paintComponent(g); Graphics2D g2=(Graphics2D)g; if(tftitle.getText()!=null){ g2.setBackground(Color.BLACK); g2.clearRect(0,0,this.getWidth(),50); Font f=new Font("Monospaced",Font.BOLD,24); FontMetrics fm=this.getFontMetrics(f); if(fm.stringWidth(tftitle.getText())>(p2.getWidth()-20)){ return; } g2.setColor(Color.WHITE); g2.setFont(f); g2.drawString(tftitle.getText(),(p2.getWidth()/2)-(fm.stringWidth(tftitle.getText())/2),30); } if(filename1!=null){ BufferedImage bufimage=null; try{ bufimage=ImageIO.read(new File(filename1)); }catch(Exception ex){ } g2.drawImage(bufimage,15,50,270,200,p2); } if(filename2!=null){ BufferedImage bufimage=null; try{ bufimage=ImageIO.read(new File(filename2)); }catch(Exception ex){ } g2.drawImage(bufimage,15,260,270,200,p2); } if(filename3!=null){ BufferedImage bufimage=null; try{ bufimage=ImageIO.read(new File(filename3)); }catch(Exception ex){ } g2.drawImage(bufimage,15,470,270,200,p2); } } } class ButtonHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ if(e.getActionCommand()=="Add Title"){ p2.repaint(); } if(e.getActionCommand()=="Add 1st Image"){ FileDialog fd=new FileDialog(ImageArranger.this,"Open Image File",FileDialog.LOAD); fd.setDirectory("C:\\"); fd.setVisible(true); filename1=fd.getFile(); p2.repaint(); } if(e.getActionCommand()=="Add 2nd Image"){ FileDialog fd=new FileDialog(ImageArranger.this,"Open Image File",FileDialog.LOAD); fd.setDirectory("C:\\"); fd.setVisible(true); filename2=fd.getFile(); p2.repaint(); } if(e.getActionCommand()=="Add 3rd Image"){ FileDialog fd=new FileDialog(ImageArranger.this,"Open Image File",FileDialog.LOAD); fd.setDirectory("C:\\"); fd.setVisible(true); filename3=fd.getFile(); p2.repaint(); } if(e.getActionCommand()=="Save"){ BufferedImage bufimage = new BufferedImage(p2.getWidth(),p2.getHeight(),BufferedImage.TYPE_INT_ARGB); Graphics2D g=(Graphics2D)bufimage.getGraphics(); p2.paint(g); try { ImageIO.write(bufimage,"png", new File("result.png")); } catch (IOException ex) { ex.printStackTrace(); } } } } } |
No comments:
Post a Comment