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();

編集:ファイルセパレータ/ウィンドウの下の間違った情報が削除されました

于 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) {}

(そして、ウェスパシアヌスが言ったように、あなたのファイルパスは疑わしいです...)

于 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) ...

このソースを使用すると、少なくとも1つのスタックトレースが表示されるように変更されます。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. (フリーフローティングフレームの方が良い場合のアプレットのコーディング)
  4. getGraphics()GUIでの呼び出し。
  5. レイアウトを使用しnullます。(ボタンのテキストはすでに「混雑」しているように見えます)
  6. ボタンの青いBGは、テキストを読みにくくします。

..しかし、一度に1つのこと。

于 2012-04-12T19:54:32.840 に答える