gpt4 book ai didi

java - 仅使用直线和仿射变换来绘制多边形

转载 作者:行者123 更新时间:2023-12-02 01:33:19 24 4
gpt4 key购买 nike

所以基本上我想做的是仅使用仿射变换和单个绘制语句绘制具有任意边数的多边形:g2.draw(new Line2D(0,0,1,0)。是的,我知道有一个 drawPolygon 函数 - 这不是这个问题的重点。为了简单起见,该多边形是一个“规则”多边形,意味着它的所有边和边之间的角度相同。

主要问题是我不完全理解如何使点正确对齐:

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class PolygonDrawer extends JComponent implements Runnable, ChangeListener {

JFrame jframe;

public PolygonDrawer() {
SwingUtilities.invokeLater(this);
}

public static void main(String[] args) {
System.setProperty("awt.useSystemAAFontSettings","on");
new PolygonDrawer();
}

public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

public void run() {
jframe = new JFrame("PolygonDrawer");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel mainpanel = new JPanel();
mainpanel.setLayout(new BorderLayout());
mainpanel.add(this, BorderLayout.CENTER);
jframe.getContentPane().add(mainpanel);
jframe.setVisible(true);
jframe.pack();
}

@Override
public void stateChanged(ChangeEvent e) {
repaint();
}

@Override
protected void paintComponent(Graphics g) {
// Configuration stuff
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Dimension dim = this.getSize();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Fill background
g2.setColor(Color.white);
g2.fillRect(0, 0, dim.width, dim.height);

g2.setColor(Color.black);
AffineTransform save = g2.getTransform();
// Example polygon is a regular square
int noVertices = 4;
int rotationAngleInDegrees = 90;

for (int i = 0; i < noVertices; i++) {
g2.setTransform(save);
AffineTransform at = new AffineTransform();
at.rotate(Math.toRadians(i + 1) * rotationAngleInDegrees);
at.translate(save.getTranslateX(), save.getTranslateY());
at.scale(100, 1);
g2.setTransform(at);
g2.draw(new Line2D.Double(0.0,0.0,1.0,0.0));
save = g2.getTransform();
}
}
}

我想要的只是在屏幕上绘制一个正方形。然而,这些线条到处都是……我猜这与 save 的使用方式有关。

最佳答案

这使用 affineTransforms 和单个绘制语句。希望对您有所帮助。


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class PolygonDrawer extends JPanel {

JFrame jframe;
int WIDTH = 500;
int HEIGHT = 500;
int AX = 100;
int AY = 100;

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new PolygonDrawer().start());
}
public void start() {
jframe = new JFrame("PolygonDrawer");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
jframe.add(this);
jframe.pack();
jframe.setLocationRelativeTo(null);
jframe.setVisible(true);
}

@Override
protected void paintComponent(Graphics g) {
// Configuration stuff
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
Dimension dim = this.getSize();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// Fill background
g2d.setColor(Color.white);
g2d.fillRect(0, 0, dim.width, dim.height);

g2d.setColor(Color.black);
AffineTransform af = g2d.getTransform();

int noVertices = 6;
double rotationAngle = ((2 * Math.PI) / noVertices);
double x1 = WIDTH / 2 - AX;
double y1 = HEIGHT / 2 - AY;
af.translate(x1, y1);
for (int i = 0; i < noVertices; i++) {
af.rotate(rotationAngle, AX, AY);
double x2 = af.getTranslateX();
double y2 = af.getTranslateY();
g2d.draw(new Line2D.Double(x1, y1, x2, y2));
x1 = x2;
y1 = y2;
}
}
}

关于java - 仅使用直线和仿射变换来绘制多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57541905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com