Transcript Slide 1

Lesson 37:
The Drawing program – Java Applets
The Java
Application
Drawing program
Importing the libraries
// SimplePaint2.java - drawing with a mouse
import java.awt.*;
import javax.swing.*;
Creating a class called SimplePaint2
class SimplePaint2{
Creating a main section
public static void main (String[] args){
JFrame frame = new JFrame("SimplePaint2");
Container pane = frame.getContentPane();
Creating a frame
DrawingCanvas2 canvas = new DrawingCanvas2(); named frame and
labeled SimplePaint2
PaintListener2 listener=new PaintListener2();
canvas.addMouseMotionListener(listener);
Creating a container named
pane, and associating it with
our frame called frame.
pane.add(canvas);
frame.pack();
frame.show();
}
}
Creating the canvas and
adding a listener to it
// DrawingCanvas2.java - a blank canvas that remembers
// drawing operations using an offscreen image
import javax.swing.*;
import java.awt.*;
class DrawingCanvas2 extends JComponent{
public void paint(Graphics g){
if (offscreenImage!=null)
g.drawImage(offscreenImage,0,0,SIZE,SIZE,null);
}
public Image getOffscreenImage(){
if (offscreenImage==null)
offscreenImage=createImage(SIZE,SIZE);
return offscreenImage;
}
public Dimension getMinimumSize(){
return new Dimension(SIZE, SIZE);
}
public Dimension getPreferredSize(){
return new Dimension(SIZE, SIZE);
}
private static final int SIZE=500;
private Image offscreenImage;
}
// PaintListener2.java - paints on a DrwaingCanvas2
// and it is associated with an offscreenImage.Each time the mouse is moved – while the
position of the over the component that is
import java.awt.*;
being listened to, the mouseDragged()
import java.awt.event.*;
method is called.
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);
// duplicating the drawing on the offscreenImage
Image image= canvas.getOffscreenImage();
g = image.getGraphics();
g.fillOval(e.getX()-radius, e.getY() - radius, diameter, diameter);
}
public void mouseMoved(MouseEvent e){}
Each change in position detected by the
protected int radius =3;
system generates another call to
protected int diameter = radius*2;
mouseDragged(). This results in many calls
}
Drawing to both the
offscreenimage and the canvas!!
by a single dragging of the mouse. Each
call is passed a a MouseEvent object that
contains, among other things, a reference
to the component that generated the event
and the coordinates of the mouse at that
time the event was generated.
Everytime a window is
placed over the drawing,
the offscreenimage is
called when the window
becomes active again
The Java
Applet
Drawing program
In this example we will rewrite
the Simplepaint so that we can
place it on the web.
The benefit is that this
application can run anywhere on
any operating system that is
Java enabled.
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);
}
}
// DrawingCanvas2.java - a blank canvas that remembers
// drawing operations using an offscreen image
import javax.swing.*;
import java.awt.*;
class DrawingCanvas2 extends JComponent{
public void paint(Graphics g){
if (offscreenImage!=null)
g.drawImage(offscreenImage,0,0,SIZE,SIZE,null);
}
public Image getOffscreenImage(){
if (offscreenImage==null)
offscreenImage=createImage(SIZE,SIZE);
return offscreenImage;
}
public Dimension getMinimumSize(){
return new Dimension(SIZE, SIZE);
}
public Dimension getPreferredSize(){
return new Dimension(SIZE, SIZE);
}
private static final int SIZE=500;
private Image offscreenImage;
}
This class can
remain unchanged
// PaintListener2.java - paints on a DrwaingCanvas2
// and it is associated with an offscreenImage.
This Listener class
remains unchanged
import java.awt.*;
import java.awt.event.*;
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);
// duplicating the drawing on the offscreenImage
Image image= canvas.getOffscreenImage();
g = image.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>
<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
Running in a browser
The web page
Running in the applet viewer
Sometimes it is easier to debug an applet using the applet viewer
(we don’t see the HTML web page)
// 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);
This is a consolidation of the 3
}
public void mouseMoved(MouseEvent e){}
classes in a single applet. You
}
should compare it to the previous
codes in this presentation and see
how it is actually the same code that
has been simplified.
We will take a closer look at this and its implications in the next lecture.