Friday, August 3, 2012
Today I am going to post a program that will create bubbles or a bubbling effect. Here I have used Ellipse2D class. A method calculates random coordinates,sizes and strokes of the ellipses. The size of the ellipses goes on increasing till it reaches its maximum value after which it starts from beginning. Each ellipse reaches its maximum size at different times. Each repainting is done after sleep of every 50ms. Related video is given below
Here is the code for you
import java.awt.*;
import java.awt.geom.Ellipse2D;
import javax.swing.*;
public class Bubbles extends JPanel{
private Ellipse2D.Float[] ellipses;
private double esize[];
private float estroke[];
private final double MAX_SIZE=40;
public Bubbles() {
ellipses = new Ellipse2D.Float[75];
esize = new double[ellipses.length];
estroke = new float[ellipses.length];
for (int i = 0; i < ellipses.length; i++) {
ellipses[i] = new Ellipse2D.Float();
getRandomXY(i, 20 * Math.random(),getWidth()-60,getHeight()-60);
}
}
public void build(){
while(true){
for (int i = 0; i < ellipses.length; i++) {
estroke[i] += 0.025f; //increasing stroke
esize[i]++; //increasing size
if (esize[i] > MAX_SIZE) //new values if reaches maximum
getRandomXY(i, 20 * Math.random(),getWidth()-60,getHeight()-60);
else
ellipses[i].setFrame(ellipses[i].getX(), ellipses[i].getY(),
esize[i], esize[i]);
}
repaint(); //repainting
try {
Thread.sleep(50); //sleeping
}catch (Exception ex) {}
}
}
public void getRandomXY(int i, double size, int w, int h) {
esize[i] = size;
estroke[i] = 1.0f; //setting size.strokes,coordinates
double x = Math.random() * (getWidth()-(MAX_SIZE/2));
double y = Math.random() * (getHeight()-(MAX_SIZE/2));
ellipses[i].setFrame(x, y, size, size); //setting ellipse
}
@Override
public void paintComponent(Graphics g){
Graphics2D g2d=(Graphics2D)g;
//clearing previous trails
g2d.setColor(Color.black);
g2d.fillRect(0,0,this.getWidth(),this.getHeight());
for (int i = 0; i < ellipses.length; i++) {
g2d.setStroke(new BasicStroke(estroke[i]));
int red=(int)(Math.random()*256);
int green=(int)(Math.random()*256);
int blue=(int)(Math.random()*256);
g2d.setColor(new Color(red,green,blue));
g2d.draw(ellipses[i]); //drawing ellipses
}
}
public static void main(String[] args) {
JFrame f=new JFrame("Bubbles");
Bubbles b=new Bubbles();
f.getContentPane().add(b);
f.setSize(350,350);
f.setVisible(true);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
b.build();
}
}You can also download source file from below links
DOWNLOAD the source from Mediafire
DOWNLOAD the source from 4shared
import java.awt.*;
import java.awt.geom.Ellipse2D;
import javax.swing.*;
public class Bubbles extends JPanel{
private Ellipse2D.Float[] ellipses;
private double esize[];
private float estroke[];
private final double MAX_SIZE=40;
public Bubbles() {
ellipses = new Ellipse2D.Float[75];
esize = new double[ellipses.length];
estroke = new float[ellipses.length];
for (int i = 0; i < ellipses.length; i++) {
ellipses[i] = new Ellipse2D.Float();
getRandomXY(i, 20 * Math.random(),getWidth()-60,getHeight()-60);
}
}
public void build(){
while(true){
for (int i = 0; i < ellipses.length; i++) {
estroke[i] += 0.025f; //increasing stroke
esize[i]++; //increasing size
if (esize[i] > MAX_SIZE) //new values if reaches maximum
getRandomXY(i, 20 * Math.random(),getWidth()-60,getHeight()-60);
else
ellipses[i].setFrame(ellipses[i].getX(), ellipses[i].getY(),
esize[i], esize[i]);
}
repaint(); //repainting
try {
Thread.sleep(50); //sleeping
}catch (Exception ex) {}
}
}
public void getRandomXY(int i, double size, int w, int h) {
esize[i] = size;
estroke[i] = 1.0f; //setting size.strokes,coordinates
double x = Math.random() * (getWidth()-(MAX_SIZE/2));
double y = Math.random() * (getHeight()-(MAX_SIZE/2));
ellipses[i].setFrame(x, y, size, size); //setting ellipse
}
@Override
public void paintComponent(Graphics g){
Graphics2D g2d=(Graphics2D)g;
//clearing previous trails
g2d.setColor(Color.black);
g2d.fillRect(0,0,this.getWidth(),this.getHeight());
for (int i = 0; i < ellipses.length; i++) {
g2d.setStroke(new BasicStroke(estroke[i]));
int red=(int)(Math.random()*256);
int green=(int)(Math.random()*256);
int blue=(int)(Math.random()*256);
g2d.setColor(new Color(red,green,blue));
g2d.draw(ellipses[i]); //drawing ellipses
}
}
public static void main(String[] args) {
JFrame f=new JFrame("Bubbles");
Bubbles b=new Bubbles();
f.getContentPane().add(b);
f.setSize(350,350);
f.setVisible(true);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
b.build();
}
}You can also download source file from below links
DOWNLOAD the source from Mediafire
DOWNLOAD the source from 4shared
Labels:Graphics2D
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
Links
Blog Archive
-
▼
2012
(
25
)
-
▼
August
(
8
)
- Differences between AWT and Swing
- How to set Java compiler's path in Windows7
- What is the difference between import and #include...
- What is a JIT compiler in Java ?
- Why cant you declare a class private ?
- Java program to create Bubbles using Graphics2D
- Rotate Text in Java using Graphics2D transform
- Java program to draw partially transparent image
-
▼
August
(
8
)
is it possible to add bubble effect on a static wallpaper in a theme using this code????
ReplyDeleteU can add bubbling effect to a static wallpaper in two ways. First way is that, u can draw an image on the panel just before drawing this bubbles. This is very easy and the original image remains same and only the bubbles are added above it. Second way is that, u can use image processing where the bubbles will be drawn on the image in offscreen mode. Thus it will change the original image in 2nd case. in 2nd case BufferedImage can be used to achieve this processing. Try both of it and I will post the 2nd way as soon as possible.
Delete