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();
}
}
}
여기서 'massDraw'를 (를) 호출 하시겠습니까? 변수'canvas'는 무엇입니까? 'getGraphics()'를 결코 호출하면 안된다. 이것은 항상 당신이 설명하는 문제로 이어진다. 'paintComponent'를 오버라이드 할 수없는 이유를 절대 이해할 수 없습니다. –
1. 다른 공용 클래스 (클라이언트) 파일에서 massDraw가 호출됩니다. 클라이언트가 좌표를 받으면 다른 사용자의 캔버스에 점을 그립니다. –
2. 캔버스는 캔버스 캔버스 = 새로운 캔버스(); –