Combinando quanto visto finora si può facilmente creare dei pulsanti personalizzati, per esempio:
Andare sopra con il mouse, premere, uscire.
Con l'aggiunta di un MouseListener, questo pulsante risponde agli eventi.
Il codice dell'applet è:
import java.applet.*;
import java.awt.*;
public class Es_OvalButt extends Applet{
OvalButt b1;
public void init(){
setBackground(Color.white);
b1 = new OvalButt(90,90,"Rotondo","Times",15,new Color(150,200,250),new Color(200,0,0));
add(b1);
}
}
L'applet usa la classe OvalButt il cui codice è:
import java.awt.*;
import java.awt.event.*;
class OvalButt extends Canvas implements MouseListener{
int w,h,l,fontsize,tx, ty;
Color bg, fg;
String txt, fonttype;
int stato;
//costruttore
OvalButt(int w, int h, String txt, String fonttype, int fontsize,Color bg, Color fg){
this.w = w;
this.h = h;
this.txt = txt;
this.fonttype = fonttype;
this.fontsize = fontsize;
this.bg = bg;
this.fg = fg;
setSize(w,h);
addMouseListener(this);
stato = 0;
repaint();
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
//bg del canvas
g.setColor(getBackground());
g.fillRect(0,0,w-1,h-1);
//bouton
g.setColor(bg);
g.fillOval(0,0,w-1,h-1);
//texte
if(stato == 0){ g.setFont(new Font(fonttype, Font.BOLD,fontsize));}
else{ g.setFont(new Font(fonttype, Font.PLAIN, fontsize-3));}
int l = (g.getFontMetrics()).stringWidth(txt);
int yh = (g.getFontMetrics()).getHeight();
int yd = (g.getFontMetrics()).getDescent();
tx = (w - l)/2;
ty = (h + yh)/2 - yd -1;
g.setColor(Color.white);
g.drawString(txt,tx+1,ty+1);
g.setColor(fg);
g.drawString(txt, tx, ty);
//bordi
if(stato == 0){
g.setColor(Color.white);
g.drawOval(1,1,w-2,h-2);
g.setColor(Color.black);
g.drawOval(0,0,w-2,h-2);
}
else if(stato == -1){
g.setColor(Color.white);
g.drawOval(1,1,w-2,h-2);
g.setColor(Color.lightGray);
g.drawOval(0,0,w-2,h-2);
}
else if(stato == 1){
Color wcol = new Color(230,230,230);
g.setColor(wcol);
g.drawOval(1,1,w-2,h-2);
Color lG = new Color(50,50,50);
g.setColor(lG);
g.drawOval(0,0,w-2,h-2);
}
}
public void mousePressed(MouseEvent e){
stato = -1;
repaint();
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){
stato = 1;
repaint();
}
public void mouseExited(MouseEvent e){
stato = 0;
repaint();
}
//dimensioni
public Dimension getMinimumSize(){
return new Dimension(w,h);
}
public Dimension getPreferredSize(){
return new Dimension(w,h);
}
}//fine classe
Chiaramente le possibili variazioni sul tema sono praticamente infinite, ognuno deve sperimentare le proprie!
PS: Dimenticavo, ci sono anche le animazioni con i threads, ma questo è un'altra storia...
Per ora, velocemente: per la prima applet, si ridisegnano i puntini con un colore (quasi) casuale, il colore viene cambiato nel metodo run() del thread:
public void run() {
Thread thisThread = Thread.currentThread();
while(runner == thisThread){
colore = new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255));
repaint();
try {
Thread.sleep(1000);
} catch(InterruptedException e) { }
}
}
Per il codice completo della seconda applet, seguire questo link.
Invece, il codice della terza applet si trova qui.
pg 1 pg 2 pg 3 pg 4