applet class
play

Applet Class Applets are Java programs that can be invoked in web - PDF document

Applet Class Applets are Java programs that can be invoked in web pages. To run an applet, Java creates an instance of the applet class. Graphics Certain methods run automatically: init() start() stop() paint() As with all methods, we can


  1. Applet Class Applets are Java programs that can be invoked in web pages. To run an applet, Java creates an instance of the applet class. Graphics Certain methods run automatically: init() start() stop() paint() As with all methods, we can overwrite these for our own purposes. T - 1 T - 2 Using Applets Coordinate System x and y are in pixels To use applets, use Java applet code: x (0,0) import java.applet.*; import java.awt.*; // graphics library in Java Abstract Windows Toolkit General form for a graphics drawing is: public class GraphicsWorld extends Applet { public void paint(Graphics g) { // your drawing instructions here } y // other methods Represents a graphics object. It is } provided by the applet. We will send our drawing commands to it. T - 3 T - 4 Basic Drawing Commands Basic Drawing Commands public void drawPolygon(Polygon p) public void drawLine(int x1, int y1, int x2, int y2) public void drawRect(int x, int y, int width, int height) Before we can draw the polygon, we must first create it! public void fillRect(int x, int y, int width, int height) Polygon tri = new Polygon(); tri.addPoint(50,50); public void setColor(Color c) tri.addPoint(75,50); public void drawOval(int x, int y, int width, int height) tri.addPoint(50,75); public void fillOval(int x, int y, int width, int height) g.drawPolygon(tri); g.fillPolygon(tri); T - 5 T - 6

  2. TinMan Example2 g.setColor(Color.red); If I only had a brain g.drawLine(10,10,250,10); g.drawString("Random graphics", 10, 390); g.setColor(Color.red); g.setColor(Color.green); g.drawOval(100,100,50,50); // Head g.fillOval(150,20,100,50); Polygon hat = new Polygon(); Polygon tri2 = new Polygon(); hat.addPoint(100,100); tri2.addPoint(150,150); tri2.addPoint(190,100); hat.addPoint(125,70); tri2.addPoint(230,150); hat.addPoint(150,100); g.setColor(Color.blue); g.fillPolygon(tri2); g.fillPolygon(hat); g.drawOval(10,20,100,50); g.fillRect(150,170,100,50); Polygon tri1 = new Polygon(); g.drawString("If I only had a brain", 80, 60); g.fillRoundRect(150,240,100,50,15,15); tri1.addPoint(10,150); g.fillRect(100,150,50,80); // Body g.fillArc(150,320,100,50,45,180); tri1.addPoint(50,100); g.fillRect(105,230,15,50); // Leg tri1.addPoint(90,150); g.fillRect(130,230,15,50); // Leg g.drawPolygon(tri1); g.drawRect(10,170,100,50); g.fillRect(75,175,100,20); // Arms g.drawRoundRect(10,240,100,50,15,15); T - 7 T - 8 g.drawArc(10,320,100,50,45,180); Example3 Example4 g.setColor(Color.magenta); g.setColor(Color.cyan); for (int i=1; i<=10; i++) for (int i=1; i<=10; i++) g.fillOval(300+(15*i),100+(10*i),25-(2*i),25-(2*i)); g.drawOval(300+(10*i),20,25,50); T - 9 T - 10 Example5 Our Very Own Rectangle Class Point object 50 x y 150 Color [] rainbow = {Color.red, Color.orange, Color.yellow, Color.green, Color.blue, Color.magenta}; g.setColor(Color.blue); height for (int j=0; j<=5; j++) { LLpt for (int i=1; i<=8; i++) { g.setColor(rainbow[j]); width 200 g.fillOval(300+(15*i),150+(10*(2*(i+j))),25-(3*i),25-(3*j)); } height 100 } LLpt (x,y) width Rectangle object T - 11 T - 12

  3. Our Very Own Rectangle Class Our Very Own Rectangle Class public class Rectangle { public class Rectangle { // instance variables // instance variables ... private Point LLpt; private int width; // constructors private int height; ... // constructors // instance methods ... ... } // instance methods ... } T - 13 T - 14 Our Very Own Rectangle Class Our Very Own Rectangle Class public class Rectangle { public class Rectangle { // instance variables // instance variables ... ... // constructors ... // constructors // instance methods public Rectangle(Point p,int w,int h) {} public int getLLx() {} public Rectangle(int LLx,int LLy,int URx,int URy) {} public int getLLy() {} public Point getLLPt() {} // instance methods public int getWidth() {} ... public int getHeight() {} } public int getURx() {} public Point getURPt() {} public int area() {} public int perimeter() {} T - 15 T - 16 } Our Very Own Rectangle Class Our Very Own Rectangle Class public class Rectangle { URpt 50 x // instance variables (x,y) y 150 ... Point object // constructors ... LLpt Rectangle // instance methods object ... URpt } LLpt (x,y) 250 x Point object y 50 T - 17 T - 18

  4. Our Very Own Rectangle Class Our Very Own Rectangle Class public class Rectangle { public class Rectangle { // instance variables // instance variables private Point LLpt; ... private Point URpt; // constructors // constructors public Rectangle(Point p, int w, int h) {} ... public Rectangle(int LLx,int LLy,int URx,int URy) {} // instance methods // instance methods ... ... } } T - 19 T - 20 Our Very Own Rectangle Class Data Abstraction public class Rectangle { Black Box // instance variables Implementer User/Client ... method or // constructors object ... // instance methods public int getLLx() {} Contract public int getLLy() {} describes rules for use (literally separates public Point getLLPt() {} public int getWidth() {} the interface from the implementation) public int getHeight() {} public int getURx() {} When given a black box method, the user (client) doesn’t know or public Point getURPt() {} care how it works, she just wants to use it. The details of the public int area() {} public int perimeter() {} implementation are hidden. This is DATA ABSTRACTION!!! T - 21 T - 22 } Data Abstraction Drawing Bagels in BuggleWorld public void drawBagel(Point p, Color background) { int inset = 1; • How the state of an object is represented is ENTIRELY up to double holeFactor = 0.35; Rectangle cellRect = cellRectangle(p); the programmer (implementer) int bagelX = cellRect.x + inset; int bagelY = cellRect.y + inset; • The client (user) doesn’t care how the object is represented int bagelWidth = cellRect.width - (2*inset); int bagelHeight = cellRect.height - (2*inset); (e.g., width & height vs. URpt), as long as the contract is int holeWidth = (int) (holeFactor*bagelWidth); int holeHeight = (int) (holeFactor*bagelHeight); preserved and the client can use the object as specified int holeX = bagelX + ((bagelWidth - holeWidth)/2); int holeY = bagelY + ((bagelHeight - holeHeight)/2); • Why hide the details of implementation? // Graphics gfx = this.getGraphics(); // The -1s below account for discrete nature of coordinate system. – Things change, programs evolve. By separating clients gfx.setColor(bagelColor); and implementers, we can limit the scope of future change gfx.fillOval(bagelX, bagelY, bagelWidth, bagelHeight); gfx.setColor(background); gfx.fillOval(holeX , holeY, holeWidth, holeHeight); – By limiting the capability of clients, we can avoid certain gfx.setColor(Color.black); gfx.drawOval(bagelX, bagelY, bagelWidth - 1, bagelHeight - 1); kinds of errors, i.e., protection gfx.drawOval(holeX , holeY, holeWidth - 1, holeHeight - 1); T - 23 T - 24 }

Recommend


More recommend