/* r - red component
* g - green
* b - blue
* w - width of image
* h - height of image
* rad - radius of image
*/
public BufferedImage circleThing(int r, int g, int b, int w, int h, int rad)
{
if(2*rad>Math.min(w,h)) return null;
BufferedImage bimg=new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
Graphics2D G=(Graphics2D)bimg.getGraphics();
G.setColor(new Color(r,g,b,255));
G.fillRect(0,0,w,(h>>1)-rad);
G.fillRect(0,(h>>1)-rad,(w>>1)-rad,2*rad);
G.fillRect((w>>1)+rad,(h>>1)-rad,(w>>1)-rad,2*rad);
G.fillRect(0,(h>>1)+rad,w,(h>>1)-rad);
double dg=2;
for(int i=-rad; i<=rad; i++)
for(int j=-rad; j<=rad; j++)
{
double d=Math.sqrt(i*i+j*j);
if(d>rad)
{
G.setColor(new Color(r,g,b,255));
G.fillRect((w>>1)+i,(h>>1)+j,1,1);
}else if(Math.abs(d-rad)<dg)
{
double gap=Math.abs(d-rad);
int op=(int)((1.0-gap/dg)*255.0);
G.setColor(new Color(r,g,b,op));
G.fillRect((w>>1)+i,(h>>1)+j,1,1);
}
}
G.dispose();
return bimg;
}
example.jpg
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
public class Example extends JFrame
{
public Example()
{
super("Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new Display(500,400));
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[]a)
{
new Example();
}
class Display extends JComponent implements Runnable
{
private int w,h;
private Image img=null;
private int t=0;
private double p=0;
public Display(int w, int h)
{
this.w=w;
this.h=h;
setPreferredSize(new Dimension(w,h));
img=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
Graphics g=img.getGraphics();
g.setColor(new Color(0xEFEFEF));
g.fillRect(0,0,w,h);
g.setColor(new Color(0x888888));
int m=10;
for(int j=0; j<h/m; j++)
{
for(int i=0; i<w/m; i++)
{
if((i+j)%2==0)
{
g.fillRect(i*m,j*m,m,m);
}
}
}
g.dispose();
new Thread(this).start();
}
public void run()
{
for(;;)
{
t+=2;
t%=w;
p+=.05;
p%=2*Math.PI;
repaint();
try{Thread.sleep(20);}catch(Exception e){}
}
}
public void paint(Graphics gg)
{
Graphics2D g=(Graphics2D)gg;
g.drawImage(img,t,0,w,h,null);
g.drawImage(img,t-w,0,w,h,null);
g.drawImage(circleThing(255,255,255,w,h,(int)(100+50*Math.cos(p))),0,0,null);
}
public BufferedImage circleThing(int r, int g, int b, int w, int h, int rad)
{
if(2*rad>Math.min(w,h)) return null;
BufferedImage bimg=new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
Graphics2D G=(Graphics2D)bimg.getGraphics();
G.setColor(new Color(r,g,b,255));
G.fillRect(0,0,w,(h>>1)-rad);
G.fillRect(0,(h>>1)-rad,(w>>1)-rad,2*rad);
G.fillRect((w>>1)+rad,(h>>1)-rad,(w>>1)-rad,2*rad);
G.fillRect(0,(h>>1)+rad,w,(h>>1)-rad);
double dg=1;
for(int i=-rad; i<=rad; i++)
for(int j=-rad; j<=rad; j++)
{
double d=Math.sqrt(i*i+j*j);
if(d>rad)
{
G.setColor(new Color(r,g,b,255));
G.fillRect((w>>1)+i,(h>>1)+j,1,1);
}else if(Math.abs(d-rad)<dg)
{
double gap=Math.abs(d-rad);
int op=(int)((1.0-gap/dg)*255.0);
G.setColor(new Color(r,g,b,op));
G.fillRect((w>>1)+i,(h>>1)+j,1,1);
}
}
G.dispose();
return bimg;
}
}
}
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
mapAB Challlenge | 35 | 126 | |
firstswap challenge | 20 | 67 | |
servlet URL Rewriting | 1 | 27 | |
factorial example | 4 | 35 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
17 Experts available now in Live!