程序功能:
使用Graphics2D类draw方法绘制立方体
运行示例:
源码:
1. 实体类
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.geom.GeneralPath;
import java.security.SecureRandom;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
/**
* 13.16 (Drawing Cubes) Write an application that draws a cube. Use class
* GeneralPath and method draw of class Graphics2D.
*
* @author Pandenghuang@163.com
* @Date Jan 22, 2019, 1:44:03 PM
*
*/
public class CubeJPanel extends JPanel
{
final SecureRandom rn = new SecureRandom();
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
super.paintComponents(g);
int width = getWidth(); // total width
int height = getHeight(); // total height
int rRed;
int rGreen;
int rBlue;
int length = width/3; //边长随窗体尺寸增大而增大
int aX = width/2 - length/2;
int aY = height/2 - length/2;
//定义一个正方体
int cubeCordinates[][] = new int[2][7]; //保存正方体的7个点(另一点作图时用不到)
cubeCordinates[0][0] = aX; //A点X坐标
cubeCordinates[0][1] = aX + length; //B点X坐标
cubeCordinates[0][2] = aX + length; //C点X坐标
cubeCordinates[0][3] = aX; //D点X坐标
cubeCordinates[0][4] = (int) (aX + 1.5*length); //E点X坐标
cubeCordinates[0][5] = (int) (aX + 0.5*length); //F点X坐标
cubeCordinates[0][6] = (int) (aX + 1.5*length); //G点X坐标
cubeCordinates[1][0] = aY; //A点Y坐标
cubeCordinates[1][1] = aY; //B点Y坐标
cubeCordinates[1][2] = aY + length; //C点Y坐标
cubeCordinates[1][3] = aY + length; //D点Y坐标
cubeCordinates[1][4] = (int) (aY - 0.3 * length); //E点X坐标
cubeCordinates[1][5] = (int) (aY - 0.3 * length); //F点X坐标
cubeCordinates[1][6] = (int) (aY + 0.7 * length); //G点X坐标
int cubeSquareCordinates[][][] = new int[3][2][4]; //保存正方体的7个点构成的三个面,用于绘制该正方体
//正面由ABCD四个点构成
for (int i=0; i<2; i++)
for (int j=0; j<4; j++) {
cubeSquareCordinates[0][i][i] = cubeCordinates[i][j]; //A点X坐标
}
cubeSquareCordinates[0][0][0] = cubeCordinates[0][0]; //A点X坐标
cubeSquareCordinates[0][0][1] = cubeCordinates[0][1]; //B点X坐标
cubeSquareCordinates[0][0][2] = cubeCordinates[0][2]; //C点X坐标
cubeSquareCordinates[0][0][3] = cubeCordinates[0][3]; //D点X坐标
cubeSquareCordinates[0][1][0] = cubeCordinates[1][0]; //A点Y坐标
cubeSquareCordinates[0][1][1] = cubeCordinates[1][1]; //B点Y坐标
cubeSquareCordinates[0][1][2] = cubeCordinates[1][2]; //C点Y坐标
cubeSquareCordinates[0][1][3] = cubeCordinates[1][3]; //D点Y坐标
//右侧面由BCGE四个点构成
cubeSquareCordinates[1][0][0] = cubeCordinates[0][1]; //B点X坐标
cubeSquareCordinates[1][0][1] = cubeCordinates[0][2]; //C点X坐标
cubeSquareCordinates[1][0][2] = cubeCordinates[0][6]; //G点X坐标
cubeSquareCordinates[1][0][3] = cubeCordinates[0][4]; //E点X坐标
cubeSquareCordinates[1][1][0] = cubeCordinates[1][1]; //B点Y坐标
cubeSquareCordinates[1][1][1] = cubeCordinates[1][2]; //C点Y坐标
cubeSquareCordinates[1][1][2] = cubeCordinates[1][6]; //G点Y坐标
cubeSquareCordinates[1][1][3] = cubeCordinates[1][4]; //E点Y坐标
//上面由ABEF四个点构成
cubeSquareCordinates[2][0][0] = cubeCordinates[0][0]; //A点X坐标
cubeSquareCordinates[2][0][1] = cubeCordinates[0][1]; //B点X坐标
cubeSquareCordinates[2][0][2] = cubeCordinates[0][4]; //E点X坐标
cubeSquareCordinates[2][0][3] = cubeCordinates[0][5]; //F点X坐标
cubeSquareCordinates[2][1][0] = cubeCordinates[1][0]; //A点Y坐标
cubeSquareCordinates[2][1][1] = cubeCordinates[1][1]; //B点Y坐标
cubeSquareCordinates[2][1][2] = cubeCordinates[1][4]; //E点Y坐标
cubeSquareCordinates[2][1][3] = cubeCordinates[1][5]; //F点Y坐标
//绘制该正方体
for (int i = 2; i >= 0;i--){
//Color represented in RGB mode
rRed = rn.nextInt(256);
rGreen = rn.nextInt(256);
rBlue = rn.nextInt(256);
Color color=new Color(rRed, rGreen, rBlue);
g2d.setColor(color);
g2d.fill(new GeneralPath(new Polygon(cubeSquareCordinates[i][0],cubeSquareCordinates[i][1],4)));
}
}
}
2. 测试类
import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class DrawCube {
static JTextArea statusBar = new JTextArea();
public static void main(String[] args)
{
// create a panel that contains our drawing
CubeJPanel panel = new CubeJPanel();
MouseHandler handler = new MouseHandler();
panel.addMouseMotionListener(handler);
// create a new frame to hold the panel
JFrame application = new JFrame();
application.setTitle("绘制正方体");
// set the frame to exit when it is closed
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel,BorderLayout.CENTER); // add the panel to the frame
application.add(statusBar,BorderLayout.SOUTH); // add the statusBar to the frame
application.setSize(460, 360); // set the size of the frame
application.setVisible(true); // make the frame visible
}
static class MouseHandler extends MouseMotionAdapter
{
// handle event when mouse enters area
@Override
public void mouseMoved(MouseEvent event)
{
statusBar.setText(String.format("光标当前坐标:[%d, %d]",
event.getX(), event.getY()));;
}
}
}