Documente Academic
Documente Profesional
Documente Cultură
Fig. A1.
Programul poate fi executat, la alegere, folosind interpreterul sau browserul integrat.
Această alegere se face în fereastra care apare atunci cînd se selectează
Execute.
import java.io.*;
class ContBancar {
// variabile instanţe
private double balanta_;
private int id_;
char c;
System.out.println("a=" + a.toString() );
System.out.println("b=" + b.toString() );
a.retragere(50000);
b.depunere(300000);
System.out.println("a=" + a);
System.out.println("b=" + b);
System.exit(0);
}
}
Aplicaţia 2. Se prezintă codul sursă al unei aplicaţii de tip applet, care de această dată
va fi executat folosind opţiunea cu browser. Pe ecran va apare o fereastră specifică
appleturilor Java în care textul ("Sistem inteligent", "Internet service") divers colorat
şi avînd ca efect special sclipirea (blinking) va apărea şi va dispărea aleator.
/*
Apare text
*/
import java.awt.*;
import java.util.*;
import java.lang.*;
String att;
Dimension d = size();
att = getParameter("speed");
speed = (att == null) ? 50 : Integer.valueOf(att).intValue();
font1 = new java.awt.Font("TimesRoman", Font.ITALIC, 24);
att = getParameter("message1");
message1 = (att == null) ? "Internet Service" : att;
lastX1 = (int)(Math.random() * (d.width - 1));
lastY1 = (int)((d.height - font1.getSize() - 1) * Math.random());
directX1 = 3;
directY1 = 3;
font2 = new java.awt.Font("TimesRoman", Font.PLAIN, 20);
att = getParameter("message2");
message2 = (att == null) ? "Sistem Inteligent" : att;
lastX2 = (int)(Math.random() * (d.width - 1));
lastY2 = (int)((d.height - font2.getSize() - 1) * Math.random());
directX2 = -3;
directY2 = -3;
}
public void start() {
/* Background color */
this.setBackground(Color.black);
/* Start fir de executie */
if (blinker == null) {
blinker = new Thread(this, "Blink");
blinker.start();
}
}
Dimension d = size();
StringTokenizer t;
FontMetrics fm;
g.setColor(Color.black);
g.setFont(font1);
Aplicaţii în Java 136
fm = g.getFontMetrics();
space = fm.stringWidth(" ");
x = lastX1;
y = lastY1;
for (t = new StringTokenizer(message1) ;
t.hasMoreTokens() ; ) {
g.drawString(word, x, y);
x += w;
}
if (Math.random() > 0.99) {
directX2 = -directX2;
}
lastX2 += directX2;
if (lastX2 >= d.width) {
lastX2 = 0;
} else if (lastX2 < 0) {
lastX2 = d.width - 1;
}
lastY2 += directY2;
if (lastY2 >= d.height) {
directY2 = -3;
} else if (lastY2 < font1.getSize()) {
directY2 = 3;
}
}
public void stop() {
blinker = null;
blinker.stop();
}
public void run() {
while (blinker != null) {
repaint();
try {
blinker.sleep(speed);
} catch (InterruptedException e)
{}
}
}
}
Aplicaţii în Java 138
import java.awt.*;
import java.util.StringTokenizer;
<APPLET CODE="AnimText" WIDTH=600 HEIGHT=400>
<PARAM NAME=text VALUE="Hello">
<PARAM NAME=font VALUE="TimesRoman">
<PARAM NAME=type VALUE=1>
<PARAM NAME=min VALUE=6>
<PARAM NAME=max VALUE=28>
<PARAM NAME=naptime VALUE=100>
<PARAM NAME=align VALUE=left>
</APPLET>
Parameters:
text - the text to display
font - the font to render the text in
style - whether PLAIN, BOLD or ITALIC
step - increments in font size each iteration
type - blink (all chars same rate)
wave ("the wave")
random (random)
align - left, center or right
bgimage - background image URL
bgcolor - background color (as RGB)
fgcolor - foreground color
naptime - time between iterations in millisecs
min - minimum font size
max - maximum font size
@Autorul după care s-a prelucrat: Suresh Srinivasan
(suresh@thomtech.com) */
Integer intObj;
s = getParameter("text");
if (s == null)
s = defaultString;
textChars = new char[s.length()];
s.getChars(0 , s.length(), textChars, 0);
s = getParameter("font");
if (s == null)
fontString = "TimesRoman";
else if (s.equalsIgnoreCase("TimesRoman"))
fontString = "TimesRoman";
else if (s.equalsIgnoreCase("Courier"))
fontString = "Courier";
else if (s.equalsIgnoreCase("Helvetica"))
fontString = "Helvetica";
else if (s.equalsIgnoreCase("Dialog"))
fontString = "Dialog";
else
fontString = "TimesRoman";
s = getParameter("style");
if (s == null)
style = Font.PLAIN;
else if (s.equalsIgnoreCase("PLAIN"))
style = Font.PLAIN;
else if (s.equalsIgnoreCase("BOLD"))
style = Font.BOLD;
else if (s.equalsIgnoreCase("ITALIC"))
style = Font.ITALIC;
else
style = Font.PLAIN;
s = getParameter("type");
if (s == null)
type = TYPE_WAVE;
else if (s.equalsIgnoreCase("blink"))
type = TYPE_BLINK;
else if (s.equalsIgnoreCase("wave"))
type = TYPE_WAVE;
else if (s.equalsIgnoreCase("random"))
type = TYPE_RANDOM;
else
type = TYPE_WAVE;
s = getParameter("align");
141 Anexa
if (s == null)
align = ALIGN_CENTER;
else if (s.equalsIgnoreCase("left"))
align = ALIGN_LEFT;
else if (s.equalsIgnoreCase("center"))
align = ALIGN_CENTER;
else if (s.equalsIgnoreCase("right"))
align = ALIGN_RIGHT;
else
align = ALIGN_CENTER;
try {
intObj = new Integer(getParameter("width"));
Width = intObj.intValue();
} catch (Exception e) {
Width = defaultWidth;
}
try { intObj = new Integer(getParameter("height"));
Height = intObj.intValue();
} catch (Exception e) {
Height = defaultHeight;
}
try { intObj = new Integer(getParameter("min"));
min = intObj.intValue();
} catch (Exception e) {
min = defaultMin;
}
try { intObj = new Integer(getParameter("max"));
max = intObj.intValue();
} catch (Exception e) {
max = defaultMax;
}
if (min >= max || min <= 0) {
min = defaultMin;
max = defaultMax;
}
try {intObj = new Integer(getParameter("step"));
step = intObj.intValue();
} catch (Exception e) {
step = defaultStep;
}
if (step > (max-min)/2) step = defaultStep;
try { intObj = new Integer(getParameter("naptime"));
Aplicaţii în Java 142
naptime = intObj.intValue();
} catch (Exception e) {
naptime = defaultNaptime;
}
if (naptime <= 0) naptime = defaultNaptime;
s = getParameter("fgColor");
if (s != null) st = new StringTokenizer(s, rgbDelimiter);
if (s == null)
fgColor = Color.black;
else if (s.equalsIgnoreCase("red"))
fgColor = Color.red;
else if (s.equalsIgnoreCase("blue"))
fgColor = Color.blue;
else if (s.equalsIgnoreCase("green"))
fgColor = Color.green;
else if (s.equalsIgnoreCase("yellow"))
fgColor = Color.yellow;
else if (s.equalsIgnoreCase("white"))
fgColor = Color.white;
else if (s.equalsIgnoreCase("orange"))
fgColor = Color.orange;
else if (s.equalsIgnoreCase("cyan"))
fgColor = Color.cyan;
else if (s.equalsIgnoreCase("magenta"))
fgColor = Color.magenta;
else if (st.countTokens() == 3) {
Integer r = new Integer(st.nextToken());
Integer g = new Integer(st.nextToken());
Integer b = new Integer(st.nextToken());
fgColor = new Color(r.intValue(), g.intValue(), b.intValue());
} else
fgColor = Color.black;
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;
143 Anexa
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 (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;
/* alocari initiale*/
n = max-min;
if (n>0) {
fonts = new Font[n];
current = new int[textChars.length];
direction = new int[textChars.length];
charWidth = new int[textChars.length];
}
for (int i=0; i<n; i++) {
fonts[i] = new Font(fontString, style, min+i);
}
for (int i=0; i<textChars.length; i++) {
switch (type) {
case TYPE_BLINK:
current[i] = 0;
direction[i] = 1;
break;
case TYPE_WAVE:
current[i]=(int)(Math.sin((double)i/(double)textChars.length*Math.PI)*
(float)(n-1));
direction[i] = 1;
break;
case TYPE_RANDOM:
current[i] = (int)(Math.random()*(float)(n));
direction[i] = 1;
Aplicaţii în Java 144
break;
default:
}
if (current[i] >= n-1) direction[i] = -1;
}
/* contextul grafic */
try {
offI = createImage(maxWidth, maxHeight);
offG = offI.getGraphics();
} catch (Exception e) {
offG = null;
}
}
public void start() {
if (thread == null) {
thread = new Thread(this);
thread.start();
}
}
public void run() {
while ((n>0) && (thread != null)) {
repaint();
try { Thread.sleep(naptime); } catch (Exception e) { }
readyToPaint = false;
next();
readyToPaint = true;
}
}
/* urmatoarea iteratie */
public void next() {
for (int i=0; i<textChars.length; i++) {
current[i] += step*direction[i];
if (current[i] >= n-1) {
current[i] = 2*n-2-current[i];
direction[i] = -1;
}
if (current[i] <= 0) {
current[i] = Math.abs(current[i]);
direction[i] = 1;
}
}
}
145 Anexa
totalWidth += charWidth[i];
}
if (totalWidth>maxWidth) totalWidth = maxWidth;
charHeight = g.getFontMetrics().getHeight();
if (charHeight>maxHeight) charHeight = maxHeight;
resize(Width, Height);
resized = true;
}
int pos = 0;
switch (align) {
case ALIGN_LEFT:
pos = leader; break;
case ALIGN_CENTER:
pos = (size().width-totalWidth)/2; break;
case ALIGN_RIGHT:
pos = (size().width-totalWidth-leader); break;
default:
}
g.setColor(bgColor);
g.fillRect(0, 0, size().width-1, size().height-1);
g.setColor(fgColor);
Aplicaţii în Java 146
import java.util.*;
import java.awt.*;
import java.applet.*;
g.drawLine(x0+x,y0+y,x0+x,y0+y);
g.drawLine(x0+y,y0+x,x0+y,y0+x);
g.drawLine(x0+y,y0-x,x0+y,y0-x);
g.drawLine(x0+x,y0-y,x0+x,y0-y);
g.drawLine(x0-x,y0-y,x0-x,y0-y);
g.drawLine(x0-y,y0-x,x0-y,y0-x);
g.drawLine(x0-y,y0+x,x0-y,y0+x);
g.drawLine(x0-x,y0+y,x0-x,y0+y);
}
// construcţia cercului
x=0;
y=r;
d=5/4-r;
plotpoints(x0,y0,x,y,g);
while (y>x){
if (d<0) {
d=d+2*x+3;
x++;
}
else {
d=d+2*(x-y)+5;
x++;
y--;
}
plotpoints(x0,y0,x,y,g);
}
}
// programul principal
s = dat.getSeconds();
m = dat.getMinutes();
h = dat.getHours();
today = dat.toLocaleString();
xcenter=80;
ycenter=55;
g.setColor(Color.lightGray);
if (xs != lastxs || ys != lastys) {
g.drawLine(xcenter, ycenter, lastxs, lastys);
g.drawString(lastdate, 5, 125);
}
if (xm != lastxm || ym != lastym) {
g.drawLine(xcenter, ycenter-1, lastxm, lastym);
g.drawLine(xcenter-1, ycenter, lastxm, lastym); }
if (xh != lastxh || yh != lastyh) {
g.drawLine(xcenter, ycenter-1, lastxh, lastyh);
g.drawLine(xcenter-1, ycenter, lastxh, lastyh); }
149 Anexa
g.setColor(Color.darkGray);
g.drawString(today, 5, 125);
g.drawLine(xcenter, ycenter, xs, ys);
g.setColor(Color.blue);
g.drawLine(xcenter, ycenter-1, xm, ym);
g.drawLine(xcenter-1, ycenter, xm, ym);
g.drawLine(xcenter, ycenter-1, xh, yh);
g.drawLine(xcenter-1, ycenter, xh, yh);
lastxs=xs; lastys=ys;
lastxm=xm; lastym=ym;
lastxh=xh; lastyh=yh;
lastdate = today;
}