灵感:java2核心技术卷1(共两卷)
最近在看java2核心技术里面提到显示图片,于是就做了个心形图片组合图案。。。你懂得
import java.awt.*;
import java.awt.geom.*;
import java.awt.geom.Point2D.Double;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.*;
public class DrawTest
{
public static void main(String[] args)
{
DrawFrame frame = new DrawFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/**
A frame that contains a panel with drawings
*/
class DrawFrame extends JFrame
{
public DrawFrame()
{
setTitle("DrawTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// add panel to frame
DrawPanel panel = new DrawPanel();
add(panel);
}
public static final int DEFAULT_WIDTH = 1000;
public static final int DEFAULT_HEIGHT =1000;
}
/**
A panel that displays rectangles and ellipses.
*/
class DrawPanel extends JPanel
{
private ArrayList image;
DrawPanel(){
try
{
image = new ArrayList();
String[] s =
{ "图片1地址",
"图片2地址",
"图片3地址"
};
System.out.println(s[0]);
System.out.println(s[1]);
System.out.println(s[2]);
image.add(ImageIO.read(new File(s[0])));
image.add(ImageIO.read(new File(s[1])));
image.add(ImageIO.read(new File(s[2])));
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
//love
//
// double theta=0.00 ;
// double offset = 0.0001;
// double x;
// double y;
// Point2D prePoint = new Point2D.Double(0,0);
// Point2D point = new Point2D.Double( 0, 0);
// x= 500-20*(16*Math.pow(Math.sin(theta),3));
// y= 500-20*(
// 13*Math.cos(theta)-5*Math.cos(2*theta)-2*Math.cos(3*theta)-Math.cos(4*theta) );
// prePoint.setLocation(x, y);
//
// for( ;theta< 2*Math.PI;theta+= offset ){
// x= 500-20*(16*Math.pow(Math.sin(theta),3));
// y= 500-20*(
// 13*Math.cos(theta)-5*Math.cos(2*theta)-2*Math.cos(3*theta)-Math.cos(4*theta) );
// point.setLocation(x, y);
// Line2D line = new Line2D.Double(point, prePoint);
// g2.draw( line );
// prePoint.setLocation(point);
// }//end love
// love filled with image
if (image.isEmpty()) return ;
int imageHeight = image.get(0).getHeight(this);
int imageWidth = image.get(0).getWidth(this);
System.out.println( imageHeight+" "+imageWidth);
double theta=0.00 ;
double offset = 0.06;
double x;
double y;
int nth=0;
Point2D prePoint = new Point2D.Double(0,0);
Point2D point = new Point2D.Double( 0, 0);
x= 450-25*(16*Math.pow(Math.sin(theta),3));
y= 400-25*(
13*Math.cos(theta)-5*Math.cos(2*theta)-2*Math.cos(3*theta)-Math.cos(4*theta) );
prePoint.setLocation(x, y);
g.drawImage(image.get(nth++), (int)x, (int)y, null);
for( ;theta< 2*Math.PI;theta+= offset ){
x= 450-25*(16*Math.pow(Math.sin(theta),3));
y= 400-25*(
13*Math.cos(theta)-5*Math.cos(2*theta)-2*Math.cos(3*theta)-Math.cos(4*theta) );
point.setLocation(x, y);
Line2D line = new Line2D.Double(point, prePoint);
// g2.draw( line );
if(nth == 3) nth %=3;
g2.drawImage(image.get(nth++),(int)x,(int)y,null);
prePoint.setLocation(point);
}//end love filled with imag
}
}
因涉及个人隐私故把图片匿了,运行不了请及时评论。
谢谢!