vi metto un po' di codice...
oltre a client e server,creo questo panel
codice:
class PaintPanel extends JPanel implements MouseMotionListener, MouseListener {
private static Color COLOR_FOREGROUND = LoggedMain.colore_lavagna;
private static final Color COLOR_BACKGROUND = Color.white;
private static final int SIZE = 300;
private int x = 10, y = 10;
private String percorso, w_user,titolo;
private BufferedImage _bufImage = null;
public PaintPanel(String titolo) throws Exception{
setPreferredSize(new Dimension(SIZE, SIZE));
setBackground(Color.WHITE);
addMouseMotionListener(this);
addMouseListener(this);
this.titolo = titolo;
Diffusore d = new Diffusore(this);
StringTokenizer stk = new StringTokenizer(titolo,"_");
w_user = stk.nextToken(); // seleziono il nome dell'utente che gestisce la lavagna
}
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(",") // creo il percorso del tratto
.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 mouseMoved(MouseEvent e) {
}
public void mouseClicked(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent e) {
percorso = percorso.substring(0, percorso.length() - 1); //quando rilascio il mouse,inserisco
Stub.out.println("insert_way " + percorso +" "+ w_user +" "+ titolo); //il tratto nella tabella
System.out.println(percorso); //dei tratti comunicando questo compito al server
Stub.out.println("diffondi "+percorso+" "+w_user+" "+COLOR_FOREGROUND);
System.out.println(w_user);
System.out.println(titolo);
}
}
e l'idea era fare un diffusore che dovrei fare anche con il broadcast solo che non so come farlo
codice:
public class Diffusore extends Thread {
boolean first = true;
PaintPanel pp;
public Diffusore(PaintPanel paintpanel) throws Exception {
pp=paintpanel;
this.start();
}
public void run() {
while(true){
try {
System.out.println("avvio "+pp);
String s = Stub.in.readLine();
StringTokenizer stk = new StringTokenizer(s," ");
String cmd = stk.nextToken();
String percorso = stk.nextToken();
String user= stk.nextToken();
String colore= stk.nextToken();
if(cmd.equals("diff")){
System.out.println("diffusore "+percorso);
}
}
catch (Exception e) {
}
}
}
}