我正在编写简单的项目来绘制线条并保存像图像,但是当我运行时,它会显示我无法解决的错误。请帮帮我。
这是我的代码:
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
答案 0 :(得分:2)
问题是,您的paint
JFrame会创建save
ActionListener的实例,而您的save
ActionListener会创建paint
JFrame的实例。因此,您会遇到无限循环的构造函数。
不是创建新的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名称,并使用正确的缩进。这将使其他人(和您自己)更容易阅读和处理您的代码。
更新:我刚刚意识到您的对象结构有点复杂......您的paint
JFrame会创建一个panel
JPanel,会创建一个{{1} ActionListener,它再次创建一个save
JFrame。基本参数和解决方案保持不变,但不是使用paint
,而是必须传递对包含JPanel的JFrame的引用,或者更改ActionListener中字段的类型。
或者,您可以在new save(this)
JFrame的JPanel和ActionListener内部类中进行操作。这样,您可以直接从ActionListener中访问JFrame,而不必传递引用,从而完全避开问题并为代码提供更多结构。