Java Graphics & 2D Help me animate this

In summary, the conversation is about creating a neon sign using Graphics and Graphics2D in Java. The person needs help animating the sign and their instructor has suggested using a delay to draw and redraw the logo to simulate a flashing effect. The person is unsure how to use the delay and needs guidance. The code for the frame and main class is provided, along with a suggestion to use a loop and the Thread.sleep() method to create the flashing effect.
  • #1
117
3
Java Graphics & 2D Help me animate this!

Instructions :

Using Graphics and Graphics2D, create a neon sign for your business logo. Create your logo to include a shape and the name of your business. The logo should flash off and on continuously. You may get as creative as you wish with your logo.

Investigate using a delay to draw and then redraw your logo so that it behaves like a neon sign.

So far I have created the graphic and just need to animate it. My Instructor has recommended using a delay to draw and redraw, but I can't figure out how to use delay. I just need someone to point me in the right direction once again.

Here is my code:

The Frame Class:

Code:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Font;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author Jonathan Smith
 */
public class ColorFrame extends JPanel{

    @Override
    public void paint( Graphics g){

        super.paintComponent( g ); // call superclass's paintComponent
        Graphics2D g2d = (Graphics2D)g;

        this.setBackground( Color.BLACK );  // set background color of frame

         
	{
        g.setFont( new Font("Comic Sans", Font.BOLD, 20)); // Set Font attributes
        g.setColor(Color.YELLOW);   // Set Font color
        g.drawString("CODE TALKERS INC.",70, 50 ); // Draw text, set position

        g.setColor(Color.YELLOW);  // Set color of rounded rectangle around text
	g.drawRoundRect(65,27,210,30,25,25);    // Draw rounded rectangle around text


        g.setColor(Color.YELLOW);   // set color of shape
        g.fillArc(20, 50, 30, 30, 0, -320); // draw shape

        g.setColor(Color.BLACK); // set color shape for eye
        g.fillArc(30, 55, 5, 5, 0, -360); // draw round eye

        g2d.setColor(Color.YELLOW); // set color for first line of speech bubble
        g2d.drawLine(55, 58, 74, 57); // draw first line of speech bubble

        g2d.setColor(Color.YELLOW); // set color for second line of speech bubble
        g2d.drawLine(55, 58, 65, 48);   // draw second line of speech bubble

        g2d.setColor(Color.BLACK); // set color of circle to black out corner of rounded rectangle
        g2d.fillArc( 65, 45, 5, 10, 0, -360); // draw circle to black out corner of rounded rectangle

        g2d.setColor(Color.BLACK); // set color of second circle to finish corner black out
        g2d.fillArc( 68, 50, 11, 7, 0, -360); // draw second circle to finish corner black out

        
	}
        
    }

  }

and my main class where the frame class is called:

Code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Font;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author Jonathan Smith
 */
public class CompanyLogo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        // create frame for ColorFrame
        JFrame frame = new JFrame("Code Walkers Inc.");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ColorFrame colorFrame = new ColorFrame();   // create ColorFrame
        frame.add( colorFrame );    // add colorFrame to frame
        frame.setSize( 400, 140 );  // set frame size
        frame.setVisible(true); // display the frame

        
    }// end main

   
}// end class CompanyLogo

I know there are some unused imports, and I will remove those once the project is finished. I am still learning the graphics side of java, so bear with me.

I would like for the "speech bubble" (comprised of 2 lines, a rounded rectangle, 2 circles to black out the corner of the rounded rectangle and the text) to flash on and off. Once I get the delay figured out I am considering redrawing the "Pac Man" to make it look like he is talking as well.
 
Physics news on Phys.org
  • #2


You could have a loop that draws the "on" version, then a delay, then the "off" version.

One way to accomplish a delay is to put the thread to sleep, using the static sleep() method of the Thread class.

In pseudo code, this would be
Code:
loop
   paint "on" image
   sleep
   pain "off" image
   sleep
end loop

The call to sleep looks like this:
Code:
Thread.sleep(2000);  // Pause thread for 2 seconds.

The sleep method is in java.lang.Thread.
 

Suggested for: Java Graphics & 2D Help me animate this

Replies
2
Views
864
Replies
7
Views
1K
Replies
12
Views
1K
Replies
17
Views
944
Replies
7
Views
1K
Replies
3
Views
800
Replies
1
Views
737
Replies
2
Views
856
Back
Top