-3

我正在编写简单的项目来绘制线条并保存喜欢的图像,但是当我运行时,它显示了一个我无法修复的错误。请帮我。

这是我的代码

package image;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Point;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class paint extends JFrame{
private Point points[] = new Point[10000];
private Point pointends[] = new Point[10000];
private int pointCount = 0;
private JButton save_btn;
public paint()
{
    panel paint2 = new panel();
    add(paint2,BorderLayout.CENTER);
}
private class panel extends JPanel
{   

    public panel()
    {   
        setBackground(Color.WHITE);
        save_btn = new JButton();
        save_btn.setText("123");
        this.add(save_btn);
    /*  save btnhandler = new save();
        save_btn.addActionListener(btnhandler);*/
        MouseHandler handler = new MouseHandler();
        this.addMouseMotionListener(handler);

        this.addMouseListener(handler);
    }
    @Override
    protected void paintComponent(Graphics g) 
    {
        // TODO Auto-generated method stub
        super.paintComponent(g);
        for(int i = 0;i <pointCount;i++)
        {   
            g.setColor(Color.RED);
            g.drawLine(points[i].x, points[i].y, pointends[i].x, pointends[i].y);
        }           
    }
}

private class MouseHandler extends MouseAdapter
{  
    @Override
    public void mouseDragged(MouseEvent e) 
    {
        // TODO Auto-generated method stub
            pointends[ pointCount-1] = e.getPoint();
            repaint();


    }
    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
        super.mousePressed(e);
//find point
                    if(pointCount < points.length)
        {
            points[ pointCount ] = e.getPoint();//find point
            pointends[ pointCount ] = e.getPoint();
            pointCount++; 
            repaint();
        }
    }
    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
        super.mouseReleased(e);
        /*pointends[pointCount]=e.getPoint();
        repaint();
        pointCount++;
    */
    }

}

}

和保存事件的类别

package image;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.Buffer;

import javax.imageio.ImageIO;
import javax.swing.JOptionPane;

public class save implements ActionListener{

private paint paint1 = new paint();
@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    String str = JOptionPane.showInputDialog(null, "Enter file name: ");
            //
    BufferedImage bufImage = new BufferedImage(paint1.getWidth(), paint1.getHeight(),BufferedImage.TYPE_INT_RGB);
    try {
        ImageIO.write(bufImage, "jpg", new File(str + ".jpg"));
    } catch (IOException ox) {
        // TODO: handle exception
        ox.printStackTrace();
    }
}

}//end class 
4

1 回答 1

2

问题是您的paintJFrame 创建了 ActionListener 的实例save,而您的saveActionListener 创建了paintJFrame 的实例。因此,您会遇到构造函数的无限循环。

与其创建新paint对象,不如将当前对象传递给save.

private paint paint1 = null;
public save(paint panel) {
    this.paint1 = panel;
}

现在,在您的panel构造函数中,将对当前实例的引用传递给 ActionListener:

save btnhandler = new save(my_paint);     // see Update below
save_btn.addActionListener(btnhandler);

这应该可以解决您的直接问题。但是,我建议您稍微重构一下您的代码,并且您还应该尝试遵循 Java 编码约定,例如,对类使用 CamelCase 名称,并使用正确的缩进。这将使其他人(和您自己)更容易阅读和理解您的代码。

更新:我刚刚意识到你的对象结构有点复杂......你的paintJFrame 创建了一个panelJPanel,它创建了一个saveActionListener,它再次创建了一个paintJFrame。基本参数和解决方案保持不变,但new save(this)您必须传递对包含 JPanel 的 JFrame 的引用,或者更改 ActionListener 中字段的类型,而不是使用。

或者,您可以同时创建 JFrame 的 JPanel 和 ActionListener 内部类paint。通过这种方式,您可以直接从 ActionListener 中访问 JFrame,而根本不必传递引用,从而完全规避了这个问题,并为代码提供了更多的结构。

于 2013-10-10T17:36:21.360 回答