Ok, ho apportato delle modifiche al codice, e ora vi scrivo tutto.
Questa e' la classe Time:
codice:
public class Time {
DrawPanel drwPanel;
int hours;
int minutes;
int seconds;
public void haveTime() { //Ottiene l'ora dal sistema
Calendar c = new GregorianCalendar();
this.hours = c.get(Calendar.HOUR_OF_DAY);
this.minutes = c.get(Calendar.MINUTE);
this.seconds = c.get(Calendar.SECOND);
this.drwPanel.repaint();
}
public void clearTime() { //Azzera l'orologio
this.hours = 0;
this.minutes = 0;
this.seconds = 0;
}
public void setDrawPanel(DrawPanel drwPanel) {
this.drwPanel = drwPanel;
}
}
Questa e' la classe RandomAction:
codice:
public class RandomAction extends Thread {
boolean pause = false;
int time = 1000;
int hours;
int minutes;
int seconds;
Time t;
public RandomAction(Time t) {
this.t = t;
}
@Override
public void run() {
while(true) {
try {
if(pause) {
Thread.sleep(time);
System.out.println("ciao");
t.haveTime();
}
} catch (InterruptedException ex) {
Logger.getLogger(RandomAction.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Questa e' la classe DrawPanel, che contiene la paintcomponent e che si occupa di calcolare la posizione delle lancette e di disegnarle, utilizzo settori circolari per disegnare le lancette, non perche' sono masochista, ma perche' la traccia d'esame lo richiede:
codice:
public class DrawPanel extends JPanel {
Time t;
String figure = "-";
int X;
int Y;
int pointerh;
int pointerm;
int pointers;
DrawPanel(Time t) {
this.setTime(t);
this.setVisible(true);
}
protected void setTime(Time t) {
this.t = t;
}
protected void getCenter(int x, int y) {
this.X = x/2;
this.Y = y/2;
}
@Override
protected void paintComponent(Graphics g) {
int width = getSize().width;
int height = getSize().height;
this.getCenter(width, height);
g.setColor(Color.lightGray);
g.fillRect(0, 0, width, height);
g.setColor(Color.black);
g.drawLine(X - width, Y, X + width, Y);
g.drawLine(X, Y - height, X, Y + height);
if(this.figure == "Square") {
g.setColor(Color.black);
g.fillRect(X - 160, Y - 160, 320, 320);
g.setColor(Color.lightGray);
g.fillRect(X - 158, Y - 158, 316, 316);
g.setColor(Color.red);
g.fillArc(X - 150, Y - 150, 300, 300, 87 - Math.abs(this.t.seconds) * 6, 5);
g.setColor(Color.green);
g.fillArc(X - 150, Y - 150, 300, 300, 87 - Math.abs(this.t.minutes) * 6, 5);
g.setColor(Color.blue);
g.fillArc(X - 150, Y - 150, 300, 300, 87 - Math.abs(this.t.hours) * 30, 5);
}
else if(this.figure == "Circle") {
g.setColor(Color.black);
g.fillOval(X - 150, Y - 150, 300, 300);
g.setColor(Color.lightGray);
g.fillOval(X - 148, Y - 148, 296, 296);
g.setColor(Color.red);
g.fillArc(X - 150, Y - 150, 300, 300, 87 - Math.abs(this.t.seconds) * 6, 5);
g.setColor(Color.green);
g.fillArc(X - 150, Y - 150, 300, 300, 87 - Math.abs(this.t.minutes) * 6, 5);
g.setColor(Color.blue);
g.fillArc(X - 150, Y - 150, 300, 300, 87 - Math.abs(this.t.hours) * 30, 5);
}
}
}
Infine questo e' l'AutoPanel, il pannello che si occupa di avviare il thread, ma che non capisco il perche' non funziona:
codice:
public class AutoPanel extends JPanel implements ActionListener {
DrawPanel drwPanel;
RandomAction th;
Time t;
JButton start;
boolean doRandomAction = false;
Panel myPanel;
AutoPanel(Time t, DrawPanel drwPanel) {
this.set(drwPanel, t);
start = new JButton("Avvia");
start.addActionListener(this);
this.add(start);
this.setVisible(false);
th = new RandomAction(t);
th.start();
}
public void set(DrawPanel drwPanel, Time t) {
this.drwPanel = drwPanel;
this.t = t;
}
@Override
public void actionPerformed(ActionEvent ae) {
if(this.drwPanel.figure != "-") {
this.th.pause = true; //qui e' dove modifico la variabile pause
System.out.println(this.th.pause);
}
else JOptionPane.showMessageDialog(this, "Errore Selezione: \n"
+ "Selezionare il tipo di quadrante per l'orologio");
}
}
Quando metto la variabile pause = true, il thread non funziona, non si avvia nulla, e' come se nel ciclo while della classe RandomAction, non ci entrasse proprio.
Perche'? Dove sbaglio?
Da notare che se io metto pause = true dal codice e poi compilo ed eseguo, funziona tutto alla perfezione.