无法使用ImageIO.write从applet保存BufferedImage

时间:2012-04-12 17:57:49

标签: java image applet nullpointerexception

我正在加载BufferedImage并在其上绘制一个矩形。然后我想将结果保存为png。但是使用ImageIO.write无法保存图像。我不认为我正确地绘制图像。我目前的代码如下:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.io.*;
import java.sql.*;
import java.awt.Graphics2D;

public class goal extends Applet implements MouseListener, ActionListener {

    Connection connection = null;
    BufferedImage img = null;
    Label ld;
    URL url;
    int x, y, w, h, entry, x2, y2, w2, h2;
    double temp;
    String id;
    TextField percent;
    Button enter;
    Boolean save = false;
    File outputfile = new File("C:/java temp/saved.png");

    public void init() {
        Graphics g = getGraphics();
        addMouseListener(this);
        this.setLayout(null);

        x = 87;
        y = 461;
        w = 22;
        h = 0;
        percent = new TextField();
        percent.setBounds(10, 10, 50, 30);
        this.add(percent);
        percent.setVisible(true);
        percent.addActionListener(this);

        enter = new Button("ENTER");
        enter.setBounds(65, 10, 50, 30);
        enter.addActionListener(this);
        this.add(enter);
        enter.setBackground(Color.blue);
        enter.setVisible(true);

        id = ("sales-goal.png");
        try {
            URL url = new URL(getCodeBase(), id);
            img = ImageIO.read(url);
        } catch (IOException e) {
        }

    }

    public void paint(Graphics g) {
        // Graphics2D g=img.createGraphics();
        g.drawImage(img, 10, 10, this);
        // g.drawImage(img,null,10,10);

        Color myColor = Color.decode("#32004b");
        g.setColor(myColor);
        g.fillRect(x, y, w, h);
        // g.fillRect(83,451,26,10);
        if (entry >= 60) {
            g.fillRect(x2, y2, w2, h2);

        }
    }

    public void actionPerformed(ActionEvent e) {
        Graphics g = getGraphics();

        if (e.getSource() == percent) {
            entry = Integer.parseInt(percent.getText());

            if (entry < 101) {
                y = 461;

                temp = entry;
                temp = temp * 2.65;
                temp = Math.round(temp);
                h = (int) temp;
                y = y - h;
            }
        }

        if (e.getSource() == enter) {
            g.drawString(outputfile + "", 10, 10);
            save = true;
            try {
                ImageIO.write(img, "png", outputfile);
            } catch (IOException i) {
            }
        }
        repaint();
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    {
    }
}

3 个答案:

答案 0 :(得分:1)

您可能还想查看File.Separator。 如果要绘制图像,则必须使用

Graphics2D g=(Graphics2D)img.getGraphics();

编辑:删除了关于文件分隔符/窗口下的错误信息

答案 1 :(得分:0)

我编写了一些代码,用于在applet中保存JPanel的内容一次。希望这会有所帮助:

private void saveView(File saveTo, JPanel view) {

    BufferedImage image = new BufferedImage(view.getPreferredSize().width,
        view.getPreferredSize().height,
        BufferedImage.TYPE_4BYTE_ABGR);

    view.print(image.getGraphics());

    try {
        ImageIO.write(image, "png", saveTo);
    } catch (IOException e) {
        //Handle exception
    }
}

它绝对有效,而且与你自己看起来并不太相似。

也许在e.printStackTrace()中加catch (IOException e) {}来查看是否有异常被抛出?

(正如Vespasian所说,你的文件路径是可疑的......)

答案 2 :(得分:0)

  

我不认为我正确地绘制图像..

时间'停止思考'&amp;开始调试。 ;)

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:204)
    at goal.actionPerformed(goal.java:87) ...

使用此源更改为显示至少一个堆栈跟踪。用户可以在每次捕获时填写printStackTrace()的调用。

// <applet code=goal width=400 height=200></applet>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import javax.swing.*;

import java.io.File;
import java.io.IOException;
import java.net.URL;

public class goal extends Applet implements ActionListener
{
    BufferedImage img=null;
    Label ld;
    URL url;
    int x,y,w,h,entry,x2,y2,w2,h2;
    double temp;
    String id;
    TextField percent;
    Button enter;
    Boolean save=false;
    File outputfile = new File("C:/java temp/saved.png");

    public void init()
    {
        Graphics g= getGraphics();
        this.setLayout(null);

        x=87; y=461; w=22; h=0;
        percent=new TextField();
        percent.setBounds(10,10,50,30);
        this.add(percent);
        percent.setVisible(true);
        percent.addActionListener(this);

        enter=new Button ("ENTER");
        enter.setBounds(65,10,50,30);
        enter.addActionListener(this);
        this.add(enter);
        enter.setBackground(Color.blue);
        enter.setVisible(true);

        id=("sales-goal.png");
        try {
            URL url = new URL(getCodeBase(),id);
            img = ImageIO.read(url);
        }   catch (IOException e) {}
    }

    public void paint(Graphics g)
    {
        g.drawImage(img,10,10,this);

        Color myColor = Color.decode("#32004b");
        g.setColor(myColor);
        g.fillRect(x,y,w,h);
        if(entry>=60)
        {
            g.fillRect(x2,y2,w2,h2);
        }
    }

    public void actionPerformed (ActionEvent e)
    {
        Graphics g= getGraphics();

        if (e.getSource()==percent)
        {
            entry= Integer.parseInt(percent.getText());

            if(entry<101)
            {
                y=461;

                temp=entry;
                temp=temp*2.65;
                temp=Math.round(temp);
                h=(int)temp;
                y=y-h;
            }
        }

        if (e.getSource()==enter)
        {
            JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(img)));
            g.drawString(outputfile+"",10,10);
            save=true;
            try {
                ImageIO.write(img, "png", outputfile);
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
        repaint();
    }
}

进一步说明:

此代码存在NPE以外的问题。

  1. 期望从小程序中保存File
  2. 使用AWT组件编码(Applet等)当更好的工具包是Swing(JApplet)。
  3. (当自由浮动帧更好时编写applet)
  4. 在GUI上调用getGraphics()
  5. 使用null布局。 (按钮的文字看起来很“拥挤”)
  6. 按钮的蓝色BG使文本难以阅读。
  7. ..但一次只有一件事。