java - Randomly choose vertex points for polygons, as well as randomize the color of the overall polygon
问题描述
How should I randomize the x/y values for specific points, as well as the color of the polygons they create, in Java? I have code I'm trying to use to create this effect, 500 times in a row, with 500 different triangles, randomizing every triangle in the same way (this is the code I'm requesting help with):
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Canvas;
import javax.swing.JFrame;
import java.awt.Polygon;
import java.util.Random;
public final class CRassignment34 extends Canvas
{
private static final long serialVersionUID=99L;
public static final void main (String[] args)
{
final JFrame win=new JFrame("500 randomly-selected triangles (a trainwreck indeed)");
win.setSize(1024,768);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.add(new CRlab34());
win.setVisible(true);
}
public final void paint (Graphics g)
{
for (int i=0;i<500;i++)
{
final Polygon tri=new Polygon();
final Random r=new Random();
tri.addPoint(x,y);
tri.addPoint(x,y);
tri.addPoint(x,y);
g.setColor(colorRand());
g.fillPolygon(tri);
}
}
public final void colorRand(Color c)
{
}
}
解决方案
推荐阅读
- javascript - 重新格式化数组对象 JavaScript
- javascript - 对象解构或可选链接哪个更好?
- google-cloud-platform - 节点升级弹出显示在工作节点中
- ios - 在 Xcode 中构建附加目标:在 AppDelegate.h 中找不到错误“React/RCTBridgeDelegate.h”文件
- c# - 如何使用 Mathf.PingPong 在最大和最小之间缩放和旋转对象?
- r - 如何包含偶数期以外的日期组?
- javascript - 使用 json 解析后获取返回 undefined
- c++ - ld.exe -lgraph 未找到
- mongodb - 为什么在 MonogoDB 中索引可以自动转换为多键?
- python - AttributeError:模块“tensorflow.python.framework.op_def_registry”没有属性“get_registered_ops”