ana sayfa : araştırma : java uygulamaları : kullanıcı ile diyalog [ 11 / 12 ] |
|
GERİ | |
11
KULLANICI İLE DİALOG
11.1
Butonlar
Bir java programında
kullanıcı ile bir dialog kurulması, kullnıcının belirli opsiyonlardan
birini seçmesi gerekebilir. Programakışıda kullanıcının aksiyonuna göre
değişebilir. Aşağıdaki program
butonların kullanılması ile kullanıcıyı yönlendiren bir örnektir. <html> <head> <title>RedButton
Menu</title> <base
target="right"> </head> <body
bgcolor="#000000"> <p
align="center"> </p> <p> </p> <div
align="center"><center> <table
border="0" cellspacing="3" width="300">
<tr>
<td><p align="center">
<applet code="ButtonPLUS3.class" align="absmiddle"
width="170" height="43">
<param name="BGCOLOR" value="230,0,0">
<param name="FONTSIZE" value="18">
<param name="HIGHLIGHT" value="8">
<param name="SOUND" value="c.au|h.au">
<param name="TARGET" value="_self">
<param name="TEXT" value="Internet Services|Click!|LOAD">
<param name="TEXTALIGN" value="center">
<param name="TEXTCOLOR" value="255,255,000">
<param name="URL" value="redbuttons/redbuttons.html">
</applet>
</td>
</tr>
<tr>
<td><p align="center">
<applet code="ButtonPLUS3.class" align="absmiddle"
width="170" height="43">
<param name="BGCOLOR" value="230,0,0">
<param name="font" value="Courier">
<param name="FONTSIZE" value="18">
<param name="frames" value="main">
<param name="HIGHLIGHT" value="8">
<param name="SOUND" value="c.au|h.au">
<param name="TARGET" value="_self">
<param name="TEXT" value="About Us|GO!|LOAD">
<param name="TEXTALIGN" value="center">
<param name="TEXTCOLOR" value="255,255,000">
<param name="URL" value="redbuttons/redbuttons.html">
</applet>
</td>
</tr>
<tr>
<td><p align="center">
<applet code="ButtonPLUS3.class" align="absmiddle"
width="170" height="43">
<param name="BGCOLOR" value="230,0,0">
<param name="FONTSIZE" value="18">
<param name="HIGHLIGHT" value="8">
<param name="SOUND" value="c.au|h.au">
<param name="TARGET" value="_self">
<param name="TEXT" value="Troubleshooting|Click!|GO!">
<param name="TEXTALIGN" value="center">
<param name="TEXTCOLOR" value="255,255,000">
<param name="URL" value="redbuttons/redbuttons.html">
</applet>
</td>
</tr>
<tr>
<td><p align="center">
<applet code="ButtonPLUS3.class" align="absmiddle"
width="170" height="43">
<param name="BGCOLOR" value="0,230,0">
<param name="font" value="Verdana">
<param name="FONTSIZE" value="18">
<param name="HIGHLIGHT" value="10">
<param name="SOUND" value="c.au|h.au">
<param name="TARGET" value="_self">
<param name="TEXT" value="New Systems|Click!|LOAD">
<param name="TEXTALIGN" value="center">
<param name="TEXTCOLOR" value="255,0,0">
<param name="URL" value="redbuttons/redbuttons.html">
</applet>
</td>
</tr>
<tr>
<td><p align="center">
<applet code="ButtonPLUS3.class" align="absmiddle"
width="170" height="43">
<param name="BGCOLOR" value="0,0,230">
<param name="font" value="Courier">
<param name="FONTSIZE" value="18">
<param name="HIGHLIGHT" value="8">
<param name="SOUND" value="c.au|h.au">
<param name="TARGET" value="_self">
<param name="TEXT" value="Contact Us|GO!|LOAD">
<param name="TEXTALIGN" value="center">
<param name="TEXTCOLOR" value="255,255,255">
<param name="URL" value="redbuttons/redbuttons.html">
</applet>
</td>
</tr> </table> </center></div> </body> </html>
buton.java import
java.awt.*; import
java.applet.Applet; import
java.util.Vector; import
java.net.URL; public
class JZOOButtonRing extends
Applet implements
Runnable {
final int DEFAULT_RADIUS = 100;
Vector items = new Vector();
Thread thread = null;
int appletWidth = 0;
int appletHeight = 0;
int radius = DEFAULT_RADIUS;
int newRadius = radius;
boolean follow = false;
int centerX = 0;
int centerY = 0;
final double MAX_THETA = 2 * Math.PI;
final double ONE_DEGREE = 2 * Math.PI / 360;
double currTheta = 0;
double deltaTheta = 0;
Image buffer = null;
Graphics bufferGraphics = null;
Image bgImage = null;
boolean ready = false;
MediaTracker tracker = new MediaTracker(this);
JZOOButton currButton = null;
String oldStatus = "";
String currStatus = "";
public void init()
{
// get applet details
appletWidth = size().width;
appletHeight = size().height;
centerY = appletHeight / 2;
buffer = createImage(appletWidth, appletHeight);
bufferGraphics = buffer.getGraphics();
// get images
String imagePath = null;
int i=1;
while (true)
{
imagePath = getParameter("image" + i);
if (imagePath == null)
{
break;
}
Image image = getImage(getCodeBase(), imagePath);
tracker.addImage(image, 0);
JZOOButton button = new JZOOButton(this);
button.addImage(image);
items.addElement(button);
String link = getParameter("link" + i);
if (link != null)
{
try
{
if ( link.startsWith("http") || link.startsWith("HTTP")
) {
button.url
= new URL(link);
}
else {
button.url = new URL(getCodeBase(), link);
}
}
catch (Exception e)
{
System.out.println("Invalid
url: " + link);
}
}
String target = getParameter("target" + i);
System.out.println("found target " + i + ": " +
target);
if (target != null) {
button.target
= target;
System.out.println(i + " button.target " + button.target);
}
i++;
}
imagePath = getParameter("bgimage");
if (imagePath != null)
{
bgImage = getImage(getCodeBase(), imagePath);
tracker.addImage(bgImage, 0);
}
tracker.checkAll(true);
// other parameters
String param;
param = getParameter("radius");
if (param != null)
{
try
{
newRadius = Integer.parseInt(param);
}
catch (Exception e)
{
newRadius = DEFAULT_RADIUS;
}
}
param = getParameter("startradius");
if (param != null)
{
try
{
radius = Integer.parseInt(param);
}
catch (Exception e)
{
radius = DEFAULT_RADIUS;
}
}
param = getParameter("follow");
if (param != null)
{
follow = param.equalsIgnoreCase("true");
}
param = getParameter("centerx");
if (param != null)
{
try
{
centerX = Integer.parseInt(param);
}
catch (Exception e)
{
centerX = appletWidth / 2;
}
}
param = getParameter("centery");
if (param != null)
{
try
{
centerY = Integer.parseInt(param);
}
catch (Exception e)
{
centerY = appletHeight / 2;
}
}
// initialise
deltaTheta = (double) MAX_THETA / items.size();
}
private boolean imagesLoaded()
{
return tracker.checkAll();
}
public void start()
{
if (thread == null)
{
thread = new Thread(this);
thread.start();
}
}
public void stop()
{
if (thread != null && thread.isAlive())
{
thread.stop();
thread = null;
}
}
public void update(Graphics g)
{
paint(bufferGraphics);
g.drawImage(buffer, 0, 0, this);
}
public void paint(Graphics g)
{
if (ready)
{
// clear screen
g.setColor(Color.black);
g.fillRect(0,0,appletWidth, appletHeight);
// draw bgImage
if (bgImage != null)
{
g.drawImage( bgImage, 0, 0, this);
}
// draw buttons
for (int i=0; i<items.size(); i++)
{
JZOOButton button = (JZOOButton) items.elementAt(i);
g.drawImage(
button.image,
centerX + (int) Math.round(button.x),
centerY + (int) Math.round(button.y),
this);
}
}
}
public void run()
{
while (true)
{
// check if images have loaded
if (ready)
{
// move images
double tempTheta = currTheta;
for (int i=0; i<items.size(); i++)
{
JZOOButton button = (JZOOButton) items.elementAt(i);
button.x = radius * Math.cos(tempTheta);
button.y = radius * Math.sin(tempTheta);
tempTheta += deltaTheta;
}
currTheta += ONE_DEGREE ;
// check radius
if (newRadius != radius)
{
if (newRadius < radius)
{
radius--;
}
else
{
radius++;
}
}
}
else
{
ready = imagesLoaded();
}
// display
repaint();
try
{
thread.sleep(50);
}
catch (Exception e)
{
System.err.println(e);
}
}
}
public boolean mouseDown(Event e, int xpos, int ypos)
{
if (currButton != null)
{
if (currButton.target == null) {
getAppletContext().showDocument(currButton.url);
}
else {
System.out.println(currButton.target);
getAppletContext().showDocument(currButton.url, currButton.target);
}
}
return true;
}
public boolean mouseMove(Event e, int xpos, int ypos)
{
if (follow)
{
// determine new radius
int x = Math.abs(centerX - xpos);
int y = Math.abs(centerY - ypos);
newRadius = (int) Math.round(Math.sqrt(x*x + y*y));
return true;
}
for (int i=0; i<items.size(); i++)
{
JZOOButton b = (JZOOButton) items.elementAt(i);
int x = (int) b.x;
int y = (int) b.y;
int w = b.getWidth();
int h = b.getHeight();
Rectangle r = new Rectangle(x + centerX, y + centerY, w, h);
if (r.inside(xpos, ypos))
{
currButton = b;
newStatus(b.url.toString());
return true;
}
}
resetStatus();
currButton = null;
return true;
}
private void newStatus(String newStatus)
{
if (!currStatus.equals(newStatus))
{
oldStatus = currStatus;
currStatus = newStatus;
showStatus(currStatus);
}
}
private void resetStatus()
{
currStatus = oldStatus;
showStatus(currStatus);
} } 11.2
Seçim Listeleri
Windows
sisteminin en belirgin araçlarından biri seçim listeleridir. import
java.util.*; import
java.net.*; import
java.awt.*; import
java.applet.Applet; public
class link extends Applet{
public int number;
public Vector urls = new Vector();
public Vector names = new Vector();
public URL theURL = null;
String at; StringTokenizer st;
StringTokenizer str;
String targetFrame;
Color bgColor; String s;
String rgbDelimiter = ":,.";
public void init() {
s = getParameter("bgColor");
if (s != null) st = new StringTokenizer(s, rgbDelimiter);
if (s == null)
bgColor = Color.lightGray;
else if (s.equalsIgnoreCase("red"))
bgColor =
Color.red;
else if (s.equalsIgnoreCase("blue")
bgColor =
Color.blue;
else if (s.equalsIgnoreCase("green"))
bgColor = Color.green;
else if (s.equalsIgnoreCase("yellow"))
bgColor = Color.yellow;
else if (s.equalsIgnoreCase("white"))
bgColor = Color.white;
else if (s.equalsIgnoreCase("orange"))
bgColor = Color.orange;
else if (s.equalsIgnoreCase("cyan"))
bgColor =
Color.cyan;
else if (s.equalsIgnoreCase("magenta")) bgColor = Color.magenta;
else if (s.equalsIgnoreCase("black"))
bgColor = Color.black;
else if (st.countTokens() == 3) {
Integer r = new Integer(st.nextToken());
Integer g =
new Integer(st.nextToken());
Integer b =
new Integer(st.nextToken());
bgColor =
new Color(r.intValue(), g.intValue(), b.intValue());
} else
bgColor =
Color.lightGray;
String input_location = getParameter("target");
if(input_location != null) {
targetFrame = input_location;
} else{
targetFrame = "_parent";
} String input_number =
getParameter("number");
number = Integer.parseInt(input_number);
Choice theLinks = new Choice();
for(int i = 0; i <= number; i++){
at = getParameter("link" + i);
str = new StringTokenizer(at, "\\");
names.addElement(new String(str.nextToken()));
urls.addElement(new String(str.nextToken()));
theLinks.addItem((String)names.elementAt(i));
}
add(theLinks); setBackground(bgColor);
} public boolean action(Event evt, Object arg){ if(evt.target instanceof
Choice){ String site =
(String)arg; for(int
n = 0; n <= number; n++){
if(site.equals((String)names.elementAt(n))){
try{theURL = new URL((String)urls.elementAt(n));}
catch(MalformedURLException e) {
System.out.println("Bad URL");}
getAppletContext().showDocument(theURL,targetFrame);
} }
} return
true; }}
link.html <html> <head> <meta
http-equiv="Content-Type" content="text/html; charset=windows-1254"> <title>New
Page 2</title> <meta
name="GENERATOR" content="Microsoft FrontPage 3.0"> </head> <body> <p> <applet
CODE="link.class" WIDTH="175" HEIGHT="30">
<param name="target" value="_top">
<param name="bgColor" value="black">
<param name="number" value="5">
<param name="link0" value="Bir Link Seciniz\not a
link">
<param name="link1" value="Netinet\\http://netinet.cjb.net">
<param name="link2" value="Shareware.com\\http://www.shareware.com">
<param name="link3" value="Gamelan\\http://www.gamelan.com">
<param name="link4" value="Yahoo\\http://www.yahoo.com">
<param name="link5" value="IcePage\\http://www.icepage.com"> </applet> </p> <p><applet
CODE="link.class" WIDTH="175" HEIGHT="30"><br> <param
name="target" value="_top"> <br> <param
name="bgColor" value="black"><br> <param
name="number" value="5"> <br> <param
name="link0" value="Bir Link Seciniz\not a
link"><br> <param
name="link1" value="Netinet\\http://netinet.cjb.net">
<br> <param
name="link2" value="Shareware.com\\http://www.shareware.com"><br> <param
name="link3" value="Gamelan\\http://www.gamelan.com"><br> <param
name="link4" value="Yahoo\\http://www.yahoo.com"><br> <param
name="link5" value="IcePage\\http://www.icepage.com"><br> </applet><br> <br> </p> </body> </html>
Şekil 11.3: Timer.html timer.java import
java.applet.*; import java.awt.*; import java.util.*; //Represents the
SpinButton/EditBox controls for hour, minute, second class SpinButton { int
x, y, number, maxVal, runner; Font font1 = new Font("Dialog",
Font.BOLD,14); boolean up, down; long timeSet; public SpinButton(int x, int
y, int max) { this.x=x; this.y=y; maxVal=max; } public void init() { number=0;
} public void DrawControl(Graphics g) { g.setColor(Color.white); g.fillRect(x,
y, 25, 18); g.setColor(Color.lightGray); g.fill3DRect(x+25, y, 14, 9, !up);
g.fill3DRect(x+25, y+9, 14, 9, !down); g.setColor(Color.black); UpTriangle(g,
x+28, y+2, 8, 5); DownTriangle(g, x+29, y+16, 7, 4); g.setFont(font1); g.drawString(""+number,x+5,y+14);
} public void UpTriangle(Graphics g, int x, int y, int w, int h) { int a[] =
{x,(x+w/2),x+w}; int b[] = {y+h,y,y+h}; g.fillPolygon(a, b, 3); } public
void DownTriangle(Graphics g, int x, int y, int w, int h) { int a[] =
{x,(x+w/2),x+w}; int b[] = {y-h,y,y-h}; g.fillPolygon(a, b, 3); } public
boolean mouseDown(int xx, int yy) { if(xx>x+25&&xx<x+40&&yy>y&&yy<y+9)
{up=true; return true;} if(xx>x+25&&xx<x+40&&yy>y+9&&yy<y+18)
{down=true; return true;} return false; } public void mouseUp() { up=down=false;
} public int GetNumber() { return number; } //when user keeps arrow key
pressed, increase/decrease value public void Update() { runner++; if(runner%2==0)
{ if(up){if(number<maxVal)number++; else number=0;} if(down){if(number>0)number--;
else number=maxVal;} } } }
//*************************************************************** //Represents
the checkboxes for Sound and CountUp / CountDown class CheckBox { int x, y;
Font font1 = new Font("Helvetica", Font.BOLD,9); boolean checked,
clicked; String string, message; int stringWidth; public CheckBox(int x, int
y, String str, boolean checked) { this.x=x; this.y=y; this.string=str; this.checked=checked;
} public void init() { } public void DrawControl(Graphics g) { g.setFont(font1);
FontMetrics fm = g.getFontMetrics(font1); stringWidth=fm.stringWidth(string);
g.setColor(Color.white); g.fillOval(x,y,13,13); g.fillRect(x+20,y,stringWidth+9,13);
g.setColor(Color.black); if(checked) g.fillOval(x+4,y+4,5,5); g.drawString(string,x+25,y+10);
} public boolean mouseDown(int xx, int yy) { if(xx>x&&xx<x+stringWidth+30&&yy>y&&yy<y+13)
{clicked=true; return true;} return false; } public void mouseUp() { clicked=false;
} } //*************************************************************** //The
main class for the timer public class Timer extends Applet implements
Runnable { Thread Clock_animation; private Image Buffer; private Graphics
gBuffer; long currTime, startTime, diffTime, pauseValue, timeSet; int hours,
minutes, seconds, runner, soundRunner; boolean pressedStart, pressedStop,
pressedReset, pressedEnlarge, countDown; boolean paused, enlarged, running,
playSound, input, blink, framed; boolean soundDelay; //Variables for
rotation int ox=0; int oy=0; double radians = 0.0; double cos = 1.0; double
sin = 0.0; AudioClip alarmSound; Font font1 = new Font("Dialog",
Font.PLAIN,14); Font font2 = new Font("Dialog", Font.BOLD,11);
Font font3 = new Font("Helvetica", Font.BOLD,25); Font font4 = new
Font("Helvetica", Font.BOLD,9); Font font5 = new Font("Helvetica",
Font.BOLD,16); Color color1 = new Color(25,55,135); Color color2 = new Color(225,225,225);
Color bgColor = new Color(0,0,0); SpinButton sb1, sb2, sb3; CheckBox cb1,
cb2, cb3; String message; public void init() { Buffer=createImage(size().width,size().height);
gBuffer=Buffer.getGraphics(); //Load the Alarmsound try{ alarmSound=getAudioClip(getCodeBase(),"alarm.au");
} catch (Exception e){} sb1 = new SpinButton(110, 280, 99); sb2 = new
SpinButton(110, 300, 59); sb3 = new SpinButton(110, 320, 59); cb1 = new
CheckBox(52,124, "Count Down", false); cb2 = new CheckBox(52,144,
"Count Up", true); cb3 = new CheckBox(10,244, "Play Sound",
true); playSound=true; currTime=startTime=diffTime=pauseValue=timeSet=0;
message=getParameter("timer_name"); String r, g, b; int rt, gr, bl;
//Interprete the parameters for the colors String clockColorStr=getParameter("clock_color");
r=clockColorStr.substring(0,2); g=clockColorStr.substring(2,4); b=clockColorStr.substring(4);
rt=Integer.parseInt(r, 16); gr=Integer.parseInt(g, 16); bl=Integer.parseInt(b,
16); color1=(new Color(rt, gr, bl)); String bgColorStr=getParameter("bg_color");
r=bgColorStr.substring(0,2); g=bgColorStr.substring(2,4); b=bgColorStr.substring(4);
rt=Integer.parseInt(r, 16); gr=Integer.parseInt(g, 16); bl=Integer.parseInt(b,
16); bgColor=(new Color(rt, gr, bl)); } public void start() { if (Clock_animation
== null) { Clock_animation = new Thread (this); Clock_animation.start(); } }
public void stop() { if (Clock_animation != null) { Clock_animation.stop();
Clock_animation = null; } } public void run() { while (true) { try {Clock_animation.sleep
(100);} catch (Exception e) { } runner++; if(runner>5)runner=0; if(soundDelay)
{ soundRunner++; if(soundRunner>50) { alarmSound.stop(); soundRunner=0;
soundDelay=false; } } if(runner<3)blink=true; else blink=false; sb1.Update();
sb2.Update(); sb3.Update(); DrawBackground(); DrawTextArea(); DrawGauge();
DrawClock(); DrawCircle(); if(!paused&&running)CalculateTime();
ObserveTimer(); repaint(); } } void ObserveTimer() { if(countDown&&running)
if(hours==0&&minutes==0&&seconds==0) { if(playSound)
alarmSound.loop(); soundDelay=true; running=false; } } void DrawBackground()
{ gBuffer.setColor(bgColor); gBuffer.fillRect(0,0,190,350); gBuffer.setColor(color1);
if(enlarged) gBuffer.fillRoundRect(0,0,190,350,32,32); else gBuffer.fillRoundRect(0,0,190,270,32,32);
gBuffer.setColor(Color.lightGray); gBuffer.fill3DRect(22,90,46,20,!pressedStart);
gBuffer.fill3DRect(72,90,46,20,!pressedStop); gBuffer.fill3DRect(122,90,46,20,!pressedReset);
gBuffer.setFont(font2); gBuffer.setColor(Color.black); gBuffer.drawString("START",24,104);
gBuffer.drawString("STOP",78,104); gBuffer.drawString("RESET",125,104);
//the Enlarge button gBuffer.setColor(color2); gBuffer.fill3DRect(100,242,19,18,!pressedEnlarge);
gBuffer.setColor(Color.black); DrawTriangle(104, 256, 10, 8); gBuffer.setFont(font2);
gBuffer.setColor(Color.white); gBuffer.drawString("Set Timer",125,255);
if(enlarged) { gBuffer.drawString("Hours",30,295); gBuffer.drawString("Minutes",30,315);
gBuffer.drawString("Seconds",30,335); sb1.DrawControl(gBuffer);
sb2.DrawControl(gBuffer); sb3.DrawControl(gBuffer); } else { gBuffer.setFont(font4);
gBuffer.setColor(Color.black); gBuffer.drawString("© 2000 by J.
Wallroth",2,322); gBuffer.setColor(Color.white); gBuffer.drawString("©
2000 by J. Wallroth",0,320); } cb1.DrawControl(gBuffer); cb2.DrawControl(gBuffer);
cb3.DrawControl(gBuffer); } void DrawClock() { if(soundRunner>0&&blink)
gBuffer.setColor(Color.red); else gBuffer.setColor(Color.white); gBuffer.fillRoundRect(38,45,112,30,15,15);
gBuffer.setFont(font3); if(soundRunner>0&&blink) gBuffer.setColor(Color.white);
else gBuffer.setColor(Color.black); String s1, s2, s3; if(hours<10)s1="0";
else s1=""; if(minutes<10)s2="0"; else
s2=""; if(seconds<10)s3="0"; else s3="";
String output=s1+hours+":"+s2+minutes+":"+s3+seconds; if(output.length()<=8)
gBuffer.drawString(output,45,69); else gBuffer.drawString("00:00:00",45,69);
} public void update(Graphics g) { paint(g); } public void paint(Graphics g)
{ g.drawImage (Buffer,0,0, this); } public boolean mouseMove(Event evt,int
x,int y) { if(x>20&&x<170&&y>200&&y<220)
framed=true; else framed=false; return true; } public boolean mouseDown(Event
evt,int x,int y) { sb1.mouseDown(x, y); sb2.mouseDown(x, y); sb3.mouseDown(x,
y); cb1.mouseDown(x, y); cb2.mouseDown(x, y); cb3.mouseDown(x, y); if(cb1.clicked)
{ ResetTimer(); cb1.checked=true; cb2.checked=false; countDown=true; if(!enlarged)enlarged=true;
pressedReset=true; } else if(cb2.clicked) { ResetTimer(); cb2.checked=true;
cb1.checked=false; countDown=false; pressedReset=true; } if(cb3.clicked){cb3.checked^=true;
playSound^=true;} if(x>22&&x<68&&y>90&&y<110){pressedStart=true;StartTimer();}
if(x>72&&x<118&&y>90&&y<110){pressedStop=true;StopTimer();}
if(x>122&&x<168&&y>90&&y<110){pressedReset=true;ResetTimer();}
if(x>100&&x<120&&y>242&&y<262){enlarged^=true;pressedEnlarge=true;}
if(x>20&&x<170&&y>200&&y<220){input=true;
message="";} else input=false; if(soundDelay) { alarmSound.stop();
soundRunner=0; soundDelay=false; } repaint(); return true; } public boolean
mouseUp(Event evt,int x,int y) { pressedStart=pressedStop=pressedReset=pressedEnlarge=false;
sb1.mouseUp(); sb2.mouseUp(); sb3.mouseUp(); cb1.mouseUp(); cb2.mouseUp();
cb3.mouseUp(); repaint(); return true; } void StartTimer() { long h=sb1.GetNumber();
long m=sb2.GetNumber(); long s=sb3.GetNumber(); timeSet=(h*3600+m*60+s)*1000;
if(!running||paused) { if(!(timeSet==0&&countDown)) { paused=false;
running=true; startTime=System.currentTimeMillis(); } } } void StopTimer() {
if(running) { if(!paused) { if(!countDown) pauseValue=diffTime; else
pauseValue=(currTime-startTime)/1000+pauseValue; } paused=true; } } void
ResetTimer() { running=false; pauseValue=diffTime=0; startTime=System.currentTimeMillis();
if(countDown) { long h=sb1.GetNumber(); long m=sb2.GetNumber(); long s=sb3.GetNumber();
timeSet=(h*3600+m*60+s)*1000; hours=(int)h; minutes=(int)m; seconds=(int)s;
} else hours=minutes=seconds=0; } void CalculateTime() { currTime=System.currentTimeMillis();
//Difference time in seconds if(!countDown) diffTime=(currTime-startTime)/1000+pauseValue;
else diffTime=((timeSet-(currTime-startTime))/1000)-pauseValue; hours=(int)(diffTime-diffTime
% 3600)/3600; minutes=(int)((diffTime-diffTime%60)/60) % 60; seconds=(int)diffTime
% 60; } void DrawTriangle(int x, int y, int w, int h) { int a[] =
{x,(x+w/2),x+w}; int b[] = {y-h,y,y-h}; gBuffer.fillPolygon(a, b, 3); } void
DrawCenteredArc(int cx, int cy, int r, int beginArc, int arc) { int x=cx-r;
int y=cy-r; gBuffer.drawArc(x-87,y-87,r*2,r*2,beginArc,arc); } void
DrawCircle() { gBuffer.setColor(Color.red); gBuffer.drawOval(7,12,174,174);
gBuffer.setColor(new Color(0,210,120)); int arc; int beginArc; //to test the
arc with seconds.. int unit=minutes; if(countDown) { beginArc=unit*6+90; arc=-unit*6;
if(hours>0)arc=360; else SetAngle(360-unit*6); } else { beginArc=90; arc=-unit*6;
SetAngle(unit*6); } for(int i=0;i<3;i++) { DrawCenteredArc(182, 186,
86+i, beginArc, arc); } int centerX=94; int centerY=99; int radius=87; ox=centerX;
oy=centerY; int x[]= new int[3]; int y[]= new int[3]; x[0]=-7; x[1]=7;
x[2]=-7; y[0]=-radius-7; y[1]=-radius; y[2]=-radius+7; RotatePolygon(x, y,
3); } int rotate_x(int x, int y) { return ((int) (ox + x * cos - y * sin));
} int rotate_y(int x, int y) { return ((int) (oy + y * cos + x * sin)); }
void SetAngle(double a) { radians = (a * 2 * Math.PI) / 360; cos = Math.cos(radians);
sin = Math.sin(radians); } void RotatePolygon(int x[], int y[], int n) { int
new_x[] = new int [3]; int new_y[] = new int [3]; for(int i=0; i<n; i++)
{ new_x[i]=rotate_x(x[i], y[i]); new_y[i]=rotate_y(x[i], y[i]); } gBuffer.fillPolygon(new_x,
new_y, n); } public boolean keyDown(Event e, int key) { if(key==10) input=false;
else if(input) message=message+(char)key; return true; } void DrawTextArea()
{ if(soundRunner>0&&blink) gBuffer.setColor(Color.red); else
gBuffer.setColor(Color.white); gBuffer.fillRect(20, 200, 150, 20); gBuffer.setColor(Color.orange);
if(framed) gBuffer.drawRect(18, 198, 153, 23); if(soundRunner>0&&blink)
gBuffer.setColor(Color.white); else gBuffer.setColor(Color.black); gBuffer.setFont(font5);
FontMetrics fm = gBuffer.getFontMetrics(font5); int stringWidth=fm.stringWidth(message);
if(input&&blink) gBuffer.drawString(message+"|",25,215);
else if(input&&!blink) gBuffer.drawString(message,25,215); else
gBuffer.drawString(message,95-stringWidth/2,215); } void DrawGauge() {
gBuffer.setColor(Color.green); gBuffer.fillRect(20, 222, 150, 10); int barL;
float frac; if(diffTime>0&&timeSet>0) { frac=(float)((timeSet-diffTime*1000)/(float)timeSet);
barL=(int)(frac*150); } else barL=0; if(paused) gBuffer.setColor(Color.blue);
else gBuffer.setColor(Color.red); if(countDown) { gBuffer.fillRect(20, 222,
barL, 10); } else if(running) { for(int i=-5;i<150;i+=6) gBuffer.fillRect(20+runner+i,
222, 3, 10); gBuffer.setColor(color1); gBuffer.fillRect(15,222,5,10);
gBuffer.fillRect(170,222,5,10); } } } Bu
örnekte kaydırma çubukları kullanılarak kullanıcıya seçim şansı
sunulmuş ve programın işleyişi kullanıcıya bağlı olarak değişmektedir.
geodetıc.java import
java.awt.*; import
java.applet.*; import
java.lang.*; import
java.net.*; import
geoWindow; public
class Geodetic2 extends Applet { geoWindow geoWin; Button button; Image image=null; MediaTracker tracker=new MediaTracker(this); int width,height; public void init() {
URL imageURL=null;
try{
imageURL=new URL(getCodeBase(),gwConst.LOGO);
} catch (MalformedURLException e)
{
imageURL=null;
image=null;
}
if(imageURL != null){
image=getImage(imageURL);
if(image != null)
tracker.addImage(image,0);
}
setFont(new Font(gwConst.FONTSTRING,Font.BOLD,gwConst.FONTHEIGHT));
add (button = new Button(gwConst.BUTTON));
width=size().width; height=size().height; button.move((width-button.size().width)/2,(width-button.size().width)/2); geoWin=new geoWindow(this); “
public void paint(Graphics g)
{
Color color=g.getColor();
g.setColor(Color.lightGray);
g.fill3DRect(0,0,size().width,size().height,true);
g.setColor(color);
if(image != null){
try{
tracker.waitForID(0); }catch
(InterruptedException e)
{
return;
}
g.drawImage(image,(width-image.getWidth(this))/2,button.size().height
+ (height-image.getHeight(this)-button.size().height)/3,this); FontMetrics fm=g.getFontMetrics(); g.drawString(gwConst.COPYRIGHT,(width-fm.stringWidth(gwConst.COPYRIGHT))/2,height-fm.getMaxDescent()-3);
}
} public boolean action(Event e, Object arg) {
if(arg.equals(gwConst.BUTTON) && null != geoWin) {
if(!geoWin.isShowing())
geoWin.show();
return true; }
else return false; }
Şekil 11.5 Menü.html import
VasTabs; import java.applet.Applet; import java.awt.*; public class
VasTabsApplet extends Applet { VasTabs tabs; Panel
mPan0,mPan1,mPan2,mPan3,mPan4,mPan5; Label lLogin,lPasswd,llState,lState,llReplay,lReplay;
TextField eLogin,ePasswd; Button bSend; //constructor public VasTabsApplet()
{ this.setLayout(null); this.resize(400,400); setBackground(Color.lightGray);
//where is the left upper corner of tabs=(25,25) //the dimension of tabs=(350,350)
tabs = new VasTabs(25,25,350,350); //Creating the Panel of the first tab
mPan0=new Panel(); mPan0.setLayout(null); lLogin = new Label("Login
:"); mPan0.add(lLogin); lLogin.reshape(10,30,100,21); eLogin = new
TextField("vasile"); mPan0.add(eLogin); eLogin.reshape(110,30,130,21);
lPasswd = new Label("Password :"); mPan0.add(lPasswd); lPasswd.reshape(10,60,100,21);
ePasswd = new TextField("password_here"); mPan0.add(ePasswd);
ePasswd.reshape(110,60,130,21); ePasswd.setEchoCharacter('*'); //Creating
the Panel of the second tab mPan1=new Panel(); mPan1.setLayout(null);
llState = new Label("State :"); mPan1.add(llState); llState.reshape(10,160,100,21);
lState = new Label("Just a test"); mPan1.add(lState); lState.reshape(110,160,390,21);
llReplay = new Label("Replay :"); mPan1.add(llReplay); llReplay.reshape(10,200,100,21);
lReplay = new Label("Register Now !"); mPan1.add(lReplay); lReplay.reshape(110,200,390,21);
bSend = new Button("Action !"); mPan1.add(bSend); bSend.reshape(110,90,80,30);
mPan2=new Panel(); mPan3=new Panel(); mPan4=new Panel(); mPan5=new Panel();
//adding the tabs tabs.addTab("Login", mPan0); tabs.addTab("Action!",
mPan1); tabs.addTab("T2", mPan2); tabs.addTab("Third",
mPan3); tabs.addTab("4", mPan4); tabs.addTab("the 5th avenue",
mPan5); add(tabs); }//end constructor }//end class VasTabsApplet |