Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Drawing program – Java Applets

Similar presentations


Presentation on theme: "The Drawing program – Java Applets"— Presentation transcript:

1 The Drawing program – Java Applets
Lesson 37: The Drawing program – Java Applets

2 The Java Application Drawing program

3 Importing the libraries
// 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); pane.add(canvas); frame.pack(); frame.show(); } Creating a class called SimplePaint2 Creating a main section Creating a frame named frame and labeled SimplePaint2 Creating a container named pane, and associating it with our frame called frame. Creating the canvas and adding a listener to it

4 // 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(){ private static final int SIZE=500; private Image offscreenImage;

5 // PaintListener2.java - paints on a DrwaingCanvas2
// and it is associated with an offscreenImage. 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(); } public void mouseMoved(MouseEvent e){} protected int radius =3; protected int diameter = radius*2; Each time the mouse is moved – while the position of the over the component that is being listened to, the mouseDragged() method is called. Each change in position detected by the system generates another call to mouseDragged(). This results in many calls 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. Drawing to both the offscreenimage and the canvas!!

6 Everytime a window is placed over the drawing, the offscreenimage is called when the window becomes active again

7 The Java Applet Drawing program

8 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.

9 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); pane.add(canvas); frame.pack(); frame.show(); } // SimplePaintApplet.java //drawing with a mouse on the web 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); }

10 // 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(){ private static final int SIZE=500; private Image offscreenImage; This class can remain unchanged

11 // PaintListener2.java - paints on a DrwaingCanvas2
This Listener class remains unchanged // PaintListener2.java - paints on a DrwaingCanvas2 // and it is associated with an offscreenImage. 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(); } public void mouseMoved(MouseEvent e){} protected int radius =3; protected int diameter = radius*2;

12 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> </BODY> </HTML> Note: this overrides any preferred and minimum size in the applet

13 The files Compiling Running in a browser

14 Running in the applet viewer
The web page Sometimes it is easier to debug an applet using the applet viewer (we don’t see the HTML web page)

15 // consolidatedSimplePaintApplet
// 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){} This is a consolidation of the 3 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.


Download ppt "The Drawing program – Java Applets"

Similar presentations


Ads by Google