1. Write a java program to scroll the text from left to right and vice versa continuously.
import java.applet.Applet;
import java.awt.*;
public class Slip1A extends Applet implements Runnable {
int x, y, z;
Thread t;
public void init() {
x = 50;
y = 50;
z = 1;
t = new Thread(this);
t.start();
}
public void mpostion() {
x = x + 10 * z;
if (x > this.getWidth())
z = -1;
if (x < 0)
z = 1;
}
public void run() {
while (true) {
repaint();
mpostion();
try {
Thread.sleep(100);
} catch (Exception e) {
}
}
}
public void paint(Graphics g) {
g.drawString("SVPM", x, y);
}
}
/*
* <applet code="Slip1A.class" width="300" height="300">
* </applet>
*/
2. Write a java program in multithreading using applet for
drawing flag.
import java.awt.*;
public class Slip2B extends Frame{
int f = 0;
public Slip2B(){
Signal s = new Signal();
s.start();
setSize(500,500);
setVisible(true);
}
public void paint (Graphics g){
switch (f){
case 0 :
g.drawLine(150, 50, 150, 300);
case 1 :
g.drawRect(150, 50, 100, 90);
}
}
class Signal extends Thread{
public void run(){
while(true){
f = (f+1)%2;
repaint();
try{
Thread.sleep(1000);
}catch(Exception e){
}
}
}
}
public static void main(String args[]){
new Slip2B();
}
}
3. Write a java program using applet for bouncing ball, for each
bounce color of ball
should change randomly.
import java.awt.*;
import java.awt.event.*;
public class Slip3B extends Frame implements Runnable {
private int x, y, w, h, f;
private Color c = Color.red;
public Slip3B() {
setTitle("Bouncing Boll");
setSize(400, 400);
setVisible(true);
w = getWidth();
h = getHeight();
x = (int) (Math.random() * getWidth());
y = (int) (Math.random() * getHeight());
Thread t = new Thread(this);
t.start();
}
public void run() {
while (true) {
switch (f) {
case 0:
y++;
if (y > h - 50) {
c = new Color((int) (Math.random() * 256), (int) (Math.random() * 256),
(int) (Math.random() * 256));
f = 1;
}
break;
case 1:
y--;
if (y < 0) {
c = new Color((int) (Math.random() * 256), (int) (Math.random() * 256),
(int) (Math.random() * 256));
f = 0;
}
}
repaint();
try {
Thread.sleep(10);
} catch (Exception e) {
}
}
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(c);
g.fillOval(x, y, 20, 20);
}
public static void main(String args[]) {
new Slip3B();
}
}
3. Write a java program in multithreading using applet for Traffic signal.
import java.awt.*;
public class Slip5B extends Frame {
int f = 0;
public Slip5B() {
Signal s = new Signal();
s.start();
setSize(500, 500);
setVisible(true);
}
public void paint(Graphics g) {
switch (f) {
case 0:
g.setColor(Color.red);
g.fillOval(60, 60, 50, 50);
g.setColor(Color.black);
g.fillOval(60, 120, 50, 50);
g.fillOval(60, 180, 50, 50);
break;
case 1:
g.setColor(Color.yellow);
g.fillOval(60, 120, 50, 50);
g.setColor(Color.black);
g.fillOval(60, 60, 50, 50);
g.fillOval(60, 180, 50, 50);
break;
case 2:
g.setColor(Color.green);
g.fillOval(60, 180, 50, 50);
g.setColor(Color.black);
g.fillOval(60, 120, 50, 50);
g.fillOval(60, 60, 50, 50);
break;
}
}
class Signal extends Thread {
public void run() {
while (true) {
f = (f + 1) % 3;
repaint();
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
}
}
public static void main(String args[]) {
new Slip5B();
}
}