Lezione 8 - Interfacce grafiche e animazioni in Java

Tutti i programmi usati in questa lezione sono disponibili .
In questa lezione abbiamo parlato di:
Esercizio fatto nella lezione:sorgente

import java.awt.*;
import javax.swing.*;
public class Slider extends JApplet {

 Display disegno;
 JSlider s1,s2,s3;
 JPanel p;
 Color c;
 int larghezza,lunghezza;
 
 public void init(){
  c = Color.red;
  disegno = new Display();  
  getContentPane().add(disegno, BorderLayout.CENTER);
  p = new JPanel();
  s1 = new JSlider();
  s2 = new JSlider();
  s3 = new JSlider();
  p.setLayout(new GridLayout(3,1,5,10)); 
  p.add(s1);
  p.add(s2);
  p.add(s3);
  getContentPane().add(p, BorderLayout.SOUTH); 
 }
class Display extends JPanel {
 public void paintComponent(Graphics g){
   super.paintComponent(g);
   larghezza=getSize().width;
   lunghezza=getSize().height;
   g.setColor(c);
   g.fillRect(0,0,larghezza,lunghezza);
  }
 }
 }

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class Slider1 extends JApplet implements ChangeListener {

 Display disegno;
 JSlider s1,s2,s3;
 JPanel p;
 Color c;
 int larghezza,lunghezza;
 
 public void init(){
  c = Color.red;
  disegno = new Display();  
  getContentPane().add(disegno, BorderLayout.CENTER);
  p = new JPanel();
  s1 = new JSlider(0,255,255);
  s1.addChangeListener(this);
  s2 = new JSlider(0,255,0);
  s2.addChangeListener(this);
  s3 = new JSlider(0,255,0);
  s3.addChangeListener(this);
  p.setLayout(new GridLayout(3,1,5,10)); 
  p.add(s1);
  p.add(s2);
  p.add(s3);
  getContentPane().add(p, BorderLayout.SOUTH); 
 }
 public void stateChanged(ChangeEvent evt){
   c = new Color(s1.getValue(),s2.getValue(),s3.getValue());
   disegno.repaint();
   }
class Display extends JPanel {
 public void paintComponent(Graphics g){
   super.paintComponent(g);
   larghezza=getSize().width;
   lunghezza=getSize().height;
   g.setColor(c);
   g.fillRect(0,0,larghezza,lunghezza);
  }
 }
 }


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



  public class Anim extends JApplet implements Runnable {
   
   Display disegno;
   Thread animazione;
   int x,y,r;
   
   public void init() {

     disegno = new Display();
     setContentPane(disegno);
     x = 0; y=10; r=20;
       }

   public void start() {
    if (animazione == null) {
        animazione = new Thread(this);
        animazione.start();
    }
   }

  public void stop() {
     if (animazione != null) {
       animazione = null;
     }
   }

   public void run() {
    while(true) {
      for (x = r; x <= disegno.getSize().width ; x=x+1) {
        disegno.repaint();
        try { Thread.sleep(100);}
       catch (InterruptedException e) { }
 }
 }
 }
   class Display extends JPanel{

    public void  paintComponent(Graphics g) {
      super.paintComponent(g);
      g.setColor(Color.red);
      g.fillOval(x,y,2*r,2*r);
   }
}
}


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;



  public class Anim1 extends JApplet implements Runnable,ActionListener {
   
   Display disegno;
   Thread animazione;
   JButton b1,b2;
   int x,y,r,x0;
   boolean running;
   
   public void init() {

     disegno = new Display();
     getContentPane().setLayout(new BorderLayout());
     getContentPane().add(disegno, BorderLayout.CENTER); 
     b1 = new JButton("Start");
     b1.addActionListener(this);
     getContentPane().add(b1, BorderLayout.NORTH); 
     b2 = new JButton("Stop");
     getContentPane().add(b2, BorderLayout.SOUTH); 
     b2.addActionListener(this);
     
     x = 0; y=10; r=20; x0=r;
     running = true;
       }

   public void start() {
    if (animazione == null) {
        animazione = new Thread(this);
        animazione.start();
    }
   }

  public void stop() {
     if (animazione != null) {
       animazione = null;
     }
   }

   public void run() {
    while(running) {
      for (x = x0; x <= disegno.getSize().width&&running ; x=x+1) {
        disegno.repaint();
        try { Thread.sleep(100);}
       catch (InterruptedException e) { }
 }
 }
      x0 = x;
 }
 public void actionPerformed(ActionEvent evt) {
   if(evt.getSource()==b1){running=true;start();} 
      else {running=false;stop();}
 }
   class Display extends JPanel{

    public void  paintComponent(Graphics g) {
      super.paintComponent(g);
      g.setColor(Color.red);
      g.fillOval(x,y,2*r,2*r);
   }
}
}