【问题标题】:How would I implement a display showing points within a triangle in java?如何在java中实现显示三角形内点的显示?
【发布时间】:2021-12-15 15:18:04
【问题描述】:

所以我正在尝试制作一个迭代程序来展示带有谢尔宾斯基三角形的混沌游戏。您从 (0,0) 开始,然后随机走到 (0, 0)、(1, sqrt(3)) 或 (2, 0) 的中途。重复会留下分形图案。我在java中的代码大致是:

public class Sierpinski {
      public static void main(String[] args) {
            double x = 0;
            double y = 0;
            int n = 0;
            while(true) {
                     // point on at (x,y) - this is what I need help with
                     // generates random number from 0 to 2
                     n = (int)(3 * Math.random())
                     // x and y randomly go halfway to one point
                     x += ((n == 1) ? 1 : 0) + ((n == 2) ? 2: 0);
                     y += ((n == 1) ? Math.sqrt(3) : 0);
                     x /= 2;
                     y /= 2;
            }
      }
}

如何实现一个在 x 和 y 方向上以 0 到 2 为边界的图形,在每次迭代时显示这些点? 谢谢

【问题讨论】:

  • 除了你的问题太宽泛之外,你的代码不会像你认为的那样做。您正在使用整数除法,这将为您提供 0 xy
  • 是的 x 和 y 应该是双倍的

标签: java graph iteration


【解决方案1】:

顺便说一句,这是一个不错的项目;)

要在 Java 中创建一个窗口并绘制图形,您需要两个东西:一个 JFrame 和一个 Canvas。

  • JFrame 是您的应用程序的框架,就像您打开 Windows 应用程序时一样。

  • 画布是我们可以在其上绘制图形的表面。它有一个宽度和一个高度 以像素为单位。

为了简化事情,我创建了一个 Window 类来管理它们。代码如下:

import java.awt.Graphics;

import javax.swing.JFrame;

import java.awt.Canvas;

import java.awt.Color;

public class Window
{
    
    //width and height of the canvas
    private static final int WIDTH = 600;
    private static final int HEIGHT = 600;

    JFrame frame;
    Canvas canvas;



    public Window()
    {
        JFrame frame = new JFrame();
        canvas = new Canvas();

        canvas.setSize(WIDTH, HEIGHT);

        //We add the canvas inside of the frame
        frame.add(canvas);

        //make the frame to fit the size of the canvas
        frame.pack();

        //click on the X button to close the app
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //set the app in the middle of the screen
        frame.setLocationRelativeTo(null);

        frame.setTitle("Sierpinski");
        frame.setVisible(true);
    }





    //We call this method from your code
    public void paint(double x, double y)
    {
        /*
        Since your numbers range is between 0 and 2,
        We need to adapt it to the size of the canvas.
        The result would be random coordinates on the canvas.
        */
        int coordX = (int)(x / 2 * WIDTH);
        /*
            Because in Java Y axe is reversed, we need to convert into its reversed value on the screen. Ex:

            pixel (0,0)  => pixel (0,599)
            pixel (0,10) => pixel (0,589)

        */
        int coordY = HEIGHT - ((int)(y / 2 * HEIGHT) + 1);

        /*
        Graphics is like a paintbrush for a specific object.
        We are asking the canvas to give us his paintbrush.
        */
        Graphics g = canvas.getGraphics();


        //Try to execute the line below!
        //g.setColor(Color.BLUE);



        /*
        Draw a rectangle of width and height of 1 pixel.
        */
        g.fillRect(coordX, coordY, 1, 1);
    }

}

最后,我们需要在你的代码中创建这个 Window 对象,并调用paint方法:

public class Sierpinski {
    
    public static void main(String[] args) {
         
        Window window = new Window();

        double x = 0;
        double y = 0;
        int n = 0;
        while(true) {
                 // point on at (x,y) - this is what I need help with
                 // generates random number from 0 to 2
                 n = (int)(3 * Math.random());
                 // x and y randomly go halfway to one point
                 x += ((n == 1) ? 1 : 0) + ((n == 2) ? 2: 0);
                 y += ((n == 1) ? Math.sqrt(3) : 0);
                 x /= 2;
                 y /= 2;
                 
                 window.paint(x, y);
        }
        

    }

}

你应该得到这样的结果:

The result expected

你可以用它做很多事情。您可以在 Oracle 网站上查看官方文档或在 Youtube 上查看一些教程。

Canvas documentation

Graphics documentation

JFrame documentation

玩得开心!

【讨论】:

    猜你喜欢
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多