• <tfoot id='cl2qu'></tfoot>
    <i id='cl2qu'><tr id='cl2qu'><dt id='cl2qu'><q id='cl2qu'><span id='cl2qu'><b id='cl2qu'><form id='cl2qu'><ins id='cl2qu'></ins><ul id='cl2qu'></ul><sub id='cl2qu'></sub></form><legend id='cl2qu'></legend><bdo id='cl2qu'><pre id='cl2qu'><center id='cl2qu'></center></pre></bdo></b><th id='cl2qu'></th></span></q></dt></tr></i><div id='cl2qu'><tfoot id='cl2qu'></tfoot><dl id='cl2qu'><fieldset id='cl2qu'></fieldset></dl></div>
        • <bdo id='cl2qu'></bdo><ul id='cl2qu'></ul>
      1. <legend id='cl2qu'><style id='cl2qu'><dir id='cl2qu'><q id='cl2qu'></q></dir></style></legend>

        <small id='cl2qu'></small><noframes id='cl2qu'>

      2. 在 Java 中绘制谢尔宾斯基三角形

        时间:2023-05-28
          <tbody id='vk0Dg'></tbody>
            <legend id='vk0Dg'><style id='vk0Dg'><dir id='vk0Dg'><q id='vk0Dg'></q></dir></style></legend>

              <bdo id='vk0Dg'></bdo><ul id='vk0Dg'></ul>
              <tfoot id='vk0Dg'></tfoot>
                  <i id='vk0Dg'><tr id='vk0Dg'><dt id='vk0Dg'><q id='vk0Dg'><span id='vk0Dg'><b id='vk0Dg'><form id='vk0Dg'><ins id='vk0Dg'></ins><ul id='vk0Dg'></ul><sub id='vk0Dg'></sub></form><legend id='vk0Dg'></legend><bdo id='vk0Dg'><pre id='vk0Dg'><center id='vk0Dg'></center></pre></bdo></b><th id='vk0Dg'></th></span></q></dt></tr></i><div id='vk0Dg'><tfoot id='vk0Dg'></tfoot><dl id='vk0Dg'><fieldset id='vk0Dg'></fieldset></dl></div>

                  <small id='vk0Dg'></small><noframes id='vk0Dg'>

                • 本文介绍了在 Java 中绘制谢尔宾斯基三角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在绘制谢尔宾斯基三角形(或谢尔宾斯基垫片)的代码时遇到了一些问题,但我不确定是什么问题.绘制三角形的线,然后绘制所有分形,然后消失.帮忙?

                  import javax.swing.*;导入 java.awt.*;公共类 SierpinskiGasket 扩展 JFrame {点 x=新点(5,545),y=新点(300,25),z=新点(605,545),当前=x,目标;私人int计数=0;公共SierpinskiGasket(){超级(谢尔宾斯基垫片");设置大小(610,550);设置默认关闭操作(JFrame.EXIT_ON_CLOSE);getContentPane().setBackground(Color.WHITE);setLocationRelativeTo(null);可调整大小(假);设置可见(真);}公共无效油漆(图形g){super.paint(g);如果(计数==0){g.drawLine(x.x,x.y,y.x,y.y);g.drawLine(x.x,x.y,z.x,z.y);g.drawLine(z.x,z.y,y.x,y.y);} 别的 {而(计数<10000){int 选择=(int)(Math.random()*3);开关(选择){案例0:目标=x;休息;案例1:目标=y;休息;案例2:目标=z;休息;默认值:System.exit(0);}当前=中点(当前,目标);g.drawRect(current.x,current.y,5,5);重绘();计数++;}}计数++;}公共点中点(点a,点b){返回新点((Math.round(a.x+b.x)/2),(Math.round(a.y+b.y)/2));}公共静态无效主要(字符串[]参数){新的谢尔宾斯基垫片();}}

                  解决方案

                  1. 不要从顶级容器(如 JFrame)扩展,你不会为它添加任何好处.
                  2. 避免绘制到顶级容器.而是使用 JPanel 之类的东西.
                  3. 避免覆盖 paint,改用 paintComponent.有关详细信息,请参阅

                    I'm having some issues with my code to draw a Sierpinski's Triangle (or Sierpinski's Gasket), but I'm not sure what the problem is. The lines for the triangle are drawn, then all the fractals, then it disappears. Help?

                    import javax.swing.*;
                    import java.awt.*;
                    
                    public class SierpinskiGasket extends JFrame {
                    
                    Point x=new Point(5,545),
                          y=new Point(300,25),
                          z=new Point(605,545),
                          current=x, target;
                    private int count=0;
                    
                    public SierpinskiGasket () {
                        super("Sierpinski Gasket");
                        setSize(610,550);
                        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        getContentPane().setBackground(Color.WHITE);
                        setLocationRelativeTo(null);
                        setResizable(false);
                        setVisible(true);
                    }
                    
                    public void paint(Graphics g) {
                        super.paint(g);
                        if(count==0) {
                        g.drawLine(x.x,x.y,y.x,y.y);
                        g.drawLine(x.x,x.y,z.x,z.y);
                        g.drawLine(z.x,z.y,y.x,y.y);
                        } else {
                            while(count<10000) {
                                int choice=(int)(Math.random()*3);
                                switch(choice) {
                                    case 0: target=x; break;
                                    case 1: target=y; break;
                                    case 2: target=z; break;
                                    default: System.exit(0);
                                }
                                current=midpoint(current,target);
                                g.drawRect(current.x,current.y,5,5);
                                repaint();
                                count++;
                            }
                        }
                        count++;
                    }
                    
                    public Point midpoint(Point a, Point b) {
                        return new Point((Math.round(a.x+b.x)/2),
                                         (Math.round(a.y+b.y)/2));
                    }
                    
                    public static void main(String[] args) {
                        new SierpinskiGasket();
                    }
                    }
                    

                    解决方案

                    1. Don't extend from a top level container (like JFrame), you're not adding anything of benefit to it.
                    2. Avoid painting to top level containers. Instead use something like JPanel.
                    3. Avoid overriding paint, use paintComponent instead. See Performing Custom Painting for more details

                    This is not how paint works (and I'm not going to try to rewrite your code to make it).

                    Paint is called in response to a number of events when the repaint system decides that part or whole of the UI needs to be updated. Paints are destructive, that is, when paint is called, the Graphics will be completely refreshed, requiring you to "rebuild" the output from scratch.

                    Instead.

                    Write a recursive algorithm that can paint to something like BufferedImage and draw that within the paintComponent...

                    Updated

                    Painting in Swing is controlled by the RepaintManager, it is it's responsibility to determine what and when to repaint the screen. You seem to be thinking the paint is something your control, when it's not.

                    You need to either be prepared to completely repaint the UI or have a buffer prepared that you can paint onto the UI, depending on your needs.

                    import java.awt.BorderLayout;
                    import java.awt.Dimension;
                    import java.awt.EventQueue;
                    import java.awt.Graphics;
                    import java.awt.Graphics2D;
                    import java.awt.Rectangle;
                    import java.awt.geom.AffineTransform;
                    import java.awt.geom.Path2D;
                    import javax.swing.JFrame;
                    import javax.swing.JPanel;
                    import javax.swing.UIManager;
                    import javax.swing.UnsupportedLookAndFeelException;
                    
                    public class SierpinskisGasket {
                    
                        public static void main(String[] args) {
                            new SierpinskisGasket();
                        }
                    
                        public SierpinskisGasket() {
                            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();
                                BaseShape base = new BaseShape(Math.min(getWidth(), getHeight()));
                                Rectangle bounds = base.getBounds();
                                int x = (getWidth() - bounds.width) / 2;
                                int y = (getHeight() - bounds.height) / 2;
                                base.transform(AffineTransform.getTranslateInstance(x, y));
                                g2d.fill(base);
                                g2d.dispose();
                            }
                        }
                    
                        public class BaseShape extends Path2D.Float {
                    
                            public BaseShape(float size) {
                    
                                float subSize = size / 2f;
                                Triangle top = new Triangle(subSize);
                                top.transform(AffineTransform.getTranslateInstance((size - subSize) / 2, 0));
                                append(top, false);
                    
                                Triangle left = new Triangle(subSize);
                                left.transform(AffineTransform.getTranslateInstance(0, subSize));
                                append(left, false);
                    
                                Triangle right = new Triangle(subSize);
                                right.transform(AffineTransform.getTranslateInstance(subSize, subSize));
                                append(right, false);
                    
                            }
                    
                        }
                    
                        public class Triangle extends Path2D.Float {
                    
                            public Triangle(float size) {
                    
                                moveTo(size / 2f, 0);
                                lineTo(size, size);
                                lineTo(0, size);
                                closePath();
                    
                            }
                    
                        }
                    
                    }
                    

                    You should never be calling anything from paint that could alter the state of the UI and trigger another repaint, otherwise you are going to end up in a end cycle of repaints that will eventually consume your CPU.

                    You should also take a look at Painting in AWT and Swing

                    这篇关于在 Java 中绘制谢尔宾斯基三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:从圆心找到一点的角度 下一篇:来自点数组列表的java圆识别

                  相关文章

                    <bdo id='1DDqO'></bdo><ul id='1DDqO'></ul>
                  1. <tfoot id='1DDqO'></tfoot>

                    <small id='1DDqO'></small><noframes id='1DDqO'>

                  2. <i id='1DDqO'><tr id='1DDqO'><dt id='1DDqO'><q id='1DDqO'><span id='1DDqO'><b id='1DDqO'><form id='1DDqO'><ins id='1DDqO'></ins><ul id='1DDqO'></ul><sub id='1DDqO'></sub></form><legend id='1DDqO'></legend><bdo id='1DDqO'><pre id='1DDqO'><center id='1DDqO'></center></pre></bdo></b><th id='1DDqO'></th></span></q></dt></tr></i><div id='1DDqO'><tfoot id='1DDqO'></tfoot><dl id='1DDqO'><fieldset id='1DDqO'></fieldset></dl></div>

                      <legend id='1DDqO'><style id='1DDqO'><dir id='1DDqO'><q id='1DDqO'></q></dir></style></legend>