Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Make A Text Blinking Effect

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Make A Text Blinking Effect

    Hello! In carrying out my little game I made a panel "Winner" in which I have two JButton plus I would like to add a blinking text to indicate the victory of the player("You win!").
    Currently this text is static, designed by the function drawImage.
    How can I make it "flashing" maybe alternating the vision of the text (which I present in two different colors)?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Make A Text Blinking Effect

    Use a Timer that changes the color and calls repaint() every time the timer's method is called.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Make A Text Blinking Effect

    This is the panel of the winner after the game :
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedWriter;
    import java.io.FileNotFoundException;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
     
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.Timer;
     
    import core.GameManager;
    import core.Score;
    import world.World;
     
    public class Winner extends JPanel{ 
     
    	public static ImageProvider imageP;
    	public MainFrame mainFrame;
    	public Image gameOver;
    	public World world;
    	public Insets inserts;
    	public GamePanel gamePanel;
    	public GameManager gameManager;
    	public final List<Score> scores = new ArrayList<Score>();
    	public ScorePanel sPanel;
    	public boolean change=false;
    	private Timer timer;
     
    	public Winner(final ImageProvider imageP, MainFrame mainframe, GamePanel gamePanel,   World world, GameManager gameManager,ScorePanel sPanel) throws FileNotFoundException {
    	this.setLayout(null);
    	this.mainFrame = mainframe;
    	this.gameManager = gameManager;
    	this.imageP = imageP;
    	this.world = world;
    	this.gamePanel = gamePanel;
    	this.sPanel = sPanel;
    	inserts = this.getInsets();
     
    	new Timer(1000, new ActionListener() {	
    	@Override
    	public void actionPerformed(ActionEvent e) {
                change=true;
    	    if(change){
    	    imageP.getWin();
    	    }
    	    change=false;
    	    imageP.getWinY();				
    	    timer.start();		
    	}
    	});
    }
     
            @Override
    	protected void paintComponent(Graphics g) {
    	super.paintComponent(g);
    	g.drawImage(imageP.getBackground(), 0, 0, getWidth(), getHeight(), this);
    	}

    Basically, the timer should alternate every second before imageP.getWin() and then imageP.getWinY().
    Unfortunately, it is the first time I use the timer and don't know how to insert the repaint();
    Could you help me to modify my code?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Make A Text Blinking Effect

    how to insert the repaint();
    Just add it like you have coded it where you want it called.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Make A Text Blinking Effect

    Some thoughts after looking at your actionPerformed() method:

    1. 'change' will always be true at the beginning of the body of the method. consider using the ternary operator to flip change's value.
    2. starting the timer inside the actionPerformed() method won't work.
    2.A the Timer construction you've used is ANONYMOUS, so does not provide an instance 'timer' to start
    3. this is where the repaint() method should be called

    Simplify. Break out some separate classes and use those as your ActionListener and JPanel. (Perhaps class Winner does that for the JPanel part, but it's hard to tell with what you've posted.)

  6. #6
    Junior Member
    Join Date
    Feb 2014
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Make A Text Blinking Effect

    Thanks for the tips. Unfortunately it is not easy to understand the problem. I can say that this class is activated once the player reaches a maximum score in the GamePanel. Then you will have the panel Winner who will have a background (as you can see from the paintComponent ()) and more this text, "You win."
    "You Win!" Is an Image of two different colors that I would like to draw using drawImage.
    You should draw a picture first and then the other at regular intervals (simulating a "flashing")

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Make A Text Blinking Effect

    You can make it as complicated as you want, for real or in your imagination, and achieving the blinking effect boils down the same, relatively simple bit of code: a timer that triggers an actionPerformed() method that calls a repaint() on the desired component. The variation that defines the effect can be done in the actionPerformed() method, in the component's paintComponent() method, or in a combination of both.

Similar Threads

  1. Replies: 0
    Last Post: January 4th, 2014, 02:25 PM
  2. Java LWJGL Blinking states
    By KevinJP in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 7th, 2012, 10:05 AM
  3. How to make Jlabel text blink using timer?'
    By suyog53 in forum AWT / Java Swing
    Replies: 2
    Last Post: November 9th, 2012, 03:28 AM
  4. [SOLVED] Is there a way to make things happen if a certain text is entered in a TextField?
    By DusteroftheCentury in forum Java Theory & Questions
    Replies: 25
    Last Post: January 26th, 2012, 08:14 PM