1

我正在加载一个 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) {
    }

    {
    }
}
4

3 回答 3

1

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

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

编辑:文件分隔符/windows下的错误信息已被删除

于 2012-04-12T18:01:02.727 回答
0

我曾经编写了一些代码来将 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 所说,你的文件路径是可疑的......)

于 2012-04-12T18:20:41.823 回答
0

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

是时候“停止思考”并开始调试了。;)

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. Applet当更好的工具包是 Swing 时,使用 AWT 组件(等)进行编码JApplet
  3. (当自由浮动框架更好时编写小程序)
  4. 调用getGraphics()图形用户界面。
  5. 使用null布局。(按钮的文字已经看起来“拥挤”)
  6. 按钮的蓝色 BG 使文本难以阅读。

..但一次一件事。

于 2012-04-12T19:54:32.840 回答