靈感:java2核心技術卷1(共兩卷)java
最近在看java2核心技術裏面提到顯示圖片,因而就作了個心形圖片組合圖案。。。你懂得ide
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.p_w_picpathio.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> p_w_picpath; DrawPanel(){ try { p_w_picpath = new ArrayList<Image>(); String[] s = { "圖片1地址", "圖片2地址", "圖片3地址" }; System.out.println(s[0]); System.out.println(s[1]); System.out.println(s[2]); p_w_picpath.add(ImageIO.read(new File(s[0]))); p_w_picpath.add(ImageIO.read(new File(s[1]))); p_w_picpath.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 p_w_picpath if (p_w_picpath.isEmpty()) return ; int p_w_picpathHeight = p_w_picpath.get(0).getHeight(this); int p_w_picpathWidth = p_w_picpath.get(0).getWidth(this); System.out.println( p_w_picpathHeight+" "+p_w_picpathWidth); 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(p_w_picpath.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(p_w_picpath.get(nth++),(int)x,(int)y,null); prePoint.setLocation(point); }//end love filled with imag } }
因涉及我的隱私故把圖片匿了,運行不了請及時評論。this
謝謝!圖片