Java 如何在jPanel中重新调整多边形的大小?
我必须制作一个程序,在随机大小的随机位置生成恒星。我的代码已经在随机位置绘制了星星,但我无法随机更改它们的大小。我试着给每个点指定一个尺寸因子来改变它们之间的距离,但是星星出来的时候都是一团糟。是否有一种缩放方法可以使用 这是我到目前为止得到的,它在随机位置绘制恒星Java 如何在jPanel中重新调整多边形的大小?,java,graphics,Java,Graphics,我必须制作一个程序,在随机大小的随机位置生成恒星。我的代码已经在随机位置绘制了星星,但我无法随机更改它们的大小。我试着给每个点指定一个尺寸因子来改变它们之间的距离,但是星星出来的时候都是一团糟。是否有一种缩放方法可以使用 这是我到目前为止得到的,它在随机位置绘制恒星 final int MID = WIDTH / 2; final int TOP = 50; //sky Color skyColor = new Color(0, 0, 0); page.fillRe
final int MID = WIDTH / 2;
final int TOP = 50;
//sky
Color skyColor = new Color(0, 0, 0);
page.fillRect(0,0,getWidth(),getHeight());
//ground
Color groundColor = new Color(95,95,95);
page.setColor(groundColor);
page.fillRect(0,HEIGHT-20,getWidth(),getHeight());
//star
for (int i = 1; i <= starCount; i++)
{
int ranLocX = gen.nextInt(700 - 100) + 100;
int ranLocY = gen.nextInt(300 - 75) + 75;
int ranSize = gen.nextInt(8 - 1) + 1;
int sizeXA = (-10 * ranSize);
int sizeXB = (10 * ranSize);
int sizeXC = (-5 * ranSize);
int sizeXD = (-10 * ranSize);
int sizeXE = (-10 * ranSize);
int sizeXF = (-10 * ranSize);
int sizeYC = (10 * ranSize);
int sizeYD = (-10 * ranSize);
int sizeYE = (10 * ranSize);
page.drawPolygon(new int[] {xa + ranLocX, xb + ranLocX, xc + ranLocX, xd + ranLocX, xe + ranLocX, xf + ranLocX}, new int[] {ya + ranLocY, yb + ranLocY, yc + ranLocY, yd + ranLocY, ye + ranLocY, yf + ranLocY}, 6);
}
final int MID=WIDTH/2;
最终int TOP=50;
//天空
颜色天空颜色=新颜色(0,0,0);
fillRect(0,0,getWidth(),getHeight());
//地面
颜色底色=新颜色(95,95,95);
页面设置颜色(底色);
page.fillRect(0,HEIGHT-20,getWidth(),getHeight());
//明星
对于(inti=1;i这里有一个如何更改多边形大小的示例。我画了正方形,但任何形状都可以
- 只需创建一个仿射变换的缩放实例,并使用它来缩放形状
- 我使用
ThreadLocalRandom
随机选择要应用的比例。我总是复制原始多边形(template
),然后缩放它
创建一个5点星
int startx = 250; // arbitrary starting points
int starty = 250;
public Polygon createStar(int armBase) {
Polygon star = new Polygon();
// The armBase is equal to one side of the inner
// pentagon of the star
// The height of the arm is the distance from the middle of the
// base to the tip of the stars arm. Since the tangent computes
// ratio of the sides of a right triangle, multiplying by half
// the base gives the other side, hence the height.
int armHeight =
(int) (armBase / 2 * Math.tan(Math.toRadians(72)));
// The center offset is the distance from the middle of a given
// base to the center of the inner pentagon.
int centerOffset =
(int) (armBase / 2 * Math.tan(Math.toRadians(54)));
// this works by creating the first arm, rotating 72 degrees
// and then adding the other two coodinates of succeeding arms.
star.addPoint(startx, starty);
star.addPoint(startx + armBase / 2, starty - armHeight);
star.addPoint(startx + armBase, starty);
for (int j = 0; j < 4; j++) {
rotatePolygon(-Math.PI / 5 * 2, startx + armBase / 2,
starty + centerOffset, star);
star.addPoint(startx + armBase / 2, starty - armHeight);
star.addPoint(startx + armBase, starty);
}
star.npoints--;
star.translate(-star.getBounds().x,-star.getBounds().y);
return star;
}
// This is general purpose rotation that rotates about a center
// point. This can be derived using the double angle identities of
// for sin and cosine.
private void rotatePolygon(double ang, double sx, double sy,
Polygon poly) {
for (int j = 0; j < poly.npoints; j++) {
double x = poly.xpoints[j];
double y = poly.ypoints[j];
double xx = sx + (x - sx) * Math.cos(ang)
- (y - sy) * Math.sin(ang);
double yy = sy + (x - sx) * Math.sin(ang)
+ (y - sy) * Math.cos(ang);
poly.xpoints[j] = (int) xx;
poly.ypoints[j] = (int) yy;
}
}
int startx=250;//任意起始点
int starty=250;
公共多边形createStar(int armBase){
多边形星形=新多边形();
//臂底等于内侧的一侧
//五角大楼
//手臂的高度是从手臂中间的距离
//底部到星形臂的顶端。因为切线计算
//直角三角形的边的比率,乘以一半
//底部为另一侧,因此为高度。
内接臂高=
(内部)(armBase/2*Math.tan(Math.toRadians(72));
//“中心偏移”是指距给定中心的距离
//从底部到内部五角大楼的中心。
整数中心偏移=
(内部)(armBase/2*Math.tan(Math.toRadians(54));
//其工作原理是创建第一个手臂,旋转72度
//然后再加上后面两个手臂的坐标。
star.addPoint(startx、starty);
star.addPoint(startx+armBase/2,starty-臂高);
star.addPoint(startx+armBase,starty);
对于(int j=0;j<4;j++){
旋转多边形(-Math.PI/5*2,startx+armBase/2,
星形+中心偏移,星形);
star.addPoint(startx+armBase/2,starty-臂高);
star.addPoint(startx+armBase,starty);
}
star.npoints--;
转换(-star.getBounds().x,-star.getBounds().y);
返回星;
}
//这是围绕中心旋转的通用旋转
//点。这可以使用的双角度恒等式导出
//对于正弦和余弦。
私人空间旋转多边形(双ang,双sx,双sy,
多边形(多边形){
对于(int j=0;j
这是一个可以画一颗五角星的图形用户界面
这是完整的可运行代码。我使用极坐标计算了画星星所需的10个点。我猜了分数以得到正确的中间点
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class StarryNight2GUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new StarryNight2GUI());
}
@Override
public void run() {
JFrame frame = new JFrame("Starry Night");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DrawingPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = 1L;
public DrawingPanel() {
this.setBackground(Color.BLACK);
this.setPreferredSize(new Dimension(640, 480));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Color groundColor = new Color(95, 95, 95);
g.setColor(groundColor);
g.fillRect(0, getHeight() - 30, getWidth(), 30);
Polygon polygon = createStar(new Point(320, 240), 80);
g.setColor(Color.YELLOW);
g.fillPolygon(polygon);
}
private Polygon createStar(Point centerPoint, int radius) {
Polygon polygon = new Polygon();
// 72, 144, 216, 288, 360
// 36, 108, 180, 252, 324
// 54, 126, 198, 270, 342
// 18, 54, 90, 126, 162, 198, 234, 270, 306, 342
for (int angle = 18; angle < 360; angle += 72) {
double r = 0.42 * radius;
Point point = toCartesian(centerPoint, angle, r);
polygon.addPoint(point.x, point.y);
point = toCartesian(centerPoint, angle + 36, radius);
polygon.addPoint(point.x, point.y);
}
return polygon;
}
private Point toCartesian(Point centerPoint, int angle, double radius) {
double theta = Math.toRadians(angle);
int x = centerPoint.x + (int) Math.round(Math.cos(theta) * radius);
int y = centerPoint.y + (int) Math.round(Math.sin(theta) * radius);
return new Point(x, y);
}
}
}
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.Point;
导入java.awt.Polygon;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
公共类StarryNight2GUI实现可运行{
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的StarryNight2GUI());
}
@凌驾
公开募捐{
JFrame=新JFrame(“星夜”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(新建DrawingPanel(),BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(真);
frame.setVisible(true);
}
公共类DrawingPanel扩展了JPanel{
私有静态最终长serialVersionUID=1L;
公共绘图面板(){
这个.背景(颜色.黑色);
此.setPreferredSize(新维度(640480));
}
@凌驾
受保护组件(图形g){
超级组件(g);
颜色底色=新颜色(95,95,95);
g、 setColor(groundColor);
g、 fillRect(0,getHeight()-30,getWidth(),30);
多边形=createStar(新点(320240),80);
g、 setColor(颜色为黄色);
g、 填充多边形(多边形);
}
专用多边形createStar(点中心点,整数半径){
多边形=新多边形();
// 72, 144, 216, 288, 360
// 36, 108, 180, 252, 324
// 54, 126, 198, 270, 342
// 18, 54, 90, 126, 162, 198, 234, 270, 306, 342
用于(内部角度=18;角度<360;角度+=72){
双r=0.42*半径;
点=自流(中心点,角度,r);
多边形添加点(点x、点y);
点=自流(中心点,角度+36,半径);
多边形添加点(点x、点y);
}
返回多边形;
}
专用点到自流(点中心点、内角、双半径){
双θ=数学环面(角度);
int x=中心点x+(int)数学圆(数学余弦(θ)*半径);
int y=中心点y+(int)Ma
int startx = 250; // arbitrary starting points
int starty = 250;
public Polygon createStar(int armBase) {
Polygon star = new Polygon();
// The armBase is equal to one side of the inner
// pentagon of the star
// The height of the arm is the distance from the middle of the
// base to the tip of the stars arm. Since the tangent computes
// ratio of the sides of a right triangle, multiplying by half
// the base gives the other side, hence the height.
int armHeight =
(int) (armBase / 2 * Math.tan(Math.toRadians(72)));
// The center offset is the distance from the middle of a given
// base to the center of the inner pentagon.
int centerOffset =
(int) (armBase / 2 * Math.tan(Math.toRadians(54)));
// this works by creating the first arm, rotating 72 degrees
// and then adding the other two coodinates of succeeding arms.
star.addPoint(startx, starty);
star.addPoint(startx + armBase / 2, starty - armHeight);
star.addPoint(startx + armBase, starty);
for (int j = 0; j < 4; j++) {
rotatePolygon(-Math.PI / 5 * 2, startx + armBase / 2,
starty + centerOffset, star);
star.addPoint(startx + armBase / 2, starty - armHeight);
star.addPoint(startx + armBase, starty);
}
star.npoints--;
star.translate(-star.getBounds().x,-star.getBounds().y);
return star;
}
// This is general purpose rotation that rotates about a center
// point. This can be derived using the double angle identities of
// for sin and cosine.
private void rotatePolygon(double ang, double sx, double sy,
Polygon poly) {
for (int j = 0; j < poly.npoints; j++) {
double x = poly.xpoints[j];
double y = poly.ypoints[j];
double xx = sx + (x - sx) * Math.cos(ang)
- (y - sy) * Math.sin(ang);
double yy = sy + (x - sx) * Math.sin(ang)
+ (y - sy) * Math.cos(ang);
poly.xpoints[j] = (int) xx;
poly.ypoints[j] = (int) yy;
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class StarryNight2GUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new StarryNight2GUI());
}
@Override
public void run() {
JFrame frame = new JFrame("Starry Night");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DrawingPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = 1L;
public DrawingPanel() {
this.setBackground(Color.BLACK);
this.setPreferredSize(new Dimension(640, 480));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Color groundColor = new Color(95, 95, 95);
g.setColor(groundColor);
g.fillRect(0, getHeight() - 30, getWidth(), 30);
Polygon polygon = createStar(new Point(320, 240), 80);
g.setColor(Color.YELLOW);
g.fillPolygon(polygon);
}
private Polygon createStar(Point centerPoint, int radius) {
Polygon polygon = new Polygon();
// 72, 144, 216, 288, 360
// 36, 108, 180, 252, 324
// 54, 126, 198, 270, 342
// 18, 54, 90, 126, 162, 198, 234, 270, 306, 342
for (int angle = 18; angle < 360; angle += 72) {
double r = 0.42 * radius;
Point point = toCartesian(centerPoint, angle, r);
polygon.addPoint(point.x, point.y);
point = toCartesian(centerPoint, angle + 36, radius);
polygon.addPoint(point.x, point.y);
}
return polygon;
}
private Point toCartesian(Point centerPoint, int angle, double radius) {
double theta = Math.toRadians(angle);
int x = centerPoint.x + (int) Math.round(Math.cos(theta) * radius);
int y = centerPoint.y + (int) Math.round(Math.sin(theta) * radius);
return new Point(x, y);
}
}
}
public static Shape radiusShape(int points, int... radii)
{
Polygon polygon = new Polygon();
for (int i = 0; i < points; i++)
{
double radians = Math.toRadians(i * 360 / points);
int radius = radii[i % radii.length];
double x = Math.cos(radians) * radius;
double y = Math.sin(radians) * radius;
polygon.addPoint((int)x, (int)y);
}
Rectangle bounds = polygon.getBounds();
polygon.translate(-bounds.x, -bounds.y);
return polygon;
}
Shape star = ShapeUtils.radiusShape(10, 30, 12);