Codice PHP:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.net.*;
import java.io.*;
import java.applet.*;
import java.awt.Toolkit.*;
//classe gestore eventi mouse
class Mouse extends MouseInputAdapter{
Lavagna lav;
int x;
int y;
int px;
int py;
Color col;
public Mouse(Lavagna lavApp,Color color){
lav=lavApp;
col=color;
x=0;
y=0;
px=0;
py=0;
}
public void mouseReleased(MouseEvent e){
px=0;
py=0;
x=0;
y=0;
}
public void mouseDragged(MouseEvent e){
x=e.getX();
y=e.getY();
if((px!=0)&&(px!=0)){
if(MasterGraph.sts){
lav.erase(px,py,x,y);
}else{
lav.draw(px,py,x,y,col);
}
}
px=x;
py=y;
}
public void setColor(Color color){
col=color;
}
}
// classe finta per catturare l' oggetto Image
class Finta extends Applet{
public void init(){
setBackground(Color.white);
}
}
// classe lavagna elettronica
class Lavagna extends Canvas{
int px;
int py;
int x;
int y;
Color col;
Graphics g;
Image im;
public Lavagna(){
px=0;
py=0;
x=0;
y=0;
col=Color.black;
}
public void initG(Finta applet,int x,int y){
im = applet.createImage(x,y);
g = im.getGraphics();
}
public void draw(int px,int py,int x,int y,Color col){
this.px=px;
this.py=py;
this.x=x;
this.y=y;
this.col=col;
g.setColor(col);
g.drawLine(px,py,x,y);
repaint();
}
public void erase(int px,int py,int x,int y){
g.setColor(Color.white);
g.drawLine(px,py,x,y);
repaint();
}
public void paint(Graphics g){
g.drawImage(im,0,0,this);
}
}
// classe principale
public class MasterGraph extends JFrame{
static Lavagna lavagna;
Mouse mouse;
Color color;
JButton bRed;
JButton bBlack;
JButton bBlue;
JButton bYellow;
JButton bMagenta;
JButton bGreen;
JButton bGray;
JButton bOrange;
JButton bPink;
JButton bCyan;
JButton jbuSelected;
JButton pp;
JButton pm;
JButton pg;
JToggleButton jtbStatus;
static boolean sts;
public MasterGraph(String title){
super(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Dimension dimm=this.getToolkit().getScreenSize();
setSize(800,600);
setLocation((dimm.width/4)-100,(dimm.height/4)-100);
setResizable(true);
sts=false;
color=Color.black;
Toolkit kit=Toolkit.getDefaultToolkit();
ImageIcon icomatita=new ImageIcon("30.gif");
Image matita=icomatita.getImage();
Cursor cursorematita=kit.createCustomCursor(matita,new Point(), "cursore matita");
setCursor(cursorematita);
jtbStatus=new JToggleButton("GOMMA");
jtbStatus.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(jtbStatus.isSelected()){
jtbStatus.setText("MATITA");
sts=true;
Toolkit kit=Toolkit.getDefaultToolkit();
ImageIcon icogomma=new ImageIcon("gomma.gif");
Image gomma=icogomma.getImage();
Cursor cursoregomma=kit.createCustomCursor(gomma,new Point(), "cursore gomma");
setCursor(cursoregomma);
}else{
jtbStatus.setText("GOMMA");
sts=false;
Toolkit kit=Toolkit.getDefaultToolkit();
ImageIcon icomatita=new ImageIcon("30.gif");
Image matita=icomatita.getImage();
Cursor cursorematita=kit.createCustomCursor(matita,new Point(), "cursore matita");
setCursor(cursorematita);
}
}
});
JPanel jpaStatus=new JPanel();
jpaStatus.add(jtbStatus);
jbuSelected=new JButton();
jbuSelected.setBackground(color);
JPanel jpa=new JPanel();
jpa.add(jbuSelected);
bRed=new JButton();
bRed.setBackground(Color.red);
bRed.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
color=bRed.getBackground();
jbuSelected.setBackground(color);
mouse.setColor(color);
}
});
bBlue=new JButton();
bBlue.setBackground(Color.blue);
bBlue.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
color=bBlue.getBackground();
jbuSelected.setBackground(color);
mouse.setColor(color);
}
});
bBlack=new JButton();
bBlack.setBackground(Color.black);
bBlack.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
color=bBlack.getBackground();
jbuSelected.setBackground(color);
mouse.setColor(color);
}
});
bGray=new JButton();
bGray.setBackground(Color.gray);
bGray.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
color=bGray.getBackground();
jbuSelected.setBackground(color);
mouse.setColor(color);
}
});
bGreen=new JButton();
bGreen.setBackground(Color.green);
bGreen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
color=bGreen.getBackground();
jbuSelected.setBackground(color);
mouse.setColor(color);
}
});
bMagenta=new JButton();
bMagenta.setBackground(Color.magenta);
bMagenta.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
color=bMagenta.getBackground();
jbuSelected.setBackground(color);
mouse.setColor(color);
}
});
bOrange=new JButton();
bOrange.setBackground(Color.orange);
bOrange.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
color=bOrange.getBackground();
jbuSelected.setBackground(color);
mouse.setColor(color);
}
});
bPink=new JButton();
bPink.setBackground(Color.pink);
bPink.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
color=bPink.getBackground();
jbuSelected.setBackground(color);
mouse.setColor(color);
}
});
bYellow=new JButton();
bYellow.setBackground(Color.yellow);
bYellow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
color=bYellow.getBackground();
jbuSelected.setBackground(color);
mouse.setColor(color);
}
});
bCyan=new JButton();
bCyan.setBackground(Color.cyan);
bCyan.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
color=bCyan.getBackground();
jbuSelected.setBackground(color);
mouse.setColor(color);
}
});
pp=new JButton("Px 1");
pp.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
pm=new JButton("Px 2");
pm.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
pg=new JButton("Px 5");
pg.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
JPanel pColor=new JPanel();
pColor.setLayout(new GridLayout(2,5));
pColor.add(bBlack);
pColor.add(bBlue);
pColor.add(bRed);
pColor.add(bGray);
pColor.add(bGreen);
pColor.add(bMagenta);
pColor.add(bOrange);
pColor.add(bPink);
pColor.add(bYellow);
pColor.add(bCyan);
JPanel dimpunt=new JPanel();
dimpunt.add(pp);
dimpunt.add(pm);
dimpunt.add(pg);
JPanel panel=new JPanel();
panel.add(dimpunt);
panel.add(jpaStatus);
panel.add(jpa);
panel.add(pColor);
lavagna= new Lavagna();
Dimension dim=this.getToolkit().getScreenSize();
lavagna.setBackground(Color.white);
Finta f = new Finta();
f.init();
Container contentPane=getContentPane();
contentPane.add(f,"Center");
contentPane.add(panel,"South");
setVisible(true);
lavagna.initG(f,dim.width,dim.height-60);
contentPane.remove(f);
contentPane.add(lavagna);
contentPane.validate();
mouse=new Mouse(lavagna,color);
lavagna.addMouseMotionListener(mouse);
lavagna.addMouseListener(mouse);
}
public static void main(String[] args){
MasterGraph frame=new MasterGraph("JavaElectronicBoard");
}
}