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
iamjon.smith
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.
 

1. What is Java Graphics & 2D?

Java Graphics & 2D is a set of classes and tools in the Java programming language that allow developers to create and manipulate 2D graphics and animations. It is commonly used for creating user interfaces, games, and other visual applications.

2. How can I use Java Graphics & 2D to animate an object?

To animate an object using Java Graphics & 2D, you can use the Graphics2D class and its methods such as translate(), rotate(), and scale() to manipulate the position, rotation, and size of the object. You can also use a combination of Graphics2D and Timer class to create a smooth animation loop.

3. Can I create custom graphics and animations with Java Graphics & 2D?

Yes, you can create custom graphics and animations in Java Graphics & 2D by using the Graphics2D class to draw basic shapes, images, and text, and the Paint interface to define custom colors and gradients. You can also use the AffineTransform class to transform and manipulate the appearance of your graphics.

4. Are there any limitations to using Java Graphics & 2D for animations?

While Java Graphics & 2D provides a powerful set of tools for creating animations, it may not be suitable for high-end, complex animations that require advanced graphics rendering techniques. Additionally, the performance of animations may be affected by the hardware and capabilities of the system running the application.

5. Where can I find resources to learn more about Java Graphics & 2D?

You can find resources to learn more about Java Graphics & 2D by referring to the official Java documentation, online tutorials and courses, and community forums. You can also explore sample code and projects to see how others have used Java Graphics & 2D to create animations.

Similar threads

  • Programming and Computer Science
Replies
4
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
4K
  • Programming and Computer Science
Replies
4
Views
944
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
12K
Back
Top