2013-10-10 192 views
-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 
+0

對不起語法錯誤:(但不關心它, –

+0

_it顯示錯誤_這些錯誤是什麼 –

+0

什麼是錯誤 – Joni

回答

1

的問題是,你paint的JFrame創建您的save ActionListener的實例,和你的save的ActionListener創建一個你的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,它創建了一個save的ActionListener,這又創建了一個paint的JFrame。基本參數和解決方案保持不變,但不是使用new save(this),而是必須傳遞對包含JPanel的JFrame的引用,或者更改ActionListener中字段的類型。

或者,您可以製作paint JFrame的JPanel和ActionListener內部類。通過這種方式,您可以直接從ActionListener中訪問JFrame,而不必傳遞引用,從而完全避免了這個問題,併爲代碼提供了更多的結構。

相關問題