Esercitazione6

annuncio pubblicitario
CECCOLINI MATTEO 113514
ESERCITAZIONE 6
DESCRIZIONE DEL PROBLEMA:
Rendere l’applicazione relativa all’esercitazione quattro utilizzabile via internet.
ANALISI:
Per poter rendere l’esercitazione quattro utilizzabile tramite internet ci si avvale delle applet java, ovvero bisogna
modificare la vecchia simulazione, trasformandola in un applet, che non è altro che una applicazione senza main. Si
vuole aggiungere all’applet il numero di iterazioni e un pulsante che faccia partire l’applicazione.
PROGETTO:
1. Un’applet non è altro che una specie di main, la differenza sta nel fatto che al suo posto c’è una classe che
estende la Japplet. Quando l’applet viene lanciata, si invoca automaticamente il metodo init( ), che inizializza
l’applet, inserendo tutto quello che serve: pulsanti, aree di testo, simulazioni ecc. Nel pannello principale
appare un pulsante ‘Start’ e un’ area di testo che mi indica quante iterazioni deve fare la macchina genera
punti. Il numero di iterazioni è modificabile a piacimento, naturalmente ci sono dei controlli che sia veramente
un numero e non per esempio delle lettere. Il pulsante ‘Start’ invece serve come avvio per la simulazione,
viene letto il numero di iterazioni e si disegna di conseguenza la figura rappresentata dalla macchina genera
punti, attraverso il plotter.
2. Nella seconda applet viene inserito un ComboBox che mi permette di selezionare la macchina genera punti
preferita. Come i pulsanti o altri oggetti interattivi, si genera un ActionEvent che dice quale stringa è stata
scelta attraverso il metodo getSelectedItem( ).
IMPLEMENTAZIONE:
//**** IMPLEMENTAZIONE DEL PANNELLLO ****//
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyPanel extends JPanel{
public MyPanel(){
super();
JButton start=new JButton("start");
JTextField text=new JTextField(5);
JLabel lab=new JLabel("Number of iterations:");
setLayout(new BorderLayout());
JPanel p=new JPanel();
p.add(start);
JPanel p1=new JPanel();
add(p,BorderLayout.SOUTH);
p1.add(lab);
p1.add(text);
add(p1,BorderLayout.NORTH);
}
}
//**** APPLET1.JAVA ****//
import tools.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;
public class Applet1 extends JApplet implements ActionListener{
private JTextField text;
private JButton start;
private JLabel lab1;
private int i=100;
private Plotter plotter;
private PointGenerator machine;
public void init(){
JFrame f=new JFrame("Esercitazione 6");
machine = new CircleGenerator();
f.setBounds(10,10,400,120);
MyPanel p = new MyPanel();
Container c = f.getContentPane();
c.add(p);
f.show();
plotter = new DrawPadPlotter();
start.addActionListener(this);
text.addActionListener(this);
}
public void draw(int n){
Simulation simulation=new Simulation();
simulation.setCurrentPlotter(plotter);
simulation.setCurrentPointGenerator(machine);
simulation.setDelay(100);
simulation.reset();
simulation.execute(n);
}
private class MyPanel extends JPanel{
public MyPanel(){
super();
start=new JButton("Start");
text=new JTextField("100",5);
lab1 = new JLabel("Number of iterations:");
setLayout(new BorderLayout());
JPanel p=new JPanel();
p.add(start);
JPanel p1=new JPanel();
add(p,BorderLayout.SOUTH);
p1.add(lab1);
p1.add(text);
add(p1,BorderLayout.NORTH);
}
}
public void actionPerformed(ActionEvent e){
Object premuto=e.getSource();
if(premuto==start)
draw(i);
if(premuto==text){
try{
i=Integer.valueOf(text.getText()).intValue();
}
catch(NumberFormatException e1){
text.setText("100");
i=100;
}
}
}
}
//**** APPLET2.JAVA ****//
import tools.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;
public class Applet2 extends JApplet implements ActionListener{
private JTextField text;
private JButton start;
private JLabel lab1, lab2;
private JComboBox selection;
private int i=100;
private Plotter plotter;
private PointGenerator machine;
public void init(){
JFrame f=new JFrame("Esercitazione 6");
f.setBounds(10,10,400,120);
MyPanel p = new MyPanel();
Container c = f.getContentPane();
c.add(p);
f.show();
plotter = new DrawPadPlotter();
start.addActionListener(this);
text.addActionListener(this);
selection.addActionListener(this);
}
public void draw(int n){
Simulation simulation=new Simulation();
simulation.setCurrentPlotter(plotter);
simulation.setCurrentPointGenerator(machine);
simulation.setDelay(100);
simulation.reset();
simulation.execute(n);
}
private class MyPanel extends JPanel{
public MyPanel(){
super();
start=new JButton("Start");
text=new JTextField("100",5);
lab1 = new JLabel("Number of iterations:");
selection = new JComboBox();
selection.addItem("Triangle Generator");
selection.addItem("Box Generator");
selection.addItem("Circle Generator");
selection.addItem("Spiral Generator");
selection.addItem("Curve Generator 1");
selection.addItem("Curve Generator 2");
setLayout(new BorderLayout());
JPanel p=new JPanel();
p.add(start);
lab2 = new JLabel("Point Generator:");
p.add(lab2);
p.add(selection);
JPanel p1=new JPanel();
add(p,BorderLayout.SOUTH);
p1.add(lab1);
p1.add(text);
add(p1,BorderLayout.NORTH);
}
}
public void actionPerformed(ActionEvent e){
Object premuto=e.getSource();
if(premuto==start)
draw(i);
if(premuto==selection){
String s = (String)selection.getSelectedItem();
if(s=="Triangle Generator")
machine = new TriangleGenerator();
if(s=="Box Generator")
machine = new BoxGenerator();
if(s=="Circle Generator")
machine = new CircleGenerator();
if(s=="Spiral Generator")
machine = new SpiralGenerator();
if(s=="Curve Generator 1")
machine = new CurveGenerator();
if(s=="Curve Generator 2")
machine = new CurveGenerator1();
}
if(premuto==text){
try{
i=Integer.valueOf(text.getText()).intValue();
}
catch(NumberFormatException e1){
text.setText("100");
i=100;
}
}
}
}
//**** IMPLEMENTAZIONE DELLA PAGINA HTML PER L’APPLET 1****//
<HTML><HEAD>
<TITLE> Applet 1 </TITLE>
</HEAD> <BODY>
<APPLET CODE="Applet1.class" WIDTH=300 HEIGHT=100 >
</APPLET>
</BODY>
</HTML>
//**** IMPLEMENTAZIONE DELLA PAGINA HTML PER L’APPLET 2****//
<HTML><HEAD>
<TITLE> Applet 2 </TITLE>
</HEAD> <BODY>
<APPLET CODE="Applet2.class" WIDTH=300 HEIGHT=100 >
</APPLET>
</BODY>
</HTML>
CASI D’USO:
//** APPLET 1 **//
PRIMA
DOPO
Una volta premuto il pulsante ‘Start’ l’applet disegna nella finestra un cerchio, perché è stato scelta la macchina genera
punti CircleGenerator.
//** APPLET 2 **//
L’applet2 è in grado di poter far scegliere all’utente quale macchina genera punti utilizzare.
CONCETTI E TECNICHE ACQUISITE:
Utilizzo delle applet java e riutilizzo dei componenti grafici swing e awt.
Scarica