370 likes | 683 Views
Features of Java. CS 3331 Fall 2009. Outline. Abstract class Interface Application --- animation applets. Motivation --- Drawing Board. Class Shape. public class Shape { private int x, y; private Color c; public Shape(int x, int y, Color c) {
E N D
Features of Java CS 3331 Fall 2009
Outline • Abstract class • Interface • Application --- animation applets
Class Shape public class Shape { private int x, y; private Color c; public Shape(int x, int y, Color c) { this.x = x; this.y = y; this.c = c; } public void draw(Graphics g) { /* … */ } public int getX() { return x; } public int getY() { return y; } public Color getColor { return c; } } Q: What is wrong with this definition?
Solution: Abstract Class public abstract class Shape { private int x, y; private Color c; protected Shape(int x, int y, Color c) { this.x = x; this.y = y; this.c = c; } public abstract void draw(Graphics g); // no body here! public int getX() { return x; } public int getY() { return y; } public Color getColor { return c; } }
Abstract Classes? • Classes that can’t be instantiated • Used to define common properties that are to be inherited by subclasses • Often provide partial implementations • May include abstract methods, methods that have no body
In Sum, Abstract Classes … • Provide partial implementations to be inherited by subclasses • May include abstract methods • Are good for factoring out common properties among classes
Outline • Abstract classes • Interfaces • Application --- animation applets
Interfaces • Declare features to be supported by classes • Provide no implementation • Only allow public abstract methods and constants (public static final fields) public interface Runnable { public void run(); }
Shape {abstract} Why Interfaces? • To draw automobiles … 0..* DrawingBoard Vehicle Circle Rectangle Triangle Automobile
How to Draw Automobiles? • By programming to the interface.
In Sum, Interfaces … • Good for establishing a well-defined boundary between modules (subsystems) • Thus, make programs more reusable and maintainable
Abstract Classes vs. Interfaces • Partial code vs. no code at all • Class vs. interface
Exercise • Separate the display of DigitalClock to support various ways of displaying time, e.g., digital, analog, customized background, etc. Explain your design by drawing a UML class diagram.
Applications --- Animation Applets • Enhanced digital clock applet • Scrolling banner applet • Initial version • Double-buffered version
Enhanced Digital Clock Applet • Setting applet parameters in the Web page <html> … <applet code=“DigitalClock2.class” width=“250” height=“80”> <param name=“color” value=“blue”> </applet> … </html>
Getting Applet Parameters import java.awt.Color; public class DigitalClock2 extends DigitalClock { public void init() { String param = getParameter(“color”); if (“red”.equals(param)) { color = Color.RED; } else if (“blue”.equals(param)) { color = Color.BLUE; } else if (“yellow”.equals(param)) { color = Color.YELLOW; } /* … */ else { color = Color.GREEN; } } }
Animation Applets • Enhanced digital clock applet • Scrolling banner applet • Initial version • Double-buffered version
The java.awt.Graphics Class • Class Graphics • Represents graphics context, an abstraction of various drawing surfaces, e.g., screen, printer, off-screen image (an image stored in memory). • Provide a rich set of graphics methods. drawString() drawLine() drawArc() fillArc() drawOval() fillOval() drawPolygon() fillPolygon() drawRect() fillRect() drawRoundRect() fillRoundRect()
Graphics Class (Cont.) • Other methods setColor(color) set the current color setFont(font) set the current font setPaintMode() set the paint, or overwrite mode setXORMode(color) set the XOR mode getColor() get the current color getFont() get the current font getFontMetrics() get the font metrics of the current font getFontMetrics(font) get the font metrics for the specified font
The java.awt.FontMetrics Class leading Up ascent height baseline descent width getAscent() getDescent() getHeight() getLeading() stringWidth(s)
Scrolling Banner Applet import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ScrollingBanner extends java.applet.Applet { <field declarations> public void init() { ... } public void paint(Graphics g) { ... } public void start() { ... } public void stop() { ... } }
Field Declarations protected String text; protected Font font = new java.awt.Font("Sans-serif", Font.BOLD, 24); protected Dimension dim; protected int x, y; protected int delay = 100; protected int offset = 1; protected Timer timer; // animation timer
Initialization public void init() { // get parameters "delay" and "text" String att = getParameter("delay"); if (att != null) { delay = Integer.parseInt(att); } att = getParameter("text"); if (att != null) { text = att; } else { text = “Go Miners!”; } // set initial position of the text dim = getSize(); x = dim.width; y = font.getSize(); // initialize animation timer timer = new Timer(delay, new ActionListener() { public void actionPerformed() { repaint(); } }); }
Painting the Current Frame (0, 0) length viewing area Go Miners! Go Miners! Go Miners! (x, y) (-length, y) (dim.width, y) (dim.width-1, y) current position leftmost position rightmost position
Painting the Current Frame (Cont.) public void paint(Graphics g) { // get the font metrics to determine the length of the text g.setFont(font); FontMetrics fm = g.getFontMetrics(); int length = fm.stringWidth(text); // adjust the position of text from the previous frame x = x - offset; // if the text is completely off to the left end // move the position back to the right end if (x < -length) { x = dim.width; } // set the pen color and draw the background g.setColor(Color.BLACK); g.fillRect(0, 0, dim.width, dim.height); // set the pen color, then draw the text g.setColor(Color.GREEN); g.drawString(text, x, y); }
The start() and stop() Methods public void start() { timer.start(); } public void stop() { timer.stop(); }
Exercise • Define a subclass of ScrollingBanner, called ScrollingBanner3, that scrolls the banner vertically. Reuse code as much as possible and minimize code duplication.
How to Avoid Flickering? • Flickering is caused by repaint() • repaint() calls the update() method. • The default update() method does the following: • paint the whole area with the background color; • set the foreground color; • call the paint() method. • The update() method is also called by the system to update windows. • Solution: • override the update() method • use an off-screen image
Using Off-Screen Image • Double buffering import java.awt.*; public class ScrollingBanner2 extends ScrollingBanner { protected Image image; // off-screen image protected Graphics offscreen; // off-screen graphics public update(Graphics g) { ... } public paint(Graphics g) { ... } }
Using Off-Screen Image (Cont.) public void update(Graphics g) { // create the offscreen image if it is the first time if (image == null) { image = createImage(dim.width, dim.height); offscreen = image.getGraphics(); } // draw the current frame into the off-screen image // using the paint method of the superclass super.paint(offscreen); // copy the off-screen image to the screen g.drawImage(image, 0, 0, this); } public void paint(Graphics g) { update(g); }
Animation Applet Idiom • Category Behavioral implementation idiom • Intent For an applet to continuously update its appearance without user input or intervention • Also known as Active Applet • Applicability Use the Animation Applet Idiom to animate dynamic processes
Animation Applet Idiom (Cont.) import java.awt.*; import java.awt.event.*; import javax.swing.*; public class AnimationAppletextends java.applet.Applet { protected Timer timer= null; protected int delay; public void init() { timer = new Timer(delay, new ActionListener() { public void actionPerformed() { repaint(); } }); }
Animation Applet Idiom (Cont.) public void start() { timer.start(); } public void stop() { timer.stop(); } public void paint(Graphics g) {<paint the current frame> } <other methods and fields>}