绘制嵌套椭圆并枚举它们

xzv2uavs  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(372)

正在尝试编写某种gui,需要绘制嵌套的4096个椭圆,并希望枚举它们(从1到4096)。但是,无法弄清楚drawoval方法是如何工作的(更具体地说,在查找此方法的数学公式以及在方法中使用坐标参数方面几乎没有问题)
到目前为止,我做了如下工作:这段代码画了一个圆,并试图减少参数,但没有成功。你能解释一下如何正确使用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();
        }
    });
}
}
hc2pp10m

hc2pp10m1#

这是在这里提供的示例之外的,该示例演示了如何绘制多个连续的圆,并演示了如何将文本呈现给它们。。。

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();
        }
    }       
}

我也建议你用二维图形来增强自己

相关问题