cube.java
来自「Java程序设计(美) David D. Riley著 机械工业出版社 书籍配套」· Java 代码 · 共 34 行
JAVA
34 行
// (Figure 8.21)import java.awt.Graphics;import javax.swing.JComponent;public class Cube extends JComponent { /** post: getbackGround() is an invisible color * and getX() == x and getY() == y * and getWidth() == getHeight() == s */ public Cube(int x, int y, int s) { super( ); setBounds(x, y, s, s); } /** pre: getWidth() == getHeight() * post: a cube is drawn with vertices at (getWidth()/2, 0), * (0, getHeight()-1) and (getWidth()-1, getHeight()-1) * and the color of the triangle is getBackground() */ public void paint( Graphics g ) { int s = getWidth()-1; g.setColor( getBackground() ); g.drawRect(0, s/4, s*3/4, s*3/4); g.drawLine(0, s/4, s/4, 0); //left diagonal line g.drawLine(s*3/4, s/4, s, 0); //right top diagonal line g.drawLine(s/4, 0, s, 0); //top horizontal line g.drawLine(s*3/4, s, s, s*3/4); //right bottom diagonal line g.drawLine(s, 0, s, s*3/4); //back vertical line g.setColor( getForeground() ); g.fillOval(s/4, s/2, s/4, s/4); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?