Animazione in Java Animazione in Java

Programmazione 2 – 2002/2003
Università degli Studi di Trento
Marco Ronchetti
Lezione 1
Animazione in Java
Problemi che possono nascere,
e loro soluzione
Animazione in Java
Application
MyFrame
(JFrame)
ClockPanel
(JPanel)
1
Programmazione 2 – 2002/2003
Università degli Studi di Trento
Marco Ronchetti
Lezione 1
public class ClockPanel extends JPanel { …
public void paintComponent(Graphics g) {
Date d=new Date();
int sec=d.getSeconds(); OBSOLETO, ma funziona
double angle=sec*Math.PI/30;
int w=this.getWidth();
int h=this.getHeight();
g.setColor(Color.YELLOW);
g.fillRect(0,0,w,h);
g.setColor(Color.GREEN);
g.fillOval(0,0,w,h);
g.setColor(Color.BLACK);
g.drawLine(w/2,h/2,
(int)(w/2*(1+Math.cos(angle)))
,(int)(h/2*(1+Math.sin(angle))));
} …
ClockPanel - paintComponent
public class MyFrame extends JFrame {
//Component initialization
private void jbInit() throws Exception {
contentPane=new ClockPanel();
this.setContentPane(contentPane);
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
while (true) {
try {
ma Sem
NO br
Thread.sleep(1000);
NF ao
}
UN k,
ZI
catch (InterruptedException ex) {
ON
A!
}
contentPane.repaint();
}
}
...
MyFrame – version 1
2
Programmazione 2 – 2002/2003
Università degli Studi di Trento
Marco Ronchetti
Lezione 1
Animazione e Threads in Java
Application
Runner
(Runnable)
MyFrame
(JFrame)
ClockPanel
(JPanel)
public class Runner implements Runnable {
ClockPanel cp;
public Runner(ClockPanel cp) {
this.cp=cp;
}
public void run() {
while (true) {
try {
Thread.sleep(1000);
}
catch (InterruptedException ex) {
}
cp.repaint();
}
}
Runner
}
3
Programmazione 2 – 2002/2003
Università degli Studi di Trento
Marco Ronchetti
Lezione 1
MyFrame – version 2
public class MyFrame extends JFrame {
//Component initialization
private void jbInit() throws Exception {
contentPane=new ClockPanel();
this.setContentPane(contentPane);
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
Runner r=new Runner(contentPane);
new Thread(r).start();
}
...
Applets
For a tutorial, see
http://java.sun.com/docs/books/tutorial/applet/overview/index.html
4
Programmazione 2 – 2002/2003
Università degli Studi di Trento
Marco Ronchetti
Lezione 1
Applets
»
»
Special Java programs (without a “main”)
callable from HTML and executed in a
graphic context.
They can be executed by:
a Java enabled Web Browser;
ad-hoc programs (e.g. Sun AppletViewer).
Applets
Every applet is implemented by creating a
subclass of the Applet class.
The hierarchy determines much of what an
applet can do and how.
5
Programmazione 2 – 2002/2003
Università degli Studi di Trento
Marco Ronchetti
Lezione 1
Applet Lifecycle
An applet can react to major events in the following ways:
init()
It can initialize itself.
It can start running.
start()
It can draw some graphics.
paint()
It can respond to user-generated events (Mouse,
keyboard, menus…).
handleEvent()
It can stop running.
stop()
It can perform a final cleanup, in preparation for being
unloaded.
destroy()
Applet Lifecycle
init()
Multithreading!
Whenever it’s needed,
at lower priority
start()
handleEvent()
stop()
destroy()
paint()
Actually, more threads are active
behind the scenes.
6
Programmazione 2 – 2002/2003
Università degli Studi di Trento
Marco Ronchetti
Lezione 1
HTML 3.2: Java Applet Support
<applet code=“Name.class”height=“150” width=“300”>
<param name=“time” value=“100”>
<param name=“color” value=“red”>
Questo Browser non supporta le applets
</Applet>
ted
a
c
re
Dep
in
4.0
L
HT M
Objects (HTML 4.0)
Viene introdotta la tag OBJECT per fornire al browser
informazioni utili a caricare o visualizzare tipi di dati
non supportati nativamente (applets, plugins,
Controlli Active-X ecc.)
Una eventuale tag PARAM al suo interno permette di
passare parametri alla applet o plugin.
<OBJECT HEIGHT=… WIDTH=… CLASSID=…>
<PARAM NAME=… VALUE=…>
</OBJECT>
7
Programmazione 2 – 2002/2003
Università degli Studi di Trento
Marco Ronchetti
Lezione 1
Applet example
package demoApplet;
import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet {
String var0;
StringBuffer buffer;
//Initialize the applet
public void init() {
buffer = new StringBuffer();
addItem("initializing... ");
}
Applet example
void addItem(String newWord) {
System.out.println(newWord);
buffer.append(newWord);
repaint();
}
public void paint(Graphics g) {
int red = (int)(Math.random() * 255);
int green = (int)(Math.random() * 255);
int blue = (int)(Math.random() * 255);
g.setColor(new Color(red, green, blue));
System.out.println(red+green+blue);
//Draw a Rectangle around the applet's display area.
g.fillRect(0, 0, 100,300);
g.setColor(new Color(0, 0, 0));
//Draw the current string inside the rectangle.
g.drawString(buffer.toString(), 5, 15);
}
}
8
Programmazione 2 – 2002/2003
Università degli Studi di Trento
Marco Ronchetti
Lezione 1
Applet example
//Start the applet
public void start() {
addItem("starting... ");
addItem("Parameter is: "+getParameter("param0"));
}
//Stop the applet
public void stop() {
addItem("stopping... ");
}
//Destroy the applet
public void destroy() {
addItem("destroying... ");
}
HTML page (tag Applet)
<head>
<title>
HTML Test Page
</title>
</head>
<body>
demoApplet.SimpleApplet will appear below
in a Java enabled browser.<br>
<applet
codebase = "."
code = "demoApplet.SimpleApplet.class"
name
= "TestApplet"
width = "400"
height = "300"
hspace = "0"
vspace = "0"
align = "middle"
>
Your browser cannot show applets
<param name = "param0" value = "hello">
</applet>
</body>
</html>
9
Programmazione 2 – 2002/2003
Università degli Studi di Trento
Marco Ronchetti
Lezione 1
HTML page (tag Object)
<head>
<title>
HTML Test Page
</title>
</head>
<body>
demoApplet.SimpleApplet will appear below
in a Java enabled browser.<br>
<object classid="java:demoApplet.SimpleApplet.class">
<param name = "param0" value = "hello" valuetype="data">
Your browser cannot show applets
<param name = "param0" value = "hello">
</object>
</body>
</html>
JApplet
package jappletdemo;
import java.awt.*;
import javax.swing.*;
public class MyApplet extends JApplet {
private JButton hello;
//Initialize the applet
public void init() {
this.setSize(new Dimension(400,300));
hello=new JButton("hello");
JPanel cp=new MyPanel();
this.setContentPane(cp);
cp.setLayout(new BorderLayout());
cp.add(hello, BorderLayout.WEST);
}
class MyPanel extends JPanel {
protected void paintComponent(Graphics g) {
g.drawOval(20, 20, 100, 100);
}
}
}
10