0% found this document useful (1 vote)
391 views

Practice Programs For Applets

This document contains code for 3 Java applets: 1) An applet that displays the text "Hello World" in different colors using a for loop to iterate through an array of colors and the drawString method. 2) An applet that displays the current time using a Date object, DateFormat, and a thread to continuously update the time displayed. 3) An applet that draws an 8x8 checkerboard with 20x20 pixel squares that are alternately colored red and black based on the parity (even or odd) of the row and column numbers.

Uploaded by

Shyam Karanth
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
391 views

Practice Programs For Applets

This document contains code for 3 Java applets: 1) An applet that displays the text "Hello World" in different colors using a for loop to iterate through an array of colors and the drawString method. 2) An applet that displays the current time using a Date object, DateFormat, and a thread to continuously update the time displayed. 3) An applet that draws an 8x8 checkerboard with 20x20 pixel squares that are alternately colored red and black based on the parity (even or odd) of the row and column numbers.

Uploaded by

Shyam Karanth
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Practice programs for applets: 1) Write a program to create an applet which shows Hello world in different colors using

java applet and Color classes.


1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. import java.applet.Applet; import java.awt.Graphics; import java.awt.Color; /* <applet code = "HelloWorldInDifferentColorsExample" width = 300 height = 300> </applet> */ public class HelloWorldInDifferentColorsExample extends Applet{ public void paint(Graphics g){ Color c[] = {Color.blue,Color.cyan, Color.darkGray, Color.gray, Color.green, Color.lightGray, Color.magenta, Color.orange, Color.pink, Color.red, Color.white, Color.yellow}; for(int i = 0; i<c.length; i++){ g.setColor(c[i]); g.drawString("Hello World...",10,10 + (i*10)); } } }

2) Write a Java program to display current time of a day using java applets and use Date class.
import java.applet.*; import java.awt.*; import java.util.Date; import java.text.DateFormat; /** An Applet to display the current time */ public class ClockApplet extends Applet implements Runnable { /** A Thread to run the timer */ protected Thread timerThread; /** The date object */ Date date = new Date(); /** The date format */ protected DateFormat format = DateFormat.getTimeInstance(); /* Applet Lifestyle Methods */ public void start() { timerThread = new Thread(this, "Clock"); timerThread.start(); } public void stop() { if (timerThread == null) return;

timerThread = null; } /** Show the time, and wait a while. */ public void run() { while (timerThread != null) { repaint(); // request a redraw try { timerThread.sleep(1000); } catch (InterruptedException e){ /* do nothing*/ } } } /** Display the time. */ public void paint(Graphics g) { date.setTime(System.currentTimeMillis()); g.drawString(format.format(date), 2, 10); } }

3) Write an applet that draws a checkerboard. Assume that the size of the applet is 160 by 160 pixels. Each square in the checkerboard is 20 by 20 pixels. The checkerboard contains 8 rows of squares and 8 columns. The squares are red and black. Here is a tricky way to determine whether a given square is red or black: If the row number and the column number are either both even or both odd, then the square is red. Otherwise, it is black. Note that a square is just a rectangle in which the height is equal to the width, so you can use the subroutine g.fillRect() to draw the squares. Here is an image of the checkerboard:
import java.awt.*; import java.applet.*; public class Checkerboard extends Applet { /* This applet draws a red-and-black checkerboard. It is assumed that the size of the applet is 160 by 160 pixels. */ public void paint(Graphics g) { int row; // Row number, from 0 to 7 int col; // Column number, from 0 to 7 int x,y; // Top-left corner of square for ( row = 0; row < 8; row++ ) { for ( col = 0; col < 8; col++) { x = col * 20; y = row * 20; if ( (row % 2) == (col % 2) )

g.setColor(Color.red); else g.setColor(Color.black); g.fillRect(x, y, 20, 20); } } // end for row } // end paint()

You might also like