2012-12-18 3 views
1

java 소켓을 사용하여 드로잉 프로그램을 개발 중입니다. 여러 사용자가 그려서 jpeg로 저장할 수 있습니다. 현재 내 이미지 저장 기능은 빈 캔버스 만 저장합니다. 그것은 그려진 좌표를 저장할 수 없습니다.Jpanel의 그림을 이미지로 저장

아래 내 코드의 일부를 공유하고 있습니다. =)

자바 소켓 사용으로 인해 캔버스 클래스에 페인트 또는 paintComponent를 사용하지 않았습니다. 좌표 오류를 보내고 있습니다. 대신 나는 massDraw()를 사용하고 있습니다.

class Canvas extends JPanel { 
    private int x, y; 
    private float x2, y2; 

    public Canvas() { 
     super(); 
     this.setBackground(Color.white); 
    } 

    public void massDraw(int px, int py, int x, int y, int red, int green, 
      int blue, int size) { 
     Graphics g = canvas.getGraphics(); 
     Graphics2D g2d = (Graphics2D) g; 
     g2d.setRenderingHints(myBrush); 

     g2d.setStroke(new BasicStroke(size, BasicStroke.CAP_ROUND, 
       BasicStroke.JOIN_BEVEL)); 
     g2d.setColor(new Color(red, green, blue)); 
     g.drawLine(px, py, x, y); 

    } 


}// end Canvas class 

SaveJpegOP 클래스

class saveJpegOP implements ActionListener { 

    public void actionPerformed(ActionEvent e) { 
     // Ask for file name 
     String str = JOptionPane 
       .showInputDialog(null, "Enter File Name : "); 
     // save as jpeg 
      BufferedImage bufImage = new BufferedImage(canvas.getSize().width, canvas.getSize().height,BufferedImage.TYPE_INT_RGB); 
      canvas.paint(bufImage.createGraphics()); 



     try { 
      ImageIO.write(bufImage, "jpg", new File(str + ".jpg")); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 

    } 

} 
+0

여기서 'massDraw'를 (를) 호출 하시겠습니까? 변수'canvas'는 무엇입니까? 'getGraphics()'를 결코 호출하면 안된다. 이것은 항상 당신이 설명하는 문제로 이어진다. 'paintComponent'를 오버라이드 할 수없는 이유를 절대 이해할 수 없습니다. –

+0

1. 다른 공용 클래스 (클라이언트) 파일에서 massDraw가 호출됩니다. 클라이언트가 좌표를 받으면 다른 사용자의 캔버스에 점을 그립니다. –

+0

2. 캔버스는 캔버스 캔버스 = 새로운 캔버스(); –

답변

1

빈 캔버스는 saveJpegOPcanvas.paint(bufImage.createGraphics())를 호출 할 때, 특히 그것이라고 아니에요, massDraw()가 호출되지 않습니다 때문에 저장됩니다.

paint() 기본적으로 는 전체 구성 요소를 다시 그립니다 당신이 결정 이후 (또는 paintComponent()가), drawMass()가 호출되지 않습니다 빈 캔버스가 그려진 무시할 수 없습니다.

따라서 paintComponent()을 무시하고 적절한 매개 변수를 사용하여 massDraw()을 호출해야합니다. 매개 변수 값은 예를 들어 Canvas 클래스의 속성으로 일찍 설정 될 수 있습니다.