controlling bliknking text .HELP!

im trying to write a program that flashes text and allows user to control blinking speed, and change background color....

im hopelessly confused....

import java.awt.*;

import java.util.*;

import javax.swing.JPanel;

import java.awt.FlowLayout;

publicclass FlashTextextends java.applet.Applet

{

private Timer timer;// Schedules the blinking

private String labelString;// The label for the window

privateint delay;

private JList colorList;//list to display color

privatefinal String colorNames[] ={"Black","Blue","Red","Green","Yellow"};

privatefinal Color colors[] ={ Color.BACK, Color.BLUE, Color.RED, Color.GREEN,Color.YELLOW};

// the delay time between blinks

publicvoid init()

{

super("List test" );

setLayout (new FlowLayout() );

colorJList =new JList( colorNames );

colorJList.setVisibleRowCount(5);

//do not allo multiple selections

colorJList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );

//add a JScrollPane containing JList to frame

add(new JScrollPane( colorJList ) );

colorList.addListSelectionListener(new ListSelectionListener()

{

publicvoid valueChanged( ListSelectionEvent event )

{

getContentPane().setBackground(

colors[ colorJList.getSelectedIndex() ] );

}//end method valueChange

}//end

);//end call to addListSelectionListener

String blinkFrequency = getParameter("speed");

delay = (blinkFrequency ==null) ? 400 :

(1000 / Integer.parseInt(blinkFrequency));

labelString = getParameter("lbl");

if (labelString ==null)

labelString ="Flashing text";

Font font =new java.awt.Font("Serif", Font.PLAIN, 24);

setFont(font);

}

publicvoid start()

{

timer =new Timer();//creates a new timer to schedule the blinking

timer.schedule(new TimerTask(){//creates a timertask to schedule

// overrides the run method to provide functionality

publicvoid run(){

repaint();

}

}

, delay, delay);

}

public Timer(int delay)

{

timer =new Timer(1000,new ActionListener() );

timer.start();

new ActionListener()// anonymous inner class

{

publicvoid actionPerformed( ActionEvent e )

{

publicvoid setDelay(int delay)

{

this.delay = delay;

}//end method setDelay

publicint getDelay(int n)

{

delay = -1;

if ((n >= 0) && (n < 30))

{

delay = ((Hello World) frames.get(n)).delay;

}

return delay;

}//end method getDelay

}//end method actionPerformed

}

}//end class Timer

publicvoid paint(Graphics g)

{

int fontSize = g.getFont().getSize();

int x = 0, y = fontSize, space;

int red =(int) ( 50 * Math.random());

int green = (int) ( 50 * Math.random());

int blue = (int) (256 * Math.random());

Dimension d = getSize();

g.setColor(Color.black);

FontMetrics fm = g.getFontMetrics();

space = fm.stringWidth(" ");

for (StringTokenizer t =new StringTokenizer(labelString);

t.hasMoreTokens();)

{

String word = t.nextToken();

int w = fm.stringWidth(word) + space;

if (x + w > d.width){

x = 0;

y += fontSize;//move word to next line if it doesn't fit

}

if (Math.random() < 0.5)

g.setColor(new java.awt.Color((red + y*30) % 256,

(green + x/3) % 256, blue));

else

g.setColor(getBackground());

g.drawString("flashing text!", x, y);

x += w;//shift to the right to draw the next word

}

}

publicvoid stop()

{

timer.cancel();//stops the timer

}

}

[8069 byte] By [Lizanea] at [2007-11-27 7:56:27]
# 1
What is your question?
Navy_Codera at 2007-7-12 19:38:03 > top of Java-index,Java Essentials,New To Java...
# 2

> public Timer(int delay){

Timer is a Swing API class. It should not be your app method.

> }//end class Timer

Your app class Timer is not defined in your current code. And you should not.

For proper usage of javax.swing.Timer, read the API documentation of the class and http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html

I did a quick and hasty coding. I hope you try and study the code below:

/*

<applet code="FlashText" width="700" height="700"></applet>

*/

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import java.util.StringTokenizer;

public class FlashText extends JApplet implements ActionListener{

private javax.swing.Timer timer;

private String labelString, blinkFrequency;

private int delay;

private JList colorList;

private final String colorNames[]

= {"Black","Blue", "Red", "Green", "Yellow"};

private final Color colors[]

= {Color.BLACK, Color.BLUE, Color.RED, Color.GREEN,Color.YELLOW};

private Container con;

private FlashPanel fp;

public void init(){

con = getContentPane();

con.setLayout(new FlowLayout());

fp = new FlashPanel();

con.add(fp);

colorList = new JList(colorNames);

colorList.setVisibleRowCount(5);

colorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

con.add(new JScrollPane(colorList));

colorList.addListSelectionListener(new ListSelectionListener(){

public void valueChanged(ListSelectionEvent event){

fp.setBackground(colors[colorList.getSelectedIndex()]);

}

});

blinkFrequency = getParameter("speed");

delay = (blinkFrequency == null) ? 400 :

(1000 / Integer.parseInt(blinkFrequency));

labelString = getParameter("lbl");

if (labelString == null){

labelString = "Flashing text";

}

Font font = new Font("Serif", Font.PLAIN, 24);

setFont(font);

}

public void start(){

timer = new javax.swing.Timer(delay, this);

timer.start();

}

public void actionPerformed(ActionEvent e){

fp.repaint();

}

public class FlashPanel extends JPanel{

public FlashPanel(){

setPreferredSize(new Dimension(500, 500));

}

public void paintComponent(Graphics g){

super.paintComponent(g);

int fontSize = g.getFont().getSize();

int x = 0, y = fontSize, space;

int red =(int) (50 * Math.random());

int green = (int) (50 * Math.random());

int blue = (int) (256 * Math.random());

Dimension d = getSize();

g.setColor(Color.black);

FontMetrics fm = g.getFontMetrics();

space = fm.stringWidth(" ");

for (StringTokenizer t

= new StringTokenizer(labelString); t.hasMoreTokens();){

String word = t.nextToken();

int w = fm.stringWidth(word) + space;

if (x + w > d.width) {

x = 0;

y += fontSize; //move word to next line if it doesn't fit

}

if (Math.random() < 0.5)

g.setColor(new java.awt.Color((red + y*30) % 256,

(green + x/3) % 256, blue));

else

g.setColor(getBackground());

g.drawString("flashing text!", x, y);

x += w; //shift to the right to draw the next word

}

}

}

public void stop(){

timer.stop(); //stops the timer

}

}

Message was edited by:

hiwa

hiwaa at 2007-7-12 19:38:03 > top of Java-index,Java Essentials,New To Java...
# 3

i compiled the following and it flashes the text, but it doesent allow the user to control the blinking speed and also it doesent allow the user to change the background color?

there is also something wrong with my browser, it is ignoring the <applet> tags...but it displays some of the stuff... and everything is set up correctly... so it might be cause of that that it isent displaying right.

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import java.util.StringTokenizer;

public class FlashText extends JApplet implements ActionListener{

private javax.swing.Timer timer;

private String labelString, blinkFrequency;

private int delay;

private JList colorList;

private final String colorNames[]

= {"Black","Blue", "Red", "Green", "Yellow"};

private final Color colors[]

= {Color.BLACK, Color.BLUE, Color.RED, Color.GREEN,Color.YELLOW};

private Container con;

private FlashPanel fp;

public void init(){

con = getContentPane();

con.setLayout(new FlowLayout());

fp = new FlashPanel();

con.add(fp);

colorList = new JList(colorNames);

colorList.setVisibleRowCount(5);

colorList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

con.add(new JScrollPane(colorList));

colorList.addListSelectionListener(new ListSelectionListener(){

public void valueChanged(ListSelectionEvent event){

fp.setBackground(colors[colorList.getSelectedIndex()]);

}

});

blinkFrequency = getParameter("speed");

delay = (blinkFrequency == null) ? 400 :

(1000 / Integer.parseInt(blinkFrequency));

labelString = getParameter("lbl");

if (labelString == null){

labelString = "Flashing text";

}

Font font = new Font("Serif", Font.PLAIN, 24);

setFont(font);

}

public void start(){

timer = new javax.swing.Timer(delay, this);

timer.start();

}

public void actionPerformed(ActionEvent e){

fp.repaint();

}

public class FlashPanel extends JPanel{

public FlashPanel(){

setPreferredSize(new Dimension(500, 500));

}

public void paintComponent(Graphics g){

super.paintComponent(g);

int fontSize = g.getFont().getSize();

int x = 0, y = fontSize, space;

int red =(int) (50 * Math.random());

int green = (int) (50 * Math.random());

int blue = (int) (256 * Math.random());

Dimension d = getSize();

g.setColor(Color.black);

FontMetrics fm = g.getFontMetrics();

space = fm.stringWidth(" ");

for (StringTokenizer t

= new StringTokenizer(labelString); t.hasMoreTokens();){

String word = t.nextToken();

int w = fm.stringWidth(word) + space;

if (x + w > d.width) {

x = 0;

y += fontSize; //move word to next line if it doesn't fit

}

if (Math.random() < 0.5)

g.setColor(new java.awt.Color((red + y*30) % 256,

(green + x/3) % 256, blue));

else

g.setColor(getBackground());

g.drawString("flashing text!", x, y);

x += w; //shift to the right to draw the next word

}

}

}

public void stop(){

timer.stop(); //stops the timer

}

}

You see..in my question, it says..you will have to use method setDelay and getDelay of class Timer..so how else do i do this without using Timer as a method?

Lizanea at 2007-7-12 19:38:03 > top of Java-index,Java Essentials,New To Java...
# 4
> user to control the blinking speedUse JSlider or JSpinner. I don't understand why you insist on using Timer for that.Background color changes via your JList, already.Useappletviewer FlashText.java
hiwaa at 2007-7-12 19:38:03 > top of Java-index,Java Essentials,New To Java...