画图向来不是我的强项,如果有了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
运行截屏: