Art
Birds
Butterflies
Books
Cats
China
Countries
Cosmetology
Education
English
Studies
Flight
& Space
France
Gifts
Health
Hebrew
Historical
Events
Holocaust
Iraq
Ireland
Israel
Italy
Japan
Japanese
>>>Java
Jewish,
Judaism
Law
Love
Lyrics
Love
Poems
Musicology
Nature
New
York
People
Photography
Professions
Spain
Weather
(1)
The Valentine Shop:
(2) The
Rainbow Valentine Shop:
|
ASDF
import java.applet.*; //Class
Applet
import java.awt.*; //Class Graphics
public class Ex1 extends Applet
{
int nCounter = 0;
Font f = new Font("TimesRoman", Font.ITALIC, 40);
int nStartValue = 0;
public Ex1()
{
}
public void init()
{
System.out.println("In init");
String sStartValue = getParameter("StartValue");
if (sStartValue != null)
nStartValue =
Integer.parseInt(sStartValue);
}
public void paint(Graphics g)
{
nCounter++;
g.setFont(f);
for (int i = 0 ; i < 5 ; ++i)
{
for (int j =
0 ; j < 5 ; ++j)
{
if ((i == 0) || (j == 0))
g.setColor(Color.red);
else
g.setColor(Color.blue);
g.drawString("" + ((nStartValue +i) * (j + nStartValue)),
i * 40 + 20, j * 40 + 20);
}
}
}
};
|
MouseListener
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
class MyMouseListener implements MouseListener
{
Ex2 m_Applet;
MyMouseListener(Ex2 app)
{
m_Applet = app;
}
public void mouseClicked(MouseEvent e)
{
System.out.println("mouseClicked");
m_Applet.incValue();
}
public void mouseEntered(MouseEvent e)
{
System.out.println("mouseEntered");
}
public void mouseExited(MouseEvent e)
{
System.out.println("mouseExited");
}
public void mousePressed(MouseEvent e)
{
System.out.println("mousePressed");
}
public void mouseReleased(MouseEvent e)
{
System.out.println("mouseReleased");
}
};
|
threads Pyramide
implements Runnable
class Pyramide implements
Runnable
{
private String m_sCharacter;
private int m_nMaxWidth;
private int m_nInterval;
Pyramide(String sCharacter, int nWidth, int nInterval)
{
m_sCharacter = sCharacter;
m_nMaxWidth = nWidth;
m_nInterval = nInterval;
startThreads(40);
}
private void startThreads(int nNumOfThreads)
{
for (int i = 0 ; i < nNumOfThreads
; ++i )
{
new Thread((Runnable)this).start();
}
}
public void run()
{
int nWidth = 1;
int nChange = 1;
while (true)
{
if ((nWidth ==
m_nMaxWidth) || (nWidth == 0))
nChange *= -1;
drawLine(nWidth
+= nChange);
try
{
Thread.currentThread().sleep(m_nInterval);
}
catch (InterruptedException
error)
{
}
}
}
private synchronized void drawLine(int nWidth)
{
for (int i = 0 ; i < nWidth ;
++i)
{
System.out.print(m_sCharacter);
}
System.out.println();
}
public static void main(String args[])
{
new Pyramide("*", 79, 200);
}
/**************************************************************************
Object Methods
/**************************************************************************
synchronized void m()
{
}
void m()
{
synchronized (this)
{
}
}
/**************************************************************************
Static Methods
/**************************************************************************
static synchronized void m()
{
}
void m()
{
synchronized (Type.class)
{
}
}
*/
};
|
Threads - Synch Test
class SynchTest
{
static int i;
static void m(int j)
{
j = i;
}
static synchronized void synchm(int j)
{
j = i;
}
public static void main(String args[])
{
long lStart = System.currentTimeMillis();
for (int i = 0 ; i < 10000000
; ++i)
{
synchm(i);
}
long lEnd = System.currentTimeMillis();
System.out.println(lEnd - lStart);
}
}
|
4- Ex4 (directory = inner)
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Ex4 extends Applet
{
private LinkedList m_Points = new LinkedList();
public Ex4()
{
//Register Listener
//In the documentation:
//Applet.addMouseListener(MouseListener)
this.addMouseListener(new MyInnerMouseListener());
}
void addPoint(Point p)
{
m_Points.addLast(p);
repaint();
}
public void paint(Graphics g)
{
Iterator iter = m_Points.iterator();
if (!iter.hasNext())
return;
Point pStart = (Point)iter.next();
while(iter.hasNext())
{
Point pEnd =
(Point)iter.next();
g.drawLine(pStart.x,
pStart.y, pEnd.x, pEnd.y);
pStart = pEnd;
}
}
class MyInnerMouseListener extends MouseAdapter
{
public void mouseClicked(MouseEvent
e)
{
System.out.println("mouseClicked");
addPoint(e.getPoint());
}
}
};
|
dir clock (threads) - addPoint, Mouse Adapter
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Ex4 extends Applet
{
private LinkedList m_Points = new LinkedList();
public Ex4()
{
//Register Listener
//In the documentation:
//Applet.addMouseListener(MouseListener)
this.addMouseListener(new MyInnerMouseListener());
}
void addPoint(Point p)
{
m_Points.addLast(p);
repaint();
}
public void paint(Graphics g)
{
Iterator iter = m_Points.iterator();
if (!iter.hasNext())
return;
Point pStart = (Point)iter.next();
while(iter.hasNext())
{
Point pEnd =
(Point)iter.next();
g.drawLine(pStart.x,
pStart.y, pEnd.x, pEnd.y);
pStart = pEnd;
}
}
class MyInnerMouseListener extends MouseAdapter
{
public void mouseClicked(MouseEvent
e)
{
System.out.println("mouseClicked");
addPoint(e.getPoint());
}
}
};
|
dir clock (threads) - Class Clock
extends Thread
class Clock extends Thread
{
Clock()
{
start(); // start the thread
}
//Entry point for thread
public void run()
{
while(true)
{
//sleep for 1
second
try
{
Thread.currentThread().sleep(1000);
//Thread.sleep();
synchronized(this)
{
this.notifyAll(); //notify();
System.out.println("After Notify");
Thread.sleep(500);
}
}
catch (InterruptedException
error)
{
error.printStackTrace();
}
}
}
};
|
dir clock (threads) Viewer extends
Thread
class Viewer extends Thread
{
private Object m_SynchObj;
private String m_sID;
Viewer(Object o, String ID)
{
m_SynchObj = o;
m_sID = ID;
start();
}
public void run()
{
while(true)
{
try
{
synchronized(m_SynchObj)
{
m_SynchObj.wait();
System.out.println("After Wait");
}
}
catch (InterruptedException
error)
{
error.printStackTrace();
}
System.out.println(m_sID
+ ": " +
new java.util.Date().toString());
}
}
};
|
threads - worker
pool - class Queue
import java.util.LinkedList;
class Queue
{
private int nNumOfThreads;
private LinkedList m_JobQueue;
Queue(int nNumOfWorkers)
{
m_JobQueue = new LinkedList();
for (int i = 0 ; i < nNumOfWorkers;
++i)
{
new Worker(this);
}
}
public void addJob(Runnable job)
{
//Add job to link list and make
adjustments so
//a worker will execute it later.
}
public Runnable getJob()
{
//return Runnable if queue is not
empty.
//or wait untill a new job will
be avaialable
}
}
|
Worker extends Thread
class Worker extends Thread
{
private Queue m_Queue;
Worker(Queue queue)
{
m_Queue = queue;
start();
}
public void run()
{
while(true)
{
Runnable r =
m_Queue.getJob();
r.run();
}
}
}
|
Outer 1 Outer
1 and Outer 2 show what like without threads ??????
class Outer1
{
int x;
Outer1()
{
System.out.println("In Outer1 CTor");
}
class Inner1
{
int x;
Inner1()
{
Outer1.this.x
= 6;
System.out.println("In
Inner1 CTor");
}
};
void m()
{
System.out.println("In M");
new Inner1();
System.out.println("x = " +x);
}
public static void main(String args[])
{
new Outer1().m();
}
};
|
Outer 2 - Outer
1 and Outer 2 show what like without threads ??????
class Outer2
{
MyInterface getMyInterface()
{
MyInterface mi = new MyInterface()
{
public void m()
{
System.out.println("In M");
}
};
return mi;
/*
return new MyInterface() {
public void m()
{
System.out.println("In M");
}
};
*/
}
public static void main(String args[])
{
Outer2 o2 = new Outer2();
MyInterface mi = o2.getMyInterface();
mi.m();
System.out.println(mi.getClass().getName());
}
};
|
Warning and
Disclaimer
We try here to provide simple,
non-professional advice and links to sites that seem
to be trustworthy for more details.
This is no substitute for
qualified medical advice.
Always consult a qualified
medical practioner to confirm any self diagnosis and
rule out other causes. Always consult a qualified medical
practioner regarding medical problems or questions.
This site takes no responsibility
for any loss or claim arising from the use or misuse
of information/"advice" on the site or information
or advice on linked sites, nor from the failure of any reader
to seek or take medical, psychological, legal or any other professionally
qualified advice. This warning/disclaimer applies to every
page of this website, whether it appears there or not.
|
|
|