用户输入后重新绘制图像

时间:2017-04-05 08:49:43

标签: java repaint fractals

因此,对于我的项目,我决定创建一个分形模拟器,到目前为止,如果手动输入用户输入的复数值,我的代码确实会输出一个工作分形,即创建对象 - > ComplexNumber cn = new ComplexNumber(0.004,-0768)。但是,为了使我的代码更进一步,我希望有用户输入,以使我的模拟更加用户友好,所以我创建了一个JButton for x-Coordinate和JTextField来获取用户输入。问题是,无论我输入什么输入,我的分形都不会改变并保持不变。我一直试图解决这个问题很长时间没有成功,所以任何帮助都会非常感激。

非常感谢:)

分形代码:

// The width and height of the JFrame window.
private static final int WIDTH = 800;// Fractal best rendered as a box format hence, HEIGHT = WIDTH.
private static final int HEIGHT = 480;
// Complex Number class instantiation and object c created with hard coded values for the JuliaSet to be fed.
ComplexNumber c = new ComplexNumber(xCoordinate, 0.281);  //Error here?
protected double xCoordinate;
// Boolean pixel Array to represent two colours plotting a fractal based off true or false if it is in the Julia Set.
private boolean[][] pixels = null;
// The limits of the Complex Plane for which the fractal is bounded between.
private double minX = -1.5;
private double maxX = 1.5;
private double minY = -1.5;
private double maxY = 1.5;

// The image to draw my fractal upon.
private BufferedImage image = null;
private JButton xValueInputButton;
private JTextField xValueInput;
private JPanel panel;

// The recursiveLimit of the ComplexNumber to be allowed in the Set. Depth also relates to detail of the fractal.
private double depth = 1;
// The number of times that the algorithm recursilevly calls itself.
private int iterations = 50;   // Optimum = 50 use 1 for testing

public Fractals(){
    // Create a BufferedImage to paint on.
    image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);
    // Fill the boolean[][].
    getPixels();
    // Go through the array and set the pixel color on the BufferedImage
    // according to the pixels in the array.
    for(int i=0;i<WIDTH;i++){
        for(int j=0;j<HEIGHT;j++){
            // If the point is in the Set, color it White, else, color it Black.
            if(pixels[i][j]) image.setRGB(i, j, Color.MAGENTA.getRGB());
            if(!pixels[i][j]) image.setRGB(i, j, Color.BLACK.getRGB());
        }
    }

    // Create a Frame to display the Fractal.
    JFrame f = new JFrame("Fractalis");
    f.setLayout(new BorderLayout());
    xValueInput = new JTextField(10);
    xValueInputButton = new JButton("Apply");
    xValueInput.setPreferredSize(new Dimension(35, 35));
    JPanel inputsPanel = new JPanel();
    panel = new JPanel(){
        // Override the paint method (for simplicity)
        @Override
        public void paintComponent(java.awt.Graphics g){
            g.drawImage(image, 0, 0, this);
        }
    };
    // Set other Frame settings.
    f.add(panel, BorderLayout.CENTER);
    f.add(inputsPanel, BorderLayout.EAST);
    inputsPanel.add(xValueInput);
    inputsPanel.add(xValueInputButton);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().setBackground(null);
    f.setSize(960,640);
    f.setVisible(true);
}

protected void xValueInputButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                  
    xCoordinate = ((Double.parseDouble(xValueInput.getText())));
}

0 个答案:

没有答案