954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Making text blink in a JPanel?

Hello everyone, I am a student learning Java and need some help getting text to flash or blink. The text I want to do this with is "Which Way" in my drawstring method. Any help would be much appreciated.

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.GeneralPath;
import java.util.Random;
import java.awt.Font;
import javax.swing.*;

public class Shapes extends JPanel {

    public void init() {
        setBackground(Color.BLACK);
    }

    //TM Sets X,Y Grid as Integer
    public void drawShapes(int w, int h, Graphics2D z) {
        GeneralPath draw = new GeneralPath(GeneralPath.WIND_EVEN_ODD);


        //TM Sets Random generator
        Random rx = new Random();

        //Starts drawing an Arrow
        draw.moveTo(w * .5f, h * .25f);
        draw.lineTo(w * .4f, h * .50f);
        draw.lineTo(w * .45f, h * .50f);
        draw.lineTo(w * .45f, h * .75f);

        draw.lineTo(w * .55f, h * .75f);
        draw.lineTo(w * .55f, h * .50f);
        draw.lineTo(w * .6f, h * .50f);
        draw.lineTo(w * .5f, h * .25f);
        //TM Ends drawing an Arrow

        //TM Fills interior of arrow with color
        z.fill(draw);

        //TM Color Chosen by random generator
        z.setColor(new Color(rx.nextInt(256),
                rx.nextInt(256), rx.nextInt(256)));


        //TM Loop for chosing XY location, font and color for text

        int y = 1;
        while (y < 11) {

            //TM Sets Fonts
            String fonts[] = {"Serif", "Monospaced", "Serif", "Bold", "Serif", "Italic",
                "SansSerif", "Dialog", "DialogInput"
            };


            //TM Colors
            z.setColor(new Color(rx.nextInt(256),
                    rx.nextInt(256), rx.nextInt(256)));
            //TM Font chosen + size
            z.setFont(new Font(fonts[rx.nextInt(5)], rx.nextInt(10), 26));
           
            //TM Outputs msg within specified range
            z.drawString("Which Way? ", rx.nextInt(400), rx.nextInt(500));

            y++;

        }
    }

   
    public void paint(Graphics g) {
        Graphics2D z = (Graphics2D) g;
        Dimension d = getSize();
        z.setBackground(getBackground());
        z.clearRect(0, 0, d.width, d.height);
        z.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        drawShapes(d.width, d.height, z);
    }

    public static void main(String arg[]) {
        final Shapes x = new Shapes();
        x.init();
        JFrame f = new JFrame("Tom's 2d Shape Assignment");  //TM Adds title to JPanel
        f.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        //TM Set Size of JPanel & Centers
        f.getContentPane().add("Center", x);
        f.pack();
        f.setSize(new Dimension(600, 600));
        f.show();
    }
}
blackcloud72
Newbie Poster
3 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

haven't got the time to read it all and think it over, but I remember I did something like that once, by adding a small thread to my application, which would make the text to be "repainted" every couple of seconds.

maybe you can do something similar?

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

Easy to do with a Swing Timer that toggles a "paint string" boolean on and off and calls repaint(). Just have your paint method check that variable before drawing the string.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You