mercoledì 24 marzo 2010

Quadrato

import javax.swing.*;
import java.awt.*;
import java.lang.String;

public class Quadrato extends JPanel{
Color colore1=Color.GREEN;
Color colore2=Color.BLUE;
public void setColore1(Color c1){
colore1=c1;
}
public void setColore2(Color c2){
colore2= c2;
}
public void paintComponent(Graphics g){

g.setColor(colore2);
g.drawRect(50,50,200,200);
g.drawLine(100,50,100,250);
g.drawLine(200,50,200,250);
g.drawLine(150,50,150,250);
g.drawLine(250,150,50,150);
g.drawLine(250,100,50,100);
g.drawLine(250,200,50,200);
g.setColor(colore1);
g.fillRect(100,100,100,100);
g.setColor(colore2);
int x=72, y=77;
int yc=80;
String[] caselle=new String[12];
Font f=new Font("Century", Font.BOLD, 14);
g.setFont(f);
for(int i=0;i<12;i++){
caselle[i]=Integer.toString(i+1);
g.drawString(caselle[i], x, y);
g.fillOval(x,yc,5,5);
if(i<3){
x=x+50;
}
else{
if(i<6){
y=y+50;
yc=y+5;
}
else{
if(i<9){
x=x-50;
}
else{
y=y-50;
yc=y+5;
}
}
}
}

}
}

ClienteCompito

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class ClienteCompito {
BufferedReader lettore;
Socket sock;
PrintWriter scrittore;
JTextField t1;
JTextArea a1;
Quadrato q1;
JScrollPane scrollPane;
public void finestra() {
JFrame f1=new JFrame("semplice chat");
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p1=new JPanel();
a1=new JTextArea(20,50);
JButton b1=new JButton("invia il messaggio");
JButton b2=new JButton("Accendi pedine");
JButton b3=new JButton("Spegni pedine");
t1=new JTextField(20);
q1=new Quadrato();
p1.setLayout(new GridLayout(3,1));
scrollPane= new JScrollPane(a1);
scrollPane= new JScrollPane(q1);
p1.add(a1);
p1.add(t1);
p1.add(b1);
p1.add(q1);
p1.add(b2);
p1.add(b3);
b1.addActionListener(new InviaMessaggio());
f1.add(p1);
f1.setSize(500,500);
f1.setVisible(true);
Thread tt=new Thread(new InArrivoDalServer());
creaCollegamento();
tt.start();
}
public class InviaMessaggio implements ActionListener{
public void actionPerformed(ActionEvent e){
try{
System.out.println(t1.getText());
scrittore.println(t1.getText());
scrittore.flush();
} catch (Exception ex){ex.printStackTrace();}
t1.setText("");
}
}
public void creaCollegamento(){
try {
sock=new Socket("192.168.3.18",4242);
lettore=new BufferedReader(new InputStreamReader(sock.getInputStream()));
scrittore=new PrintWriter(sock.getOutputStream());
System.out.println("collegamento stabilito");
} catch (Exception ex){ex.printStackTrace();}
}
public class InArrivoDalServer implements Runnable{
public void run(){
String messaggio;
System.out.println("sono qui");
try {
while ( (messaggio=lettore.readLine())!=null){

a1.append(messaggio+"\n");
System.out.println("leggo il messaggio:"+messaggio);
}
} catch (Exception ex){ex.printStackTrace();}
}
}
public static void main(String[] args){
ClienteCompito cc=new ClienteCompito();
cc.finestra();
}
}