使用循环语句绘制图案-1

画图向来不是我的强项,如果有了Java的帮忙,或许情况会有所改观。

60条线,对电脑来说就是一瞬间的事,但如果用画图板人工来画的话,呵呵,你试试看?

 

代码如下:

//Graphics Exercise 4.1
/*Using loops and control statements to draw lines can lead 
 * to many interesting designs Graphics101.java
 */
//Creating JFrame to display DrawPanel.
import javax.swing.JFrame;
import java.awt.Graphics; 
import javax.swing.JPanel;

public class Graphics101 extends JPanel
{
	// draws an X from the corners of the panel
	public void paintComponent(Graphics g)
	{
	  // call paintComponent to ensure the panel displays correctly
	  super.paintComponent(g);
	  
	  int width = getWidth(); // total width   
	  int height = getHeight(); // total height
	  int step=1;

	  // draw a line from the upper-left to the lower-right
	  while (step<=15)
	  {
		  g.drawLine(0, 0, width/15*step, height/15*(15-step));
		  g.drawLine(width, 0, width/15*step, height/15*step);
		  g.drawLine(width, height, width/15*step, height/15*(15-step));
		  g.drawLine(0, height, width/15*step, height/15*step);
		  step++;
	  }
	} 

	
	public static void main(String[] args)
{
 // create a panel that contains our drawing
Graphics101 panel = new Graphics101();
 
 // create a new frame to hold the panel
 JFrame application = new JFrame();
 
 // set the frame to exit when it is closed
 application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 application.add(panel); // add the panel to the frame      
 application.setSize(250, 250); // set the size of the frame
 application.setVisible(true); // make the frame visible    
} 
} // end class DrawPanelTest

 

 

运行截屏:

 

 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值