Download Lesson_38_Consolidating_code__Java_Applets

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lesson 38:
Software architecture– Java Applets
Software architecture– Java Applets
1. This presentation will focus on the decisions
around software architecture and the decisions
that will drive the code deployment.
2. We will extend the Drawing applet we created
before and take a look on how this code can be
consolidated.
When using applets significant through are needed to determine the class
layout and the deployment of the program files.
In our first example we had three classes that was distributed as:
Web Server
Paint on web.htm
SimplePaintApplet.class
DrawingCanvas2.class
PaintListener2.class
Web user
Web page + applet
Paint on web.htm
SimplePaintApplet.class
Classes being called by applet
This dialogue may be slow
depending on your network speed
An alternate software architecture might have been:
Java Server
This dialogue may still be slow
depending on your network speed
DrawingCanvas2.class
PaintListener2.class
Web Server
Web user
Web page + applet
Paint on web.htm
SimplePaintApplet.class
Paint on web.htm
SimplePaintApplet.class
An fully architected solution with a web and a client server application
leveraging as much as possible of the same code.
Application server
Application user
SimplePaint2.class
SimplePaint2.java
Java Server
DrawingCanvas2.class
PaintListener2.class
Web user
Web Server
Paint on web.htm
SimplePaintApplet.class
Web page + applet
Paint on web.htm
SimplePaintApplet.class
An fully architected solution with a web and a client server application
using much of the same code and a database.
Application server
Application user
SimplePaint2.class
SimplePaint2.java
Java Server
DrawingCanvas2.class
PaintListener2.class
Database Server
Database.mdb
Web user
Web Server
Paint on web.htm
SimplePaintApplet.class
Web page + applet
Paint on web.htm
SimplePaintApplet.class
An web application that execute the code only on the client side. Useful
for “thin-clients” with low network bandwidth.
Web user
Web Server
Paint on web.htm
ConsolidatedSimplePaintApplet.class
Web page + applet
Paint on web.htm
ConsolidatedSimplePaintApplet.class
1. The consolidated applet contains all code classes from
DrawingCanvas2.class and PaintListener2.class.
2. All code not needed for the applet is removed.
A look at the code changes needed to
consolidate the code into one applet
The code from the previous class
The original
code
When using applets singificant throughs are needed to determine the
class layout and the dep[loyment of the program files.
In our first example we had three classes that was distribued as:
Web Server
Paint on web.htm
SimplePaintApplet.class
DrawingCanvas2.class
PaintListener2.class
Web user
Web page + applet
Paint on web.htm
SimplePaintApplet.class
Classes being called by applet
This dialogue may be slow
depending on your network speed
We don’t
need
frames in
an applet
// SimplePaint2.java - drawing with a mouse
import java.awt.*;
import javax.swing.*;
class SimplePaint2{
public static void main (String[] args){
JFrame frame = new JFrame("SimplePaint2");
Container pane = frame.getContentPane();
DrawingCanvas2 canvas = new DrawingCanvas2();
PaintListener2 listener=new PaintListener2();
canvas.addMouseMotionListener(listener);
// SimplePaintApplet.java
//drawing with a mouse on the web
pane.add(canvas);
frame.pack();
frame.show();
import java.awt.*;
import javax.swing.*;
}
}
public class SimplePaintApplet extends JApplet{
public void init(){
Container pane = getContentPane();
DrawingCanvas2 canvas = new DrawingCanvas2();
PaintListener2 listener=new PaintListener2();
canvas.addMouseMotionListener(listener);
pane.add(canvas);
}
}
The original
code
// DrawingCanvas2.java - a blank canvas
import javax.swing.*;
import java.awt.*;
class DrawingCanvas2 extends JComponent{
public Dimension getMinimumSize(){
return new Dimension(SIZE, SIZE);
}
public Dimension getPreferredSize(){
return new Dimension(SIZE, SIZE);
}
private static final int SIZE=500;
}
The original
code
// PaintListener2.java - paints on a DrwaingCanvas2
import java.awt.*;
import java.awt.event.*;
The original
code
class PaintListener2 implements MouseMotionListener{
public void mouseDragged(MouseEvent e) {
DrawingCanvas2 canvas = (DrawingCanvas2)e.getSource();
Graphics g = canvas.getGraphics();
g.fillOval(e.getX()-radius, e.getY() - radius, diameter, diameter);
}
public void mouseMoved(MouseEvent e){}
protected int radius =3;
protected int diameter = radius*2;
}
The HMTL page to embed the JavaApplet
<HTML>
<HEAD>
<TITLE> The drawing on canvas on the web using an applet </TITLE>
</HEAD>
The original
code
<BODY>
<font size="6" color="blue">
<b>Here is Berg's very own Drawing tool</b>
</font>
<p>
<CENTER>
<applet code="SimplePaintApplet.class" WIDTH=300 HEIGHT=200></applet>
<CENTER>
</BODY>
</HTML>
Note: this overrides any preferred and
minimum size in the applet
The files
Compiling
The original
code
Running in a browser
The consolidated applet code
// consolidatedSimplePaintApplet.java - drawing with a mouse on the web
import java.awt.*;
import javax.swing.*;
Libraries needed
import java.awt.event.*;
public class consolidatedSimplePaintApplet extends JApplet implements MouseMotionListener{
public void init(){
Container pane = getContentPane();
consolidatedSimplePaintApplet canvas = new consolidatedSimplePaintApplet();
canvas.addMouseMotionListener(canvas);
pane.add(canvas);
}
public void mouseDragged(MouseEvent e) {
consolidatedSimplePaintApplet canvas = (consolidatedSimplePaintApplet)e.getSource();
Graphics g = canvas.getGraphics(); // PaintListener2.java - paints on a DrwaingCanvas2
g.fillOval(e.getX()-3,e.getY()- 3,6,6); import java.awt.*;
import java.awt.event.*;
}
class PaintListener2 implements MouseMotionListener{
public void mouseDragged(MouseEvent e) {
public void mouseMoved(MouseEvent e){}
DrawingCanvas2 canvas = (DrawingCanvas2)e.getSource();
}
Graphics g = canvas.getGraphics();
// SimplePaintApplet.java
import java.awt.*;
import javax.swing.*;
public class SimplePaintApplet extends JApplet{
public void init(){
Container pane = getContentPane();
DrawingCanvas2 canvas = new DrawingCanvas2();
PaintListener2 listener=new PaintListener2();
canvas.addMouseMotionListener(listener);
pane.add(canvas);
}}
g.fillOval(e.getX()-radius, e.getY() - radius, diameter, diameter);
}
public void mouseMoved(MouseEvent e){}
protected int radius =3;
protected int diameter = radius*2;
}
// DrawingCanvas2.java - a blank canvas
import javax.swing.*;
import java.awt.*;
class DrawingCanvas2 extends JComponent{
public Dimension getMinimumSize(){
return new Dimension(SIZE, SIZE);
}
public Dimension getPreferredSize(){
return new Dimension(SIZE, SIZE);
}
private static final int SIZE=500;
}
// consolidatedSimplePaintApplet.java - drawing with a mouse on the web
import java.awt.*;
The consolidated class needs both the
import javax.swing.*;
listeners and the Japplet (for the GUI components)
import java.awt.event.*;
public class consolidatedSimplePaintApplet extends JApplet implements MouseMotionListener{
public void init(){
Container pane = getContentPane();
consolidatedSimplePaintApplet canvas = new consolidatedSimplePaintApplet();
canvas.addMouseMotionListener(canvas);
pane.add(canvas);
}
public void mouseDragged(MouseEvent e) {
consolidatedSimplePaintApplet canvas = (consolidatedSimplePaintApplet)e.getSource();
Graphics g = canvas.getGraphics(); // PaintListener2.java - paints on a DrwaingCanvas2
g.fillOval(e.getX()-3,e.getY()- 3,6,6); import java.awt.*;
import java.awt.event.*;
}
class PaintListener2 implements MouseMotionListener{
public void mouseDragged(MouseEvent e) {
public void mouseMoved(MouseEvent e){}
DrawingCanvas2 canvas = (DrawingCanvas2)e.getSource();
}
Graphics g = canvas.getGraphics();
// SimplePaintApplet.java
import java.awt.*;
import javax.swing.*;
public class SimplePaintApplet extends JApplet{
public void init(){
Container pane = getContentPane();
DrawingCanvas2 canvas = new DrawingCanvas2();
PaintListener2 listener=new PaintListener2();
canvas.addMouseMotionListener(listener);
pane.add(canvas);
}}
g.fillOval(e.getX()-radius, e.getY() - radius, diameter, diameter);
}
public void mouseMoved(MouseEvent e){}
protected int radius =3;
protected int diameter = radius*2;
}
// DrawingCanvas2.java - a blank canvas
import javax.swing.*;
import java.awt.*;
class DrawingCanvas2 extends JComponent{
public Dimension getMinimumSize(){
return new Dimension(SIZE, SIZE);
}
public Dimension getPreferredSize(){
return new Dimension(SIZE, SIZE);
}
private static final int SIZE=500;
}
// consolidatedSimplePaintApplet.java - drawing with a mouse on the web
import java.awt.*;
import javax.swing.*;
The remains unchanged
import java.awt.event.*;
(we don’t need JFrames)
public class consolidatedSimplePaintApplet extends JApplet implements MouseMotionListener{
public void init(){
Container pane = getContentPane();
consolidatedSimplePaintApplet canvas = new consolidatedSimplePaintApplet();
canvas.addMouseMotionListener(canvas);
pane.add(canvas);
}
public void mouseDragged(MouseEvent e) {
consolidatedSimplePaintApplet canvas = (consolidatedSimplePaintApplet)e.getSource();
Graphics g = canvas.getGraphics(); // PaintListener2.java - paints on a DrwaingCanvas2
g.fillOval(e.getX()-3,e.getY()- 3,6,6); import java.awt.*;
import java.awt.event.*;
}
class PaintListener2 implements MouseMotionListener{
public void mouseDragged(MouseEvent e) {
public void mouseMoved(MouseEvent e){}
DrawingCanvas2 canvas = (DrawingCanvas2)e.getSource();
}
Graphics g = canvas.getGraphics();
// SimplePaintApplet.java
import java.awt.*;
import javax.swing.*;
public class SimplePaintApplet extends JApplet{
public void init(){
Container pane = getContentPane();
DrawingCanvas2 canvas = new DrawingCanvas2();
PaintListener2 listener=new PaintListener2();
canvas.addMouseMotionListener(listener);
pane.add(canvas);
}}
g.fillOval(e.getX()-radius, e.getY() - radius, diameter, diameter);
}
public void mouseMoved(MouseEvent e){}
protected int radius =3;
protected int diameter = radius*2;
}
// DrawingCanvas2.java - a blank canvas
import javax.swing.*;
import java.awt.*;
class DrawingCanvas2 extends JComponent{
public Dimension getMinimumSize(){
return new Dimension(SIZE, SIZE);
}
public Dimension getPreferredSize(){
return new Dimension(SIZE, SIZE);
}
private static final int SIZE=500;
}
// consolidatedSimplePaintApplet.java - drawing with a mouse on the web
import java.awt.*;
This is now based on itself (all that is needed from
import javax.swing.*;
DrawingCanvas2 is placed in the same class)
import java.awt.event.*;
public class consolidatedSimplePaintApplet extends JApplet implements MouseMotionListener{
public void init(){
Container pane = getContentPane();
consolidatedSimplePaintApplet canvas = new consolidatedSimplePaintApplet();
canvas.addMouseMotionListener(canvas);
pane.add(canvas);
}
public void mouseDragged(MouseEvent e) {
consolidatedSimplePaintApplet canvas = (consolidatedSimplePaintApplet)e.getSource();
Graphics g = canvas.getGraphics(); // PaintListener2.java - paints on a DrwaingCanvas2
g.fillOval(e.getX()-3,e.getY()- 3,6,6); import java.awt.*;
import java.awt.event.*;
}
class PaintListener2 implements MouseMotionListener{
public void mouseDragged(MouseEvent e) {
public void mouseMoved(MouseEvent e){}
DrawingCanvas2 canvas = (DrawingCanvas2)e.getSource();
}
Graphics g = canvas.getGraphics();
// SimplePaintApplet.java
import java.awt.*;
import javax.swing.*;
public class SimplePaintApplet extends JApplet{
public void init(){
Container pane = getContentPane();
DrawingCanvas2 canvas = new DrawingCanvas2();
PaintListener2 listener=new PaintListener2();
canvas.addMouseMotionListener(listener);
pane.add(canvas);
}}
g.fillOval(e.getX()-radius, e.getY() - radius, diameter, diameter);
}
public void mouseMoved(MouseEvent e){}
protected int radius =3;
protected int diameter = radius*2;
}
// DrawingCanvas2.java - a blank canvas
import javax.swing.*;
import java.awt.*;
class DrawingCanvas2 extends JComponent{
public Dimension getMinimumSize(){
return new Dimension(SIZE, SIZE);
}
public Dimension getPreferredSize(){
return new Dimension(SIZE, SIZE);
}
private static final int SIZE=500;
}
// consolidatedSimplePaintApplet.java - drawing with a mouse on the web
import java.awt.*;
The Mouselistener can be directly associated with
import javax.swing.*;
the canvas since it is no longer in a separate class
import java.awt.event.*;
public class consolidatedSimplePaintApplet extends JApplet implements MouseMotionListener{
public void init(){
Container pane = getContentPane();
consolidatedSimplePaintApplet canvas = new consolidatedSimplePaintApplet();
canvas.addMouseMotionListener(canvas);
pane.add(canvas);
}
public void mouseDragged(MouseEvent e) {
consolidatedSimplePaintApplet canvas = (consolidatedSimplePaintApplet)e.getSource();
Graphics g = canvas.getGraphics(); // PaintListener2.java - paints on a DrwaingCanvas2
g.fillOval(e.getX()-3,e.getY()- 3,6,6); import java.awt.*;
import java.awt.event.*;
}
class PaintListener2 implements MouseMotionListener{
public void mouseDragged(MouseEvent e) {
public void mouseMoved(MouseEvent e){}
DrawingCanvas2 canvas = (DrawingCanvas2)e.getSource();
}
Graphics g = canvas.getGraphics();
// SimplePaintApplet.java
import java.awt.*;
import javax.swing.*;
public class SimplePaintApplet extends JApplet{
public void init(){
Container pane = getContentPane();
DrawingCanvas2 canvas = new DrawingCanvas2();
PaintListener2 listener=new PaintListener2();
canvas.addMouseMotionListener(listener);
pane.add(canvas);
}}
g.fillOval(e.getX()-radius, e.getY() - radius, diameter, diameter);
}
public void mouseMoved(MouseEvent e){}
protected int radius =3;
protected int diameter = radius*2;
}
// DrawingCanvas2.java - a blank canvas
import javax.swing.*;
import java.awt.*;
class DrawingCanvas2 extends JComponent{
public Dimension getMinimumSize(){
return new Dimension(SIZE, SIZE);
}
public Dimension getPreferredSize(){
return new Dimension(SIZE, SIZE);
}
private static final int SIZE=500;
}
// consolidatedSimplePaintApplet.java - drawing with a mouse on the web
import java.awt.*;
Adding the canvas to the container remains unchanged
import javax.swing.*;
import java.awt.event.*;
public class consolidatedSimplePaintApplet extends JApplet implements MouseMotionListener{
public void init(){
Container pane = getContentPane();
consolidatedSimplePaintApplet canvas = new consolidatedSimplePaintApplet();
canvas.addMouseMotionListener(canvas);
pane.add(canvas);
}
public void mouseDragged(MouseEvent e) {
consolidatedSimplePaintApplet canvas = (consolidatedSimplePaintApplet)e.getSource();
Graphics g = canvas.getGraphics(); // PaintListener2.java - paints on a DrwaingCanvas2
g.fillOval(e.getX()-3,e.getY()- 3,6,6); import java.awt.*;
import java.awt.event.*;
}
class PaintListener2 implements MouseMotionListener{
public void mouseDragged(MouseEvent e) {
public void mouseMoved(MouseEvent e){}
DrawingCanvas2 canvas = (DrawingCanvas2)e.getSource();
}
Graphics g = canvas.getGraphics();
// SimplePaintApplet.java
import java.awt.*;
import javax.swing.*;
public class SimplePaintApplet extends JApplet{
public void init(){
Container pane = getContentPane();
DrawingCanvas2 canvas = new DrawingCanvas2();
PaintListener2 listener=new PaintListener2();
canvas.addMouseMotionListener(listener);
pane.add(canvas);
}}
g.fillOval(e.getX()-radius, e.getY() - radius, diameter, diameter);
}
public void mouseMoved(MouseEvent e){}
protected int radius =3;
protected int diameter = radius*2;
}
// DrawingCanvas2.java - a blank canvas
import javax.swing.*;
import java.awt.*;
class DrawingCanvas2 extends JComponent{
public Dimension getMinimumSize(){
return new Dimension(SIZE, SIZE);
}
public Dimension getPreferredSize(){
return new Dimension(SIZE, SIZE);
}
private static final int SIZE=500;
}
// consolidatedSimplePaintApplet.java - drawing with a mouse on the web
import java.awt.*;
The MouseMotionListener interface still requires that
import javax.swing.*;
these methods are defined
import java.awt.event.*;
public class consolidatedSimplePaintApplet extends JApplet implements MouseMotionListener{
public void init(){
Container pane = getContentPane();
consolidatedSimplePaintApplet canvas = new consolidatedSimplePaintApplet();
canvas.addMouseMotionListener(canvas);
pane.add(canvas);
}
public void mouseDragged(MouseEvent e) {
consolidatedSimplePaintApplet canvas = (consolidatedSimplePaintApplet)e.getSource();
Graphics g = canvas.getGraphics(); // PaintListener2.java - paints on a DrwaingCanvas2
g.fillOval(e.getX()-3,e.getY()- 3,6,6); import java.awt.*;
import java.awt.event.*;
}
class PaintListener2 implements MouseMotionListener{
public void mouseDragged(MouseEvent e) {
public void mouseMoved(MouseEvent e){}
DrawingCanvas2 canvas = (DrawingCanvas2)e.getSource();
}
Graphics g = canvas.getGraphics();
// SimplePaintApplet.java
import java.awt.*;
import javax.swing.*;
public class SimplePaintApplet extends JApplet{
public void init(){
Container pane = getContentPane();
DrawingCanvas2 canvas = new DrawingCanvas2();
PaintListener2 listener=new PaintListener2();
canvas.addMouseMotionListener(listener);
pane.add(canvas);
}}
g.fillOval(e.getX()-radius, e.getY() - radius, diameter, diameter);
}
public void mouseMoved(MouseEvent e){}
protected int radius =3;
protected int diameter = radius*2;
}
// DrawingCanvas2.java - a blank canvas
import javax.swing.*;
import java.awt.*;
class DrawingCanvas2 extends JComponent{
public Dimension getMinimumSize(){
return new Dimension(SIZE, SIZE);
}
public Dimension getPreferredSize(){
return new Dimension(SIZE, SIZE);
}
private static final int SIZE=500;
}
// consolidatedSimplePaintApplet.java - drawing with a mouse on the web
import java.awt.*;
This is now based on itself (all that is needed from
import javax.swing.*;
DrawingCanvas2 and PaintListener2 are placed in the same class)
import java.awt.event.*;
public class consolidatedSimplePaintApplet extends JApplet implements MouseMotionListener{
public void init(){
Container pane = getContentPane();
consolidatedSimplePaintApplet canvas = new consolidatedSimplePaintApplet();
canvas.addMouseMotionListener(canvas);
pane.add(canvas);
}
public void mouseDragged(MouseEvent e) {
consolidatedSimplePaintApplet canvas = (consolidatedSimplePaintApplet)e.getSource();
Graphics g = canvas.getGraphics(); // PaintListener2.java - paints on a DrwaingCanvas2
g.fillOval(e.getX()-3,e.getY()- 3,6,6); import java.awt.*;
import java.awt.event.*;
}
class PaintListener2 implements MouseMotionListener{
public void mouseDragged(MouseEvent e) {
public void mouseMoved(MouseEvent e){}
DrawingCanvas2 canvas = (DrawingCanvas2)e.getSource();
}
Graphics g = canvas.getGraphics();
// SimplePaintApplet.java
import java.awt.*;
import javax.swing.*;
public class SimplePaintApplet extends JApplet{
public void init(){
Container pane = getContentPane();
DrawingCanvas2 canvas = new DrawingCanvas2();
PaintListener2 listener=new PaintListener2();
canvas.addMouseMotionListener(listener);
pane.add(canvas);
}}
g.fillOval(e.getX()-radius, e.getY() - radius, diameter, diameter);
}
public void mouseMoved(MouseEvent e){}
protected int radius =3;
protected int diameter = radius*2;
}
// DrawingCanvas2.java - a blank canvas
import javax.swing.*;
import java.awt.*;
class DrawingCanvas2 extends JComponent{
public Dimension getMinimumSize(){
return new Dimension(SIZE, SIZE);
}
public Dimension getPreferredSize(){
return new Dimension(SIZE, SIZE);
}
private static final int SIZE=500;
}
// consolidatedSimplePaintApplet.java - drawing with a mouse on the web
import java.awt.*;
Using the Graphics library from awt is still the same
import javax.swing.*;
import java.awt.event.*;
public class consolidatedSimplePaintApplet extends JApplet implements MouseMotionListener{
public void init(){
Container pane = getContentPane();
consolidatedSimplePaintApplet canvas = new consolidatedSimplePaintApplet();
canvas.addMouseMotionListener(canvas);
pane.add(canvas);
}
public void mouseDragged(MouseEvent e) {
consolidatedSimplePaintApplet canvas = (consolidatedSimplePaintApplet)e.getSource();
Graphics g = canvas.getGraphics(); // PaintListener2.java - paints on a DrwaingCanvas2
g.fillOval(e.getX()-3,e.getY()- 3,6,6); import java.awt.*;
import java.awt.event.*;
}
class PaintListener2 implements MouseMotionListener{
public void mouseDragged(MouseEvent e) {
public void mouseMoved(MouseEvent e){}
DrawingCanvas2 canvas = (DrawingCanvas2)e.getSource();
}
Graphics g = canvas.getGraphics();
// SimplePaintApplet.java
import java.awt.*;
import javax.swing.*;
public class SimplePaintApplet extends JApplet{
public void init(){
Container pane = getContentPane();
DrawingCanvas2 canvas = new DrawingCanvas2();
PaintListener2 listener=new PaintListener2();
canvas.addMouseMotionListener(listener);
pane.add(canvas);
}}
g.fillOval(e.getX()-radius, e.getY() - radius, diameter, diameter);
}
public void mouseMoved(MouseEvent e){}
protected int radius =3;
protected int diameter = radius*2;
}
// DrawingCanvas2.java - a blank canvas
import javax.swing.*;
import java.awt.*;
class DrawingCanvas2 extends JComponent{
public Dimension getMinimumSize(){
return new Dimension(SIZE, SIZE);
}
public Dimension getPreferredSize(){
return new Dimension(SIZE, SIZE);
}
private static final int SIZE=500;
}
// consolidatedSimplePaintApplet.java - drawing with a mouse on the web
import java.awt.*;
Since the applet will be compiled and no modifications or access functions to
import javax.swing.*;
radius and diameter will occur, we can use numbers instead of variables
import java.awt.event.*;
public class consolidatedSimplePaintApplet extends JApplet implements MouseMotionListener{
public void init(){
Container pane = getContentPane();
consolidatedSimplePaintApplet canvas = new consolidatedSimplePaintApplet();
canvas.addMouseMotionListener(canvas);
pane.add(canvas);
}
public void mouseDragged(MouseEvent e) {
consolidatedSimplePaintApplet canvas = (consolidatedSimplePaintApplet)e.getSource();
Graphics g = canvas.getGraphics(); // PaintListener2.java - paints on a DrwaingCanvas2
g.fillOval(e.getX()-3,e.getY()- 3,6,6); import java.awt.*;
import java.awt.event.*;
}
class PaintListener2 implements MouseMotionListener{
public void mouseDragged(MouseEvent e) {
public void mouseMoved(MouseEvent e){}
DrawingCanvas2 canvas = (DrawingCanvas2)e.getSource();
}
Graphics g = canvas.getGraphics();
// SimplePaintApplet.java
import java.awt.*;
import javax.swing.*;
public class SimplePaintApplet extends JApplet{
public void init(){
Container pane = getContentPane();
DrawingCanvas2 canvas = new DrawingCanvas2();
PaintListener2 listener=new PaintListener2();
canvas.addMouseMotionListener(listener);
pane.add(canvas);
}}
g.fillOval(e.getX()-radius, e.getY() - radius, diameter, diameter);
}
public void mouseMoved(MouseEvent e){}
protected int radius =3;
protected int diameter = radius*2;
}
// DrawingCanvas2.java - a blank canvas
import javax.swing.*;
import java.awt.*;
class DrawingCanvas2 extends JComponent{
public Dimension getMinimumSize(){
return new Dimension(SIZE, SIZE);
}
public Dimension getPreferredSize(){
return new Dimension(SIZE, SIZE);
}
private static final int SIZE=500;
}
// consolidatedSimplePaintApplet.java - drawing with a mouse on the web
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class consolidatedSimplePaintApplet extends JApplet implements MouseMotionListener{
public void init(){
Container pane = getContentPane();
consolidatedSimplePaintApplet canvas = new consolidatedSimplePaintApplet();
canvas.addMouseMotionListener(canvas);
pane.add(canvas);
}
public void mouseDragged(MouseEvent e) {
consolidatedSimplePaintApplet canvas = (consolidatedSimplePaintApplet)e.getSource();
Graphics g = canvas.getGraphics();
g.fillOval(e.getX()-3,e.getY()- 3,6,6);
}
public void mouseMoved(MouseEvent e){}
Since the viewer will take charge of the applet
}
size, the setting of minumumsize and
preferredsize are not needed
Web page
/<HTML>
<HEAD>
<TITLE> The drawing on canvas on the web using an applet </TITLE>
</HEAD>
<BODY>
<font size="6" color="blue">
<b>Here is Berg's very own Drawing tool</b>
</font>
<p>
<CENTER>
<applet code="consolidatedSimplePaintApplet.class" WIDTH=300
HEIGHT=200></applet>
<CENTER>
</BODY>
</HTML>
// DrawingCanvas2.java - a blank canvas
import javax.swing.*;
import java.awt.*;
class DrawingCanvas2 extends JComponent{
public Dimension getMinimumSize(){
return new Dimension(SIZE, SIZE);
}
public Dimension getPreferredSize(){
return new Dimension(SIZE, SIZE);
}
private static final int SIZE=500;
}
// consolidatedSimplePaintApplet.java - drawing with a mouse on the web
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class consolidatedSimplePaintApplet extends JApplet implements MouseMotionListener{
public void init(){
Container pane = getContentPane();
consolidatedSimplePaintApplet canvas = new consolidatedSimplePaintApplet();
canvas.addMouseMotionListener(canvas);
pane.add(canvas);
}
public void mouseDragged(MouseEvent e) {
consolidatedSimplePaintApplet canvas = (consolidatedSimplePaintApplet)e.getSource();
Graphics g = canvas.getGraphics();
g.fillOval(e.getX()-3,e.getY()- 3,6,6);
}
public void mouseMoved(MouseEvent e){}
An web application that execute the code only on the client side. Useful
}
for “thin-clients” with low network bandwidth.
The result
Web user
Web Server
Paint on web.htm
ConsolidatedSimplePaintApplet.class
Web page + applet
Paint on web.htm
ConsolidatedSimplePaintApplet.class
1. The consolidated applet contains all code classes from
DrawingCanvas2.class and PaintListener2.class.
2. All code not needed for the applet is removed.
Modifying the applet
// consolidatedSimplePaintApplet.java - drawing with a mouse on the web
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class consolidatedSimplePaintApplet extends JApplet implements MouseMotionListener{
public void init(){
Container pane = getContentPane();
consolidatedSimplePaintApplet canvas = new consolidatedSimplePaintApplet();
canvas.addMouseMotionListener(canvas);
pane.add(canvas);
}
public void mouseDragged(MouseEvent e) {
consolidatedSimplePaintApplet canvas = (consolidatedSimplePaintApplet)e.getSource();
Graphics g = canvas.getGraphics();
g.fillOval(e.getX()-3,e.getY()- 3,6,6);
}
public void mouseMoved(MouseEvent e){}
}
Changing pen color and paint size
// consolidatedSimplePaintApplet.java - drawing with a mouse on the web
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class consolidatedSimplePaintApplet extends JApplet implements MouseMotionListener{
public void init(){
Container pane = getContentPane();
consolidatedSimplePaintApplet canvas = new consolidatedSimplePaintApplet();
canvas.addMouseMotionListener(canvas);
pane.add(canvas);
}
public void mouseDragged(MouseEvent e) {
consolidatedSimplePaintApplet canvas = (consolidatedSimplePaintApplet)e.getSource();
Graphics g = canvas.getGraphics();
g.setColor (Color.red);
g.fillOval(e.getX()-3,e.getY()- 3,4,4);
}
public void mouseMoved(MouseEvent e){}
}
// consolidatedSimplePaintApplet.java - drawing with a mouse on the web
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class consolidatedSimplePaintApplet extends JApplet implements MouseMotionListener{
public void init(){
Container pane = getContentPane();
consolidatedSimplePaintApplet canvas = new consolidatedSimplePaintApplet();
canvas.addMouseMotionListener(canvas);
pane.add(canvas);
}
public void mouseDragged(MouseEvent e) {
consolidatedSimplePaintApplet canvas = (consolidatedSimplePaintApplet)e.getSource();
Graphics g = canvas.getGraphics();
g.setColor (Color.red);
g.fillOval(e.getX()-3,e.getY()- 3,4,4);
}
public void mouseMoved(MouseEvent e){}
}
An web application that execute the code only on the client side. Useful
for “thin-clients” with low network bandwidth.
Web user
Web Server
Paint on web.htm
ConsolidatedSimplePaintApplet.class
Web page + applet
Paint on web.htm
ConsolidatedSimplePaintApplet.class
1. The consolidated applet contains all code classes from
DrawingCanvas2.class and PaintListener2.class.
2. All code not needed for the applet is removed.
Lessons Learned
1. We need consider the application layout and reusable code
2. Consolidated applets have a very high cost of ownership (code is
copied in each applet and is therefore hard to modify)
3. Consolidated applet should be created with caution
4. Applet can be made really small if your write only the code needed
to make it work. However, applet can be very complex.
The architecture of the system should drive design, not bandwidth
Related documents