0% found this document useful (0 votes)
23 views

Java Micro Project Sangram

This Java code creates a loading animation application with a rotating arc. It uses a JFrame to display the animation, a LoadingPanel class that extends JPanel to handle the animation, and a Timer to increment the arc angle over time, repainting to create motion. The animation depicts a gray circle with a rotating blue arc. The code provides an example of how to programmatically create and control a basic loading animation in Java Swing.

Uploaded by

Kailas Jagtap
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Java Micro Project Sangram

This Java code creates a loading animation application with a rotating arc. It uses a JFrame to display the animation, a LoadingPanel class that extends JPanel to handle the animation, and a Timer to increment the arc angle over time, repainting to create motion. The animation depicts a gray circle with a rotating blue arc. The code provides an example of how to programmatically create and control a basic loading animation in Java Swing.

Uploaded by

Kailas Jagtap
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Contents

Introduction ..................................................................................................................................................... 2
Program code: .................................................................................................. Error! Bookmark not defined.
Program output ............................................................................................................................................... 6
Algorithm: User Registration Form .................................................................................................................. 7
Flowchart for registration form ..................................................................................................................... 10
Conclusion ..................................................................................................................................................... 11
Reference ....................................................................................................................................................... 12
Introduction

This code demonstrates a Java Swing application that displays a loading animation. It creates a
JFrame with a panel that shows a rotating loading arc. The animation is achieved using a Timer
to increment the arc angle, creating a visual loading effect. When you run this code, it opens a
window titled "Loading Animation" with a loading animation in the center.
Program code

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class LoadingAnimationExample {

public static void main(String[] args) {

JFrame frame = new JFrame("Loading Animation");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(300, 200);

LoadingPanel loadingPanel = new LoadingPanel();

frame.add(loadingPanel);

frame.setVisible(true);

// Start the loading animation


loadingPanel.startAnimation();

class LoadingPanel extends JPanel {

private int arcAngle = 0;

private Timer timer;

public LoadingPanel() {

timer = new Timer(50, new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

arcAngle += 10;

if (arcAngle >= 360) {

arcAngle = 0;

repaint();

});

}
public void startAnimation() {

timer.start();

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

int centerX = getWidth() / 2;

int centerY = getHeight() / 2;

int radius = 50;

g.setColor(Color.GRAY);

g.fillOval(centerX - radius, centerY - radius, 2 * radius, 2 * radius);

g.setColor(Color.BLUE);

g.fillArc(centerX - radius, centerY - radius, 2 * radius, 2 * radius, 0, arcAngle);

}
Program output
Algorithm: loading animation
1. Import necessary Java
libraries, including Swing
for the graphical user
interface.

2. Define a
LoadingAnimationExampl
e class with a main
method:
a. Create a JFrame and
set its title to "Loading
Animation."
b. Set the default close
operation to exit the
application when the
frame is closed.
c. Set the size of the
frame to 300 pixels in
width and 200 pixels in
height.
d. Create an instance of
the LoadingPanel class,
which is a custom JPanel
designed for the loading
animation.
e. Add the LoadingPanel
to the JFrame.
f. Make the frame visible.
g. Start the loading
animation by calling the
startAnimation method
of the LoadingPanel
instance.

3. Define the
LoadingPanel class that
extends JPanel:
a. Create an integer
arcAngle to track the
angle of the loading arc.
b. Create a Timer
instance to periodically
update the loading
animation.
c. In the constructor:
i. Initialize the Timer with
a delay of 50
milliseconds and an
ActionListener that
updates the arcAngle
and repaints the panel.
d. Create a
startAnimation method
to start the Timer.
e. Override the
paintComponent
method:
i. Call the
super.paintComponent(g
) method to clear the
panel.
ii. Calculate the center,
radius, and dimensions
for the loading shapes.
iii. Draw a gray circle
(background) using
g.fillOval.
iv. Draw a blue loading
arc using g.fillArc.

4. The Timer in the


LoadingPanel triggers the
loading animation by
updating the arcAngle
and periodically
repainting the panel. This
creates the illusion of a
loading animation with a
rotating arc.

5. When you run the


code, a window with the
title "Loading Animation"
appears, displaying the
loading animation in the
center of the frame. The
loading animation
consists of a gray circle
with a rotating blue arc,
creating a loading effect.
Flowchart for loading animation

Start
|
|--> Create a JFrame (frame) for the loading animation
| |
| |--> Set the frame's title and size
| |
| |--> Create a LoadingPanel (loadingPanel)
| | |
| | |--> Initialize arcAngle to 0
| | |
| | |--> Create a Timer (timer) with a 50ms delay
| | | |
| | | |--> Define an ActionListener for the timer
| | | | |
| | | | |--> In ActionListener, increment arcAngle by 10 degrees
| | | | |--> If arcAngle >= 360, reset it to 0
| | | | |--> Trigger repaint to update the panel
| |
| |--> Set frame's close operation, add loadingPanel, and make the frame visible
| |
| |--> Start the loading animation by calling loadingPanel.startAnimation()
|
|--> LoadingPanel paintComponent
| |
| |--> Calculate centerX, centerY, and radius for drawing
| |--> Draw a gray circle at the center
| |--> Draw a blue arc based on the current arcAngle
|
|--> Timer continues to trigger ActionListener and update the animation
|
End

Conclusion
In conclusion, the provided Java code demonstrates a simple loading animation using Swing
components. Here's a summary of what the code does:
In essence, this code provides a basic example of how to create and control a loading animation
using Java Swing components, making it a useful foundation for more complex user interface
applications that require visual feedback to indicate ongoing processes.

Reference

Web-browser: google
Social-media: youtube
Thank you

You might also like