gpt4 book ai didi

java - 尝试创建一个使用参数方程绘制椭圆的程序,但线条未显示。为什么? ( java )

转载 作者:行者123 更新时间:2023-11-29 03:05:52 25 4
gpt4 key购买 nike

我正在尝试创建一个程序,使用参数方程,可以在直线之间画线并创建一个椭圆。这是我的代码:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.util.ArrayList;

import javax.swing.JFrame;


public class test extends JFrame{

ArrayList<Line2D> lines = new ArrayList<>();
public test(){
this.setTitle("test");
this.setSize(800, 600);
this.setLocationRelativeTo(null);
this.setLayout(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
drawCircle(3,3.0,3.0);

this.setVisible(true);


System.out.println(lines.size());
}

public void drawCircle(int radio, double xCenter, double yCenter) {
double t = 0;
double xPoint;
double yPoint;
double xActual = xCenter;
double yActual = yCenter + radio*Math.sin(0);
t += 0.01;

while(t < 360) {
xPoint = xCenter + radio*Math.cos(t);
yPoint = yCenter + radio*Math.sin(t);

lines.add(new Line2D.Double(xActual, yActual, xPoint, yPoint));
this.repaint();

t += 0.01;
xActual = xPoint;
yActual = yPoint;
}
}

public void paint(Graphics g) {
super.paint(g);

for(Line2D s : lines){
((Graphics2D) g).draw(s);
}
}
}

我没有收到任何错误,但线条似乎没有出现在框架中。我做错了什么?

最佳答案

欢迎来到为什么不应该从 JFrame 扩展的精彩世界以及为什么你不应该覆盖 paint顶级容器,如 JFrame

主要问题是,框架有边框。如果您覆盖 paintJFrame并在 0x0 上作画,您实际上是在框架下绘画。您的圆圈(比例为 3)足够小,可以完全绘制在窗口的标题栏下方。

参见 How to get the EXACT middle of a screen, even when re-sized , How can I set in the midst?Java AWT drawString() does not display on window有关此问题的示例以及为什么您不应覆盖 paint顶级容器

相反,使用类似 JPanel 的组件并覆盖它的 paintComponent并将您的自定义绘画放在那里。然后将它放在一个 JFrame 的实例中, 例如...

Panel in frame

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.HeadlessException;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test extends JFrame {

public static void main(String[] args) {
new Test();
}

public Test() throws HeadlessException {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Test");
frame.add(new TestPane());
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

ArrayList<Line2D> lines = new ArrayList<>();

public TestPane() {
drawCircle(3, 3.0, 3.0);
}

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

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();

g2d.setColor(Color.RED);
System.out.println(lines.size());
for (Line2D s : lines) {
g2d.draw(s);
}
g2d.dispose();
}

public void drawCircle(int radio, double xCenter, double yCenter) {
double t = 0;
double xPoint;
double yPoint;
double xActual = xCenter;
double yActual = yCenter + radio * Math.sin(0);
t += 0.01;

while (t < 360) {
xPoint = xCenter + radio * Math.cos(t);
yPoint = yCenter + radio * Math.sin(t);

// System.out.println(xActual + "x" + yActual + "x" + xPoint + "x" + yPoint);
lines.add(new Line2D.Double(xActual, yActual, xPoint, yPoint));

t += 0.01;
xActual = xPoint;
yActual = yPoint;
}
}
}

}

你的下一个问题是,0x0 (或足够接近它)是圆的中心点。您需要能够计算出 x/y生成形状时的偏移量,使圆的上/左角位于 0x0相反

例如,在测试您的代码时,我增加了 radio100 (看看它是否有任何不同,这就是我发现你在框架下绘画的方式)并且它产生了类似...的东西

Offset

关于java - 尝试创建一个使用参数方程绘制椭圆的程序,但线条未显示。为什么? ( java ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32215850/

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