text color

design of the background and text colors for

HTML pages. Provides an easy interface for selecting the colors and

to In my HTML document . so i can't able to getting text colors...

please help me

import java.awt.*;

class Selector extends Panel

{

ColorChooser app;

Label laCol = new Label();

Scrollbar sbCol = new Scrollbar(Scrollbar.HORIZONTAL, 255, 8, 0, 255);

TextField tfDec = new TextField("255", 3);

TextField tfHex = new TextField("FF", 2);

Selector(ColorChooser ap, String label, Color col)

{

app = ap;

laCol.setText(label);

GridBagLayout gbLay = new GridBagLayout();

GridBagConstraints gbCon = new GridBagConstraints();

setLayout(gbLay);

gbCon.anchor = GridBagConstraints.WEST;

gbCon.gridwidth = GridBagConstraints.REMAINDER;

gbCon.weightx = 1.0;

gbLay.setConstraints(laCol, gbCon);

add(laCol);

gbCon.fill = GridBagConstraints.BOTH;

gbCon.gridwidth = 1;

gbLay.setConstraints(sbCol, gbCon);

sbCol.setBackground(col);

add(sbCol);

gbCon.fill = GridBagConstraints.NONE;

gbCon.insets = new Insets(5, 5, 5, 5);

gbCon.weightx = 0.0;

gbLay.setConstraints(tfHex, gbCon);

add(tfHex);

gbLay.setConstraints(tfDec, gbCon);

add(tfDec);

setBackground(Color.lightGray);

}

public boolean handleEvent(Event ev)

{

if (ev.target == sbCol)

{

int val = sbCol.getValue();

if (ev.id == Event.MOUSE_MOVE ||

ev.id == Event.MOUSE_ENTER ||

ev.id == Event.MOUSE_EXIT)

return true;

tfHex.setText(app.convHex(val));

tfDec.setText(Integer.toString(val));

return true;

}

else

return super.handleEvent(ev);

}

public boolean action(Event ev, Object ob)

{

try

{

int value = Integer.parseInt((String)ob,

(ev.target == tfDec)? 10 : 16);

if (value >= 0 && value <= 255)

sbCol.setValue(value);

}

catch (NumberFormatException e)

{

}

return postEvent(new Event(sbCol, Event.SCROLL_ABSOLUTE, null));

}

void setValue(int value)

{

sbCol.setValue(value);

postEvent(new Event(sbCol, Event.SCROLL_ABSOLUTE, null));

}

}

interface CommonVars

{

static int BGCOLOR = 0;

static int TEXT = 1;

static int LINK = 2;

static int VLINK = 3;

static int ALINK = 4;

static int RED = 0;

static int GREEN = 1;

static int BLUE = 2;

}

class Example extends Panel implements CommonVars

{

ColorChooser app;

Color[] colArr = new Color[5];

String[] msg = {"" , "This is an example of normal text",

"This is a new hyperlink",

"This is a visited hyperlink",

"This is an active hyperlink"};

public void paint(Graphics g)

{

int index;

for (index = BGCOLOR ; index <= ALINK ; index++)

colArr[index] = new Color(app.colors[index][RED],

app.colors[index][GREEN],

app.colors[index][BLUE]);

Font font = new Font("TimesRoman", Font.PLAIN, 20);

FontMetrics fm = g.getFontMetrics(font);

int fnHeight = fm.getHeight();

Dimension dim = size();

g.setColor(colArr[BGCOLOR]);

g.fillRect(0, 0, dim.width, dim.height);

if (app.img != null && app.cbCol.getState())

{

int imWidth = app.img.getWidth();

int imHeight = app.img.getHeight();

if (imWidth > 0 && imHeight > 0)

for (int x = 0 ; x < dim.width ; x += imWidth)

for (int y = 0 ; y < dim.height ;

y += imHeight)

g.drawImage(app.img, x,y, this);

else

g.drawImage(app.img, 0, 0, this);

}

g.setFont(font);

for (index = TEXT ; index <= ALINK ; index++)

{

g.setColor(colArr[index]);

g.drawString(msg[index],

(dim.width - fm.stringWidth(msg[index]))

/ 2,

index * ((dim.height - 4 * fnHeight) / 5

+ fnHeight));

}

}

}

public class ColorChooser extends java.applet.Applet implements CommonVars

{

Label laOpt = new Label("ColorChooser 1.1");

Choice chTyp = new Choice();

Selector[] selArr = new Selector[3];

Checkbox cbCol = new Checkbox("Background URL:");

TextField tfImg = new TextField(80);

TextArea taTag = new TextArea(3, 50);

Example paExa = new Example(this);

int[][] colors = {{255,255,255},{0,0,0},{255,0,0},{255,184,115},{200,200,200}};

int actual = BGCOLOR;

Image img = null;

public void init()

{

taTag.setEditable(false);

selArr[RED] = new Selector(this, "Red:", Color.red);

selArr[GREEN] = new Selector(this, "Green:", Color.green);

selArr[BLUE] = new Selector(this, "Blue:", Color.blue);

chTyp.addItem("Background color");

chTyp.addItem("Normal text color");

chTyp.addItem("Hyperlink color");

chTyp.addItem("Visited hyperlink color");

chTyp.addItem("Active hyperlink color");

GridBagLayout gbLay = new GridBagLayout();

GridBagConstraints gbCon = new GridBagConstraints();

setLayout(gbLay);

gbCon.fill = GridBagConstraints.BOTH;

gbLay.setConstraints(laOpt, gbCon);

add(laOpt);

gbCon.gridwidth = GridBagConstraints.REMAINDER;

gbCon.weightx = 1.0;

gbLay.setConstraints(chTyp, gbCon);

add(chTyp);

gbCon.fill = GridBagConstraints.BOTH;

gbCon.weightx = 0.0;

for (int index = RED ; index <= BLUE ; index++)

{

gbLay.setConstraints(selArr[index], gbCon);

add(selArr[index]);

}

gbCon.fill = GridBagConstraints.HORIZONTAL;

gbCon.gridwidth = 1;

gbCon.insets = new Insets(5, 0, 5, 5);

gbLay.setConstraints(cbCol, gbCon);

cbCol.setBackground(Color.lightGray);

add(cbCol);

gbCon.gridwidth = GridBagConstraints.REMAINDER;

gbLay.setConstraints(tfImg, gbCon);

add(tfImg);

gbLay.setConstraints(taTag, gbCon);

add(taTag);

gbCon.fill = GridBagConstraints.BOTH;

gbCon.weighty = 1.0;

gbLay.setConstraints(paExa, gbCon);

add(paExa);

setBackground(Color.lightGray);

}

public Insets insets()

{

return new Insets(10, 10, 10, 10);

}

public boolean action(Event ev, Object ob)

{

if (ev.target == chTyp)

{

int sel = chTyp.getSelectedIndex();

for (int index = RED ; index <= BLUE ; index++)

{

colors[actual][index] = selArr[index].getValue();

selArr[index].setValue(colors[sel][index]);

}

actual = sel;

repaint();

return true;

}

else if (ev.target == cbCol)

{

if (cbCol.getState() && img == null)

cbCol.setState(false);

repaint();

return true;

}

else if (ev.target == tfImg)

{

img = getImage(getDocumentBase(), tfImg.getText());

cbCol.setState(img != null);

repaint();

return true;

}

return false;

}

public void paint(Graphics g)

{

int sel = chTyp.getSelectedIndex();

for (int index = RED ; index <= BLUE ; index++)

colors[sel][index] = selArr[index].getValue();

String tag = "<BODY BGCOLOR=" + stdHex(colors[BGCOLOR])

+ " TEXT=" + stdHex(colors[TEXT]) + "\n"

+ " LINK=" + stdHex(colors[LINK])

+ " VLINK=" + stdHex(colors[VLINK])

+ " ALINK=" + stdHex(colors[ALINK]);

if (cbCol.getState())

tag += "\nBACKGROUND=\"" + tfImg.getText() + "\"";

tag += ">";

taTag.setText(tag);

paExa.repaint();

}

static String convHex(int value)

{

String aux = Integer.toString(value,16).toUpperCase();

if (aux.length() == 1)

aux = "0" + aux;

return aux;

}

static String stdHex(int[] rgb)

{

return "#" + convHex(rgb[RED]) + convHex(rgb[GREEN])

+ convHex(rgb[BLUE]);

}

public String getAppletInfo()

{

return "ColorChooser ";

}

}>

[8257 byte] By [tadi] at [2007-9-26 3:33:12]
# 1

Hi Tadi,

what i understood from your programme is you are using interface for selecting the colors, and creates the HTML tag in a TextField , and also you are reffering to applet.

Here you are assiging hexadecimal value and decimal value to textfiled, after that you are intializing it by using contructor.

In GridBagLayout you are doing gbLay.setConstraints(sbCol, gbCon); sbCol.setBackground(col); .So upto now no problem in your code part , while handling events you have to have use repaint method for particular color chooser bocz it depends on browser.

For example: after this tfDec.setText(Integer.toString(val));

you have to have this repaint method with your color chooser.

app.repaint();

return true;

In your programme you are not using repaint method.

I had gone through your code as of my knowledge its okay except this one. Still if you have any problem please post the query.

Here i modyfied your code

import java.awt.*;

class Selector extends Panel

{

ColorChooser app;

Label laCol = new Label();

Scrollbar sbCol = new Scrollbar(Scrollbar.HORIZONTAL, 255, 8, 0, 255);

TextField tfDec = new TextField("255", 3);

TextField tfHex = new TextField("FF", 2);

Selector(ColorChooser ap, String label, Color col)

{

app = ap;

laCol.setText(label);

GridBagLayout gbLay = new GridBagLayout();

GridBagConstraints gbCon = new GridBagConstraints();

setLayout(gbLay);

gbCon.anchor = GridBagConstraints.WEST;

gbCon.gridwidth = GridBagConstraints.REMAINDER;

gbCon.weightx = 1.0;

gbLay.setConstraints(laCol, gbCon);

add(laCol);

gbCon.fill = GridBagConstraints.BOTH;

gbCon.gridwidth = 1;

gbLay.setConstraints(sbCol, gbCon);

sbCol.setBackground(col);

add(sbCol);

gbCon.fill = GridBagConstraints.NONE;

gbCon.insets = new Insets(5, 5, 5, 5);

gbCon.weightx = 0.0;

gbLay.setConstraints(tfHex, gbCon);

add(tfHex);

gbLay.setConstraints(tfDec, gbCon);

add(tfDec);

setBackground(Color.lightGray);

}

public boolean handleEvent(Event ev)

{

if (ev.target == sbCol)

{

int val = sbCol.getValue();

if (ev.id == Event.MOUSE_MOVE ||

ev.id == Event.MOUSE_ENTER ||

ev.id == Event.MOUSE_EXIT)

return true;

tfHex.setText(app.convHex(val));

tfDec.setText(Integer.toString(val));

app.repaint();

return true;

}

else

return super.handleEvent(ev);

}

public boolean action(Event ev, Object ob)

{

try

{

int value = Integer.parseInt((String)ob,

(ev.target == tfDec)? 10 : 16);

if (value >= 0 && value <= 255)

sbCol.setValue(value);

}

catch (NumberFormatException e)

{

}

return postEvent(new Event(sbCol, Event.SCROLL_ABSOLUTE, null));

}

void setValue(int value)

{

sbCol.setValue(value);

postEvent(new Event(sbCol, Event.SCROLL_ABSOLUTE, null));

}

}

interface CommonVars

{

static int BGCOLOR = 0;

static int TEXT = 1;

static int LINK = 2;

static int VLINK = 3;

static int ALINK = 4;

// Items to control.

static int RED = 0;

static int GREEN = 1;

static int BLUE = 2;

}

class Example extends Panel implements CommonVars

{

ColorChooser app;

Color[] colArr = new Color[5];

String[] msg = {"" , "This is an example of normal text",

"This is a new hyperlink",

"This is a visited hyperlink",

"This is an active hyperlink"};

public void paint(Graphics g)

{

int index;

for (index = BGCOLOR ; index <= ALINK ; index++)

colArr[index] = new Color(app.colors[index][RED],

app.colors[index][GREEN],

app.colors[index][BLUE]);

Font font = new Font("TimesRoman", Font.PLAIN, 20);

FontMetrics fm = g.getFontMetrics(font);

int fnHeight = fm.getHeight();

Dimension dim = size();

g.setColor(colArr[BGCOLOR]);

g.fillRect(0, 0, dim.width, dim.height);

if (app.img != null && app.cbCol.getState())

{

int imWidth = app.img.getWidth(this);

int imHeight = app.img.getHeight(this);

if (imWidth > 0 && imHeight > 0)

for (int x = 0 ; x < dim.width ; x += imWidth)

for (int y = 0 ; y < dim.height ;

y += imHeight)

g.drawImage(app.img, x,y, this);

else

g.drawImage(app.img, 0, 0, this);

}

g.setFont(font);

for (index = TEXT ; index <= ALINK ; index++)

{

g.setColor(colArr[index]);

g.drawString(msg[index],

(dim.width - fm.stringWidth(msg[index]))

/ 2,

index * ((dim.height - 4 * fnHeight) / 5

+ fnHeight));

}

}

}

I hope this will help you out.

Regards,

Tirumalarao.

Developer Technical Support,

Sun Microsystem,India.

rao_indts at 2007-6-29 12:01:30 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...