📄 pyramid.java
字号:
// Lab 3: Pyramid.java
// Program draws a pyramid.
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
public class Pyramid extends JFrame {
/* Declare two arrays that contain x and y coordinates */
float X[] = {85,60,40,110,130};
float Y[] = {40, 80,100,100,80};
// constructor
public Pyramid()
{
super( "Pyramid" );
setSize( 275, 150 );
setVisible( true );
}
// draw pyramid
public void paint( Graphics g )
{
super.paint( g );
/* Cast the Graphics reference received by paint to a Graphics2D */
Graphics2D g2d = (Graphics2D)g;
/* Define a GeneralPath object that represents a pyramid */
GeneralPath p = new GeneralPath();
/* Set the color of pyramid using Graphics2D method setColor */
g2d.setColor(Color.pink);
/* Use method moveTo to specify the first point in the pyramid */
p.moveTo(X[0], Y[0]);
/* Create a for loop that will access all the points in both arrays
to create the pyramid in the GeneralPath object */
for(int i = 1; i<=4; i++){
p.moveTo(X[i], Y[i]);
p.lineTo(X[i%4+1],Y[i%4+1]);
p.lineTo(X[0],Y[0]);
}
/* Use method closePath to complete the pyramid */
//p.moveTo(X[1], Y[1]);
p.closePath();
/* Use Graphics2D method draw to display the pyramid. */
g2d.draw(p);
}
// execute application
public static void main( String args[] )
{
Pyramid application = new Pyramid();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
} // end class Pyramid
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -