//Example1D import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JPanel; public class Example1D extends JFrame implements ActionListener { private static final String window_title = "Image File IO"; private static final String btnOpen_title = "Open"; private static final String btnSave_title = "Save"; private static final int window_width = 640; private static final int window_height = 400; private static final long serialVersionUID = 1L; JFileChooser fc = new JFileChooser(); JButton bt01; JButton bt02; BufferedImage img = null; public static void main(final String ar[]) { final JFrame f = new Example1D(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(Example1D.window_width, Example1D.window_height); f.setVisible(true); } Example1D() { super(Example1D.window_title); this.bt01 = new JButton(Example1D.btnOpen_title); this.bt01.setActionCommand(Example1D.btnOpen_title); this.bt01.addActionListener(this); this.bt02 = new JButton(Example1D.btnSave_title); this.bt02.setActionCommand(Example1D.btnSave_title); this.bt02.addActionListener(this); final Container c = this.getContentPane(); final JPanel p1 = new JPanel(); p1.add(this.bt01); p1.add(this.bt02); c.add(p1, BorderLayout.NORTH); } public void actionPerformed(final ActionEvent ae) { if (ae.getActionCommand() == Example1D.btnOpen_title) { final int fd = this.fc.showOpenDialog(Example1D.this); try { if (fd == JFileChooser.APPROVE_OPTION) { try { this.img = ImageIO.read(this.fc.getSelectedFile()); this.repaint(); } catch (final Exception e) { } } } catch (final Exception e) { } } else if (ae.getActionCommand() == Example1D.btnSave_title) { final int fd = this.fc.showSaveDialog(Example1D.this); try { if (fd == JFileChooser.APPROVE_OPTION) { try { final boolean rc = ImageIO.write(this.img, "jpeg", this.fc.getSelectedFile()); this.repaint(); } catch (final Exception e) { } } } catch (final Exception e) { } } } @Override public void update(final Graphics g) { this.paint(g); } @Override public void paint(final Graphics g) { final Graphics2D g2 = (Graphics2D) g; if (this.img == null) { g.clearRect(0,0,Example1D.window_width-1, Example1D.window_height-1); }else{ g2.drawImage(this.img, 0, 0, this); final Graphics2D g3 = this.img.createGraphics(); g3.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Font font = new Font("Arial", Font.PLAIN, 24); g3.setFont(font); g3.setColor(Color.red); g3.drawString("hear", 100, 100); } if (this.img != null) { g2.drawImage(this.img, 0, 0, this); } bt01.repaint(); bt02.repaint(); } }