
Originariamente inviata da
andbin
Dipende cosa/dove/come disegni .... ad esempio: disegni su una immagine (BufferedImage) che tieni in memoria? Allora potrebbe essere condivisa tra i vari frame. Senza sapere cosa hai fatto non posso dire più di tanto ....
si ho bufferedimage
codice:
public PaintPanel(String titolo) {
setPreferredSize(new Dimension(SIZE, SIZE));
setBackground(Color.WHITE);
addMouseMotionListener(this);
addMouseListener(this);
this.titolo = titolo;
}
public void paintComponent(Graphics g) {
super.paintComponents(g);
Graphics2D g2 = (Graphics2D) g;
if (_bufImage == null) {
int w = this.getWidth();
int h = this.getHeight();
_bufImage = (BufferedImage) this.createImage(w, h);
Graphics2D gc = _bufImage.createGraphics();
gc.setColor(COLOR_BACKGROUND);
gc.fillRect(0, 0, w, h);
}
g2.drawImage(_bufImage, null, 0, 0);
}
private void drawCurrentShape(Graphics2D g2) {
g2.setColor(COLOR_FOREGROUND);
g2.drawLine(x, y, x, y);
}
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
drawCurrentShape(_bufImage.createGraphics());
percorso = percorso.concat(String.valueOf(x).concat(",")
.concat(String.valueOf(y)).concat(","));
repaint();
}
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
percorso = String.valueOf(x).concat(",").concat(String.valueOf(y))
.concat(",");
}
public void mouseReleased(MouseEvent e) {
percorso = percorso.substring(0, percorso.length() - 1);
Stub.out.println("insert_way " + percorso + " " + titolo);
System.out.println(percorso);
System.out.println(titolo);
}
salvo il percorso che disegno su un database ogni volta che rilascio il mouse..
e questi punti devono essere riprodotti su altri frame