Comp Sci Java Graphics & 2D Help me animate this

  • Thread starter Thread starter iamjon.smith
  • Start date Start date
  • Tags Tags
    2d Graphics Java
Click For Summary
To animate a neon sign logo in Java using Graphics and Graphics2D, the user needs to implement a delay mechanism to create a flashing effect. The suggestion is to utilize the Thread.sleep() method to alternate between drawing the "on" and "off" states of the logo. The user has already created the logo with various graphical elements but is struggling with the animation aspect. A loop can be established to repeatedly paint the logo in both states with a specified delay in between. This approach will effectively simulate the neon sign's flashing behavior.
iamjon.smith
Messages
117
Reaction score
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


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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 0 ·
Replies
0
Views
634
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
13K