版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hpdlzu80100/article/details/86597951
程序功能:
绘制8个带有填充色的三角形(顶点(大小)随机、颜色随机)。
运行结果:
源码:
1. 实体类
//Creating JFrame to display DrawPanel.
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.9 (Random Triangles) Write an application that displays randomly generated
* triangles in different colors. Each triangle should be filled with a
* different color. Use class GeneralPath and method fill of class Graphics2D to
* draw the triangles.
*
* @author [email protected]
* @Date Jan 22, 2019, 15:44:03 PM
*
*/
public class RandomTrianglesJPanel 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=0;
int rGreen=0;
int rBlue=0;
//画8个三角形
for (int i = 8; 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); //设置颜色
boolean triangleFound = false;
int[][] triangleCordinates = new int[2][3];
do {
int aX = 1+rn.nextInt(width);
int aY = 1+rn.nextInt(height);
int bX = 1+rn.nextInt(width);
int bY = 1+rn.nextInt(height);
int cX = 1+rn.nextInt(width);
int cY = 1+rn.nextInt(height);
double abL = Math.sqrt(Math.pow(Math.abs(aX-bX), 2)+Math.pow(Math.abs(aY-bY), 2));
double bcL = Math.sqrt(Math.pow(Math.abs(bX-cX), 2)+Math.pow(Math.abs(bY-cY), 2));
double acL = Math.sqrt(Math.pow(Math.abs(aX-cX), 2)+Math.pow(Math.abs(aY-cY), 2));
triangleCordinates[0][0] = aX; //A点X坐标
triangleCordinates[0][1] = bX; //B点X坐标
triangleCordinates[0][2] = cX; //C点X坐标
triangleCordinates[1][0] = aY; //A点Y坐标
triangleCordinates[1][1] = bY; //B点Y坐标
triangleCordinates[1][2] = cY; //C点Y坐标
if (abL+bcL>acL && abL+acL>bcL && bcL+acL>abL) //三角形判定法则:任意两边之和大于第三边
triangleFound = true;
} while (!triangleFound); //如果随机生成的坐标无法构成三角形,则继续随机构造
g2d.fill(new GeneralPath(new Polygon(triangleCordinates[0],triangleCordinates[1],3))); //绘制三角形
}
}
}
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 DrawRandomTriangles {
static JTextArea statusBar = new JTextArea();
public static void main(String[] args)
{
// create a panel that contains our drawing
RandomTrianglesJPanel panel = new RandomTrianglesJPanel();
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()));;
}
}
}