Shapes and Colors in Applet
Applet has a Life Cycle method public void paint(Graphics g) which can be used to design/draw shapes in Applet area. Like: Line, Circle, Oval, Arc, Text, Polygon, etc…
Writing a String on Applet
File Name: WriteString.java
import java.applet.*; import java.awt.*; public class WriteString extends Applet { public void paint(Graphics g) { g.drawString("Ankit Virparia - www.ankit.co", 100, 100); } } /* <applet code="WriteString" width="500" height="200"></applet> */
Output:
Changing Text Font
We need to invoke a method named: setFont(Font f) to change the Font size and style. To create a Font object we have used a constructor which takes following arguments. 1) String FontName, 2) Font Style as int. Here, We have used Font.BOLD whereby BOLD is the static int which belongs to java.awt.Font class. 3) Font size as int.
To change Text Color we have to invoke a method named: setColor(Color c) of Graphics class. Color object can be passed in 2 ways
1. By Creating Color object from the scratch like Color clr = new Color(255,0,0); Here, Constructor takes red, green, blue int values which can vary from 0 to 255.
2. By Passing static color object Like Color.RED or Color.red
File Name: FontString.java
import java.applet.*; import java.awt.*; public class FontString extends Applet { public void paint(Graphics g) { Font f = new Font("Arial", Font.BOLD, 36); g.setFont(f); g.setColor(Color.GREEN); g.drawString("Ankit Virparia, www.ankit.co", 10, 100); } } /* <applet code="FontString" width="500" height="200"></applet> */
Drawing Shapes
File Name: ShapeApplet.java
import java.applet.*; import java.awt.*; public class ShapeApplet extends Applet { public void paint(Graphics g) { setBackground(Color.BLACK); //setting background as BLACK g.setColor(new Color(255,255,255)); g.drawLine(10, 10, 400, 10); //g.drawLine(x1, y1, x2, y2); g.setColor(Color.red); g.drawRect(30,30,200,100); //g.drawRect(x, y, w, h); g.setColor(new Color(80,10,124)); g.drawOval(30,30,200,100); //g.drawOval(x, y, w, h); g.setColor(Color.BLUE); g.drawArc(100, 20, 300, 300, 0, 90); //g.drawArc(x, y, w, h, Starting Angle, Further Angle); } } /* <applet code="ShapeApplet" width="500" height="200"></applet> */
Output:
Filling Shapes
File Name: ShapeFillApplet.java
import java.applet.*; import java.awt.*; public class ShapeFillApplet extends Applet { public void paint(Graphics g) { setBackground(Color.BLACK); //setting background as BLACK g.setColor(new Color(255,255,255)); g.drawLine(10, 10, 400, 10); //g.drawLine(x1, y1, x2, y2); g.setColor(Color.red); g.fillRect(30,30,200,100); //g.fillRect(x, y, w, h); g.setColor(new Color(80,10,124)); g.fillOval(30,30,200,100); //g.fillOval(x, y, w, h); g.setColor(Color.BLUE); g.fillArc(100, 20, 300, 300, 0, 90); //g.fillArc(x, y, w, h, Starting Angle, Further Angle); } } /* <applet code="ShapeFillApplet" width="500" height="200"></applet> */
Output:
Drawing Polygon
File Name: DrawingPolygons.java
import java.awt.*; import java.applet.*; public class DrawingPolygons extends Applet { public void paint(Graphics g) { int x[] = { 70, 150, 190, 80, 100 }; int y[] = { 80, 110, 160, 190, 100 }; g.drawPolygon (x, y, 5); int x1[] = { 210, 280, 330, 210, 230 }; int y1[] = { 70, 110, 160, 190, 100 }; g.fillPolygon (x1, y1, 5); } } /* <applet code="DrawingPolygons" width="500" height="200"></applet> */
Recent Comments