Download presentation
Presentation is loading. Please wait.
Published byJoel Mosley Modified over 9 years ago
1
CSI 3125, Preliminaries, page 1 Applet
2
CSI 3125, Preliminaries, page 2 Graphics Methods public abstract void drawString(String str, int x, int y): is used to draw the specified string. public void drawRect(int x, int y, int width, int height): draws a rectangle with the specified width and height. public abstract void fillRect(int x, int y, int width, int height): is used to fill rectangle with the default color and specified width and height. public abstract void drawOval(int x, int y, int width, int height): is used to draw oval with the specified width and height. public abstract void fillOval(int x, int y, int width, int height): is used to fill oval with the default color and specified width and height.
3
CSI 3125, Preliminaries, page 3 Graphics Methods public abstract void drawLine(int x1, int y1, int x2, int y2): is used to draw line between the points(x1, y1) and (x2, y2). public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer): is used draw the specified image. public abstract void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle): is used draw a circular or elliptical arc. public abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle): is used to fill a circular or elliptical arc. public abstract void setColor(Color c): is used to set the graphics current color to the specified color. public abstract void setFont(Font font): is used to set the graphics current font to the specified font.
4
CSI 3125, Preliminaries, page 4 Applet Example import java.applet.Applet; import java.awt.*; public class GraphicsDemo extends Applet{ public void paint(Graphics g){ g.setColor(Color.red); g.drawString("Welcome",50, 50); g.drawLine(20,30,20,300); g.drawRect(70,100,30,30); g.fillRect(170,100,30,30); g.drawOval(70,200,30,30); g.setColor(Color.pink); g.fillOval(170,200,30,30); g.drawArc(90,150,30,30,30,270); g.fillArc(270,150,30,30,0,180); }
5
CSI 3125, Preliminaries, page 5 Image in Applet Displaying Image in Applet The java.awt.Graphics class provide a method drawImage() to display the image. Syntax of drawImage() method: public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer): is used draw the specified image.
6
CSI 3125, Preliminaries, page 6 Image in Applet import java.awt.*; import java.applet.*; public class DisplayImage extends Applet { Image picture; public void init() { picture = getImage(getDocumentBase(),“popo.jpg"); } public void paint(Graphics g) { g.drawImage(picture, 30,30, this); } The 4th argument of drawImage() method of is ImageObserver object. Current class object would also be treated as ImageObserver because Applet class indirectly extends the Component class.
7
CSI 3125, Preliminaries, page 7 Passing Parameters to Applets Parameters are passed to applets in NAME=VALUE pairs in tags between the opening and closing APPLET tags. Inside the applet, the values can be read using the getParameter() method of the java.applet.Applet class.
8
CSI 3125, Preliminaries, page 8 Passing Parameters to Applets import java.applet.*; import java.awt.*; /* */ public class para1 extends Applet { String s; public void init() { s=getParameter("first"); } public void paint(Graphics g) { g.drawString("Parameter is "+s,100,100); }
9
CSI 3125, Preliminaries, page 9 Passing Parameters to Applets import java.applet.*; import java.awt.*; /* */ public class para2 extends Applet { int x,y,sum; public void init() { x=Integer.parseInt(getParameter("first")); y=Integer.parseInt(getParameter("second")); } public void paint(Graphics g) { sum=x+y; g.drawString("Total Sum is "+sum,100,100); }
10
CSI 3125, Preliminaries, page 10 Adding sound to an applet. There are really only two steps required to play a sound in a Java applet: (1) loading the sound file into an AudioClip object, and (2) playing the sound using the play() method of the AudioClip class.
11
CSI 3125, Preliminaries, page 11 Adding sound to an applet. import java.applet.*; public class Sound extends Applet { public void init() { AudioClip g = getAudioClip(getDocumentBase(), "a.au"); g.play(); } /* */
12
CSI 3125, Preliminaries, page 12 Applet sound control with Button import java.applet.*; import java.awt.*; import java.awt.event.*; /* */ public class PlaySoundApplet extends Applet implements ActionListener{ Button play,stop; AudioClip audioClip; public void init(){ play = new Button("Play"); add(play); play.addActionListener(this); stop = new Button("Stop"); add(stop); stop.addActionListener(this); audioClip = getAudioClip(getCodeBase(), "a.au"); } public void actionPerformed(ActionEvent ae){ Button source = (Button)ae.getSource(); if (source.getLabel() == "Play"){ audioClip.play(); } else if(source.getLabel() == "Stop"){ audioClip.stop(); }
13
CSI 3125, Preliminaries, page 13 Applet
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.