BufferedImage image=ImageIO.read(new File(fileName));//读取图片
JLabel jl=new JLabel(new ImageIcon(image)); //把图片放在JLABEL中显示
.
.
.
Graphics2D g=image.createGraphics();
g.drawString("为啥不好用呢",0,0);
g.dispose();大概代码就是这样了,所有组件放在一个JFRAME中。无论怎么弄这行字也整不到图片上,劳烦各位大大指条明路吧。

解决方案 »

  1.   

    坐标有问题吧
    g.drawString("为啥不好用呢",0,0);0,0划在一个点了
      

  2.   

    改了一下还是不行,图片的大小时81*159
    改成了g.drawString("为啥不好用呢",50,100);还是不好用。。
      

  3.   

    BufferedImage image=ImageIO.read(new File(fileName));//读取图片
    .
    .
    .
    Graphics2D g=image.createGraphics();
    g.drawString("为啥不好用呢",0,0);
    g.dispose();
    JLabel jl=new JLabel(new ImageIcon(image)); //把图片放在JLABEL中显示----------------这样你试试!
      

  4.   

    还是不好用……哭了
    完整代码如下。。JDK6,各位大大们再帮帮忙吧,小弟脑瓜都炸了package testapp;import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;import javax.imageio.ImageIO;
    import javax.swing.*;import sun.awt.image.GifImageDecoder;
    import sun.awt.image.InputStreamImageSource;public class ImageFrame extends JFrame {    BufferedImage image;
        //Image image;    ImagePanel l;    Panel pl;
        //JPanel l;    Container c;    JPanel p;    JButton jx;    JTextField jtf;    Panel pjx;    public ImageFrame() {
            super();        this.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent arg0) {
                    ImageFrame.this.setVisible(false);
                    ImageFrame.this.dispose();
                }
            });        l = new ImagePanel();
            l.setLayout(new BorderLayout());        jx = new JButton("Change");        jtf = new JTextField(6);        pjx = new Panel();        JButton b1 = new JButton("OPEN");
            b1.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {
                    // TODO 自動生成されたメソッド・スタブ
                    FileDialog d = new FileDialog(ImageFrame.this, "Load", FileDialog.LOAD);
                    //d.setSize(300,300);
                    d.setLocation(100, 100);
                    d.show();                String dir = d.getDirectory();
                    String fileName = d.getFile();                if (fileName != null) {
                        if (!(fileName.endsWith(".gif") || fileName.endsWith(".GIF"))) {
                            JOptionPane jd = new JOptionPane("Error", JOptionPane.INFORMATION_MESSAGE);
                            jd.showMessageDialog(ImageFrame.this, "Please Select a gif/GIF file");                        this.actionPerformed(e);
                        } else {
                            try {
                                //                            Graphics g=l.getGraphics();
                                //                            BufferedImage image=(BufferedImage) Toolkit.getDefaultToolkit().getImage(dir+fileName);
                                //                            System.out.println(image.getHeight());
                                //                            l.paint(g,image,200,200);                            //                            p.add(l,"Center");
                                //                            c.add(p);
                                //                            ImageFrame.this.setVisible(true);                            //p.add(l,"Center");
                                //c.add(p);                            image = ImageIO.read(new File(dir + fileName));
                                JLabel jl = new JLabel(new ImageIcon(image));
                                l.removeAll();
                                l.add(new Label(""), "North");
                                l.add(jl, "Center");
                                JPanel px = new JPanel();
                                px.setLayout(new BorderLayout());
                                px.add(new JLabel("     X:" + image.getHeight() + "     Y:" + image.getWidth()), "Center");
                                pjx.add(jtf);
                                pjx.add(jx);
                                px.add(pjx, "South");
                                pl.add(px, "East");
                                ImageFrame.this.setVisible(true);
                            } catch (Exception x) {
                                System.out.println(x);
                            }
                        }
                    } else {
                        return;
                    }
                }        });
            JButton b2 = new JButton("SAVE");
            b2.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {
                    // TODO 自動生成されたメソッド・スタブ
                    FileDialog d = new FileDialog(ImageFrame.this, "Save", FileDialog.SAVE);
                    //d.setSize(300,300);
                    d.setLocation(100, 100);
                    d.show();                String dir = d.getDirectory();
                    String fileName = d.getFile();                try {
                        ImageIO.write(image, "GIF", new File(dir + fileName));
                        System.out.println("ok");
                    } catch (IOException e1) {
                        // TODO 自動生成された catch ブロック
                        e1.printStackTrace();
                    }
                }        });        jx.addActionListener(new ActionListener() {            public void actionPerformed(ActionEvent e) {
                    // TODO 自動生成されたメソッド・スタブ
                    if (image != null) {
                        String value = jtf.getText();
                        Graphics2D g = image.createGraphics();
                        //                    System.out.print(image.);
                        //                    System.out.println("     "+image.getMinY());
                        //Graphics g=image.getGraphics();
                        //g.setFont(new Font("宋体", Font.PLAIN,   18));                    g.drawString(value, 50, 100);                    //image.flush();                    g.dispose();
                        JLabel jl = new JLabel(new ImageIcon(image));
                        l.add(jl, "Center");
                        ImageFrame.this.setVisible(true);
                        //ImageFrame.this.setVisible(true);
                    }
                }        });        p = new JPanel();
            p.setBackground(Color.WHITE);
            p.setLayout(new BorderLayout());        pl = new Panel();
            pl.setLayout(new BorderLayout());
            pl.add(l, "Center");
            p.add(pl, "Center");        JPanel p2 = new JPanel();
            //p2.setBackground(Color.WHITE);
            p2.add(b1);
            p2.add(b2);        p.add(p2, "South");        JPanel blank = new JPanel();
            blank.setLayout(new BorderLayout());
            blank.add(new Label(""), "North");
            blank.add(new Label(""), "Center");
            blank.add(new Label(""), "South");        p.add(blank, "North");        c = this.getContentPane();
            c.add(p);        this.setResizable(false);
            this.setSize(800, 600);
            this.setLocation(150, 80);
            this.setVisible(true);
        }}