gpt4 book ai didi

java - 绘制嵌套椭圆并枚举它们

转载 作者:行者123 更新时间:2023-12-01 14:29:06 25 4
gpt4 key购买 nike

尝试编写某种 Gui,需要绘制嵌套的 4096 个椭圆并想要枚举它们(从 1 到 4096)。但是,无法弄清楚drawOval方法是如何工作的(更具体地说,为此找到数学公式并在方法中使用坐标参数没有什么问题)

这是我到目前为止所做的:这段代码画了 1 个圆圈,并尝试减少参数,但没有成功。您能解释一下如何正确使用draw方法的参数以及如何为此制定公式吗?

其次,我想给每个椭圆一个数字,该怎么做?

    protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D obj = (Graphics2D)g;
obj.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
obj.setColor(Color.black);
obj.drawOval(0,0, getWidth()+1, getHeight()+1);

}

private static void make() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new deneme2());
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
make();
}
});
}
}

最佳答案

这是对示例的补充,提供 here它展示了如何绘制多个连续的圆圈,并演示了如何向它们呈现文本......

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Circles {

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

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

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

public class TestPane extends JPanel {

public TestPane() {
}

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

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
FontMetrics fm = g2d.getFontMetrics();
int padding = fm.getHeight() + 4;
int width = getWidth() - 1 - padding;
int height = getHeight() - 1 - padding;

int radius = Math.min(width, height);
int x = (padding + (width - radius)) / 2;
int y = (padding + (height - radius)) / 2;
g2d.drawOval(x, y, radius, radius);

String text = "This is a test";

int centerX = (padding + radius) / 2;
int centerY = (padding + radius) / 2;

x = (centerX - (fm.stringWidth(text)) / 2);
y = centerY - (radius / 2);
g2d.drawString(text, x, y);

x = centerX - (radius / 2);
g2d.drawString(text, x, centerY);

x = (centerX - (fm.stringWidth(text)) / 2);
y = centerY + (radius / 2);
g2d.drawString(text, x, y);

x = (centerX - (fm.stringWidth(text)) / 2);
y = centerY - (radius / 2);
g2d.rotate(Math.toRadians(90), getWidth() / 2, getHeight() / 2);
g2d.drawString(text, x, y);

g2d.dispose();
}
}
}

我还建议您使用 2D Graphics 来农场化自己

关于java - 绘制嵌套椭圆并枚举它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16956814/

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