Java Micro Project Sangram
Java Micro Project Sangram
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;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.add(loadingPanel);
frame.setVisible(true);
public LoadingPanel() {
@Override
arcAngle += 10;
arcAngle = 0;
repaint();
});
}
public void startAnimation() {
timer.start();
@Override
super.paintComponent(g);
g.setColor(Color.GRAY);
g.setColor(Color.BLUE);
}
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.
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