ana sayfa : araştırma : java uygulamaları : renkler ve fontlar [ 7 / 12 ] |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GERİ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
7
RENKLER VE FONTLAR
7.1
Font Kontrolü
Oluşturacağımız java
appletlerin de değişik yazı türleri (fonts) kullanmamız gerekebilir. Bu
yazı türleri değişik boyut ve özelliklerde olabilir. Bu bölümde bu
ayarların nasıl yapılabileceğini inceleyeceğiz. JAVA dilinde, Sun
Microsystem tarafından hazırlanmış çeşitli alt programlar bulunur ve
biz bu alt programları kullanırız. Herhangi bir yazıyı
applet içerisinde değişik fontlar ve puntolarla yazabiliriz.bunun için
sisteme;
Bildirmek zorundayız. Bu işlemi
gerçekleştirebilmek için Font türünden bir değişken tanımlamak
gerekir. Bu tanımlamanın bir örneğini aşağıdaki satırda görüyorsunuz: Font yazı tipi = new
Font(“ TimesRoman”, Font.BOLD,18); Yukarıdaki program satırında
her üç özellik de bulunmakta.
Sun Microsystem firmasına
göre JAVA sistemindeki bir alt program olan GetFontList ile sistemde
bulunan bütün fontlar bulunabiliyor. Kullanılan yazı türü
ancak 4 değişik özelliğe sahip olabiliyor:
Aşağıdaki program kullanıcıya
değişik yazı tiplerini seçip kullanmayı sağlar. Combo Box kullanımından
faydalanılmaktadır. yazı.html <!--
saved from url=(0022)http://internet.e-mail --> <html> <head> <title>Chat</title> </head> <body> <CENTER> <TABLE BORDER><TR><TD> <applet code=aFont width=310 height=230> </applet> </TD></TR></TABLE> </CENTER> </body> </html> yazı.java import java.awt.*; import java.applet.*; public class aFont extends Applet { Choice
lFont
= new
Choice(); Choice lSize
= new
Choice(); Choice
lStyle
= new Choice(); TextField
sString = new TextField
("Javaside.com font test"); TextField
sChar = new
TextField ("20AC"); Font f
= null ; public void init () {
String [ ] arFont = getToolkit().getFontList(); for
(int i = 0; i < arFont.length; i++)
lFont.addItem (arFont[i]) ; lSize.addItem
("7") ; lSize.addItem
("8") ; lSize.addItem
("9") ; lSize.addItem
("10") ; lSize.addItem
("11") ; lSize.addItem
("12") ; lSize.addItem
("14") ; lSize.addItem
("16") ; lSize.addItem
("18") ; lSize.addItem
("20") ; lSize.addItem
("24") ; lSize.addItem
("28") ; lSize.addItem
("32") ; lSize.addItem
("36") ; lSize.addItem
("40") ; lSize.select(10); lStyle.addItem
("PLAIN") ; lStyle.addItem
("BOLD") ; lStyle.addItem
("ITALIC") ; lStyle.addItem
("BOLD+ITALIC") ; setLayout(null) ; int
iY = 10 ; Label l =
new Label("Font") ;
add( l ) ; l.reshape(5,
iY, 40, 20) ; add(lFont)
; lFont.reshape(50,
iY, 100, 20); l =
new Label("Size") ; add(
l ) ; l.reshape(170,
iY, 40, 20) ; add(lSize)
; lSize.reshape(220, iY, 60, 20); iY
+= 22 ;
l = new Label("Style") ;
add( l ) ; l.reshape(5,
iY, 40, 20) ; add(lStyle)
; lStyle.reshape(50,
iY, 100, 20); iY +=
22 ;
l = new Label("Texte") ; add(
l ) ; l.reshape(5,
iY, 40, 20) ; add(sString)
; sString.reshape(50, iY, 100, 20); l
= new Label("char") ;
add( l ) ; l.reshape(170,
iY, 40, 20) ; add(sChar)
; sChar.reshape(220,
iY, 60, 20); repaint()
; } public
boolean handleEvent (Event event) {
if ( event.id
== Event.ACTION_EVENT
&& ( (event.target == sString) || (event.target == sChar) || (event.target
== lFont) || (event.target == lSize) ||
(event.target == lStyle) )
) { repaint() ;
return true; } return
super.handleEvent (event); }
public void paint(Graphics g) {
f = new Font (lFont.getSelectedItem (), lStyle.getSelectedIndex()
Integer.parseInt(lSize.getSelectedItem())
); g.setColor(
Color.white ) ; g.fillRect(0,85,400,200);
g.setColor( Color.black ) ; if (f
!= null) { g.setFont(f);
} g.drawString(sString.getText(), 20, 120); String
s = sChar.getText() ; char c ; try
{ c = (char)Integer.parseInt(s,
16) ;
if (Character.isDefined(c))
g.drawString("char else
g.drawString("char
}
catch(Exception e) {
g.drawString(""+e , 20, 180);}
}}
Şekil 7.1 Yazı.html 7.2
Renk Kontrolü
Tablo
7.2 Java’da kullanılan renkler.
Eğer appletlerimiz için
bu renkler yetersiz kalıyorsa bizler istediğimiz kombinasyonla yeni
renkler tanımlayabiliriz. Bu işlem için:
Biçiminde
bir komut vermemiz gerekir.
Aşağıdaki program yazılan kelimelerin herbir
harfinin farklı renkte görünmesini ve yanıp sönmesini sağlamaktadır. Rainbow.java import java.awt.*; public class RainbowText extends java.applet.Applet implements
Runnable { String
str = null; int strlen; Thread
runner = null; char
theChars[]; int charOffsets[]; Color
colors[]; int phase = 0; Image
offScreenImage; Graphics
offScreenG; Font f; FontMetrics
fm; public void init() { String
paramStr = null; float
h; int
xPos=20; str =
getParameter("text"); if (str
== null) {
str = "Museum of Java Applets"; } f=new
Font("TimesRoman",Font.BOLD,36); fm=getFontMetrics(f); resize(40+fm.stringWidth(str),40); setBackground(Color.black); strlen
= str.length(); theChars
= new char [strlen]; charOffsets
= new int [strlen]; str.getChars(0,strlen,theChars,0); colors
= new Color[strlen]; for (int
i = 0; i < strlen; i++) { h
= ((float)i)/((float)strlen); colors[i]
= new Color(Color.HSBtoRGB(h,1.0f,1.0f)); charOffsets[i]
= xPos; xPos+=fm.charWidth(theChars[i]); } offScreenImage
= createImage(this.size().width,this.size().height); offScreenG
= offScreenImage.getGraphics(); offScreenG.setFont(f); } public void start() { if(runner
== null) { runner
= new Thread(this); runner.start(); } } public void stop() { if (runner
!= null) { runner.stop(); runner
= null; } } public void run() { while (runner
!= null) {
try {
Thread.sleep(100);
}
catch (InterruptedException
e) { }
repaint(); } } public
void update(Graphics g) { int
x, y; offScreenG.setColor(Color.black); offScreenG.fillRect(0,0,this.size().width,this.size().height); phase++; phase%=str.length(); for(int
i=0;i<strlen;i++)
{
x = charOffsets[i];
offScreenG.setColor(colors[(phase+i)%strlen]);
offScreenG.drawChars(theChars,i,1,x,30);
} paint(g); } public
void paint(Graphics g) {
g.drawImage(offScreenImage,0,0,this); } }
Şekil 7.3
Rainbow.html <html><head><title>II´´´´Rainbow
Applet````II</title> </head><body background="" bgcolor=#000000 link=#ff0000 vlink=#ff0000
text=#FFFFFF> <BR> <FONT SIZE="+2"><CENTER><B><I>Rainbow
applets in appplet14.zip as rainb.zip</I></B></CENTER></FONT> <BR> <CENTER><applet code="RainbowText.class"
width=530 height=80> <param name=text value="GateWay III Homepage
Heaven"> <param name=direction value="1"> <param name=horizontalradius value="12"> <param name=verticalradius value="12"> </applet> </CENTER> <BR> <BR> <A HREF="cafe7.htm"><FONT SIZE="+2"><CENTER><B><I>Back
To Cafe7</I></B></CENTER></FONT></A> <BR> <BR> </BODY> </HTML> |