JAVA - 从不同的班级获取随机

时间:2014-02-07 23:26:21

标签: java random jframe jpanel draw

嘿我有一个应用程序在随机位置生成随机形状,现在我试图将代码分成负责某些事情的类。但是,当我删除负责“随机化”的部分代码并将其移至另一个类时,它不再起作用(不随机化)。有任何想法吗?我的代码如下:

MAIN CLASS

import java.awt.BorderLayout;

public class Main extends JFrame {

    public Main() {
        setLayout(new BorderLayout());
        add(new TestPanel(), BorderLayout.CENTER);
}

public static void main(String[] args) {
    new TestPanel();
    Main frame = new Main();
    frame.setSize(1000, 700);
    frame.setTitle("Random things");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    //frame.setResizable(false);
}

}

TESTPANEL CLASS

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestPanel extends JPanel {
    Randoms randomized = new Randoms();
    int howManyGeometrics = randomized.random1.nextInt(5-1) + 1;
    int whichGeometrics = randomized.random1.nextInt(3);

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (int i = 0; i < howManyGeometrics; i++) {
            draw(g);
        }
    }

    public void draw(Graphics g) {
        if(whichGeometrics==0){
            //oval
            g.setColor(randomized.randomColour2);
            g.fillOval(randomized.x1, randomized.x2, randomized.x4, randomized.x3);

            //triangle
            g.setColor(randomized.randomColour3);
            int xpoints[] = {randomized.x1+10, randomized.x2+15, randomized.x3+20};
            int ypoints[] = {randomized.x4-20, randomized.x3-15, randomized.x2-20};
            int npoints = 3;
            g.fillPolygon(xpoints, ypoints, npoints);

            //hourglass
            g.setColor(randomized.randomColour4);
            int a1=50+randomized.x1;
            int a2=200+randomized.x2;
            int a3=a1;
            int a4=a2;
            int b1=a1;
            int b2=a2/2;
            int b3=a2/2;
            int b4=a1;
            int x1points[] = {a1, a2, a3, a4};
            int y1points[] = {b1, b2, b3, b4};
            int ntpoints = 4;
            g.fillPolygon(x1points, y1points, ntpoints);
        }

        else if(whichGeometrics==1){
            //rectangle
            g.setColor(randomized.randomColour1);
            g.fillRect(randomized.x2, randomized.x3, randomized.x1, randomized.x4/2);

            //triangle
            g.setColor(randomized.randomColour3);
            int xpoints[] = {randomized.x1+10, randomized.x2+15, randomized.x3+20};
            int ypoints[] = {randomized.x4-20, randomized.x3-15, randomized.x2-20};
            int npoints = 3;
            g.fillPolygon(xpoints, ypoints, npoints);

            //hourglass
            g.setColor(randomized.randomColour4);

            int a1=50+randomized.x1;
            int a2=200+randomized.x2;
            int a3=a1;
            int a4=a2;
            int b1=a1;
            int b2=a2/2;
            int b3=a2/2;
            int b4=a1;
            int x1points[] = {a1, a2, a3, a4};
            int y1points[] = {b1, b2, b3, b4};
            int ntpoints = 4;
            g.fillPolygon(x1points, y1points, ntpoints);
            }

        else if(whichGeometrics==2){
            //rectangle
            g.setColor(randomized.randomColour1);
            g.fillRect(randomized.x2, randomized.x3, randomized.x1, randomized.x4/2);

            //oval
            g.setColor(randomized.randomColour2);
            g.fillOval(randomized.x1, randomized.x2, randomized.x4, randomized.x3);

            //hourglass
            g.setColor(randomized.randomColour4);

            int a1=50+randomized.x1;
            int a2=200+randomized.x2;
            int a3=a1;
            int a4=a2;
            int b1=a1;
            int b2=a2/2;
            int b3=a2/2;
            int b4=a1;
            int x1points[] = {a1, a2, a3, a4};
            int y1points[] = {b1, b2, b3, b4};
            int ntpoints = 4;
            g.fillPolygon(x1points, y1points, ntpoints);
            }

        else if(whichGeometrics==3){
            //rectangle
            g.setColor(randomized.randomColour1);
            g.fillRect(randomized.x2, randomized.x3, randomized.x1, randomized.x4/2);

            //oval
            g.setColor(randomized.randomColour2);
            g.fillOval(randomized.x1, randomized.x2, randomized.x4, randomized.x3);

            //triangle
            g.setColor(randomized.randomColour3);
            int xpoints[] = {randomized.x1+10, randomized.x2+15, randomized.x3+20};
            int ypoints[] = {randomized.x4-20, randomized.x3-15, randomized.x2-20};
            int npoints = 3;
            g.fillPolygon(xpoints, ypoints, npoints);
            }
    }
}

RANDOMS CLASS

import java.awt.Color;
import java.util.Random;

public class Randoms {

    Random random1 = new Random(System.currentTimeMillis());

    int x1 = random1.nextInt(800);
    int x2 = random1.nextInt(800);
    int x3 = random1.nextInt(800);
    int x4 = random1.nextInt(800);

    int red = random1.nextInt(255);
    int green = random1.nextInt(255);
    int blue = random1.nextInt(255);

    Color randomColour1 = new Color(red,green,blue);
    Color randomColour2 = new Color(green,blue,red);
    Color randomColour3 = new Color(blue,red,green);
    Color randomColour4 = new Color(green,red,red,green);
}

我将非常感谢您的帮助或/和建议:)

3 个答案:

答案 0 :(得分:1)

您创建了Randoms的一个实例,然后继续使用它的成员x1等,而不更改其值。

我怀疑您的旧工作代码创建了多个Randoms个实例?

一个简单的解决方案是向reRandomize()添加Randoms方法,将每个成员设置为一个新的随机值,并在每个形状之前调用它。

答案 1 :(得分:1)

John3136已在https://stackoverflow.com/a/21639680中指出,主要问题是你总是使用相同的Randoms实例。但诚然,这个Randoms类本身在目前的形式下似乎有问题。随机值存储为 fields ,隐式导致原始问题。我可以想象,如果它包含适当的方法,那么这个Randoms类可能会更有用。例如,像

这样的方法
class Randoms 
{
    private static final Random random = 
        new Random(System.currentTimeMillis());

    public static Color createRandomColor()
    {
        int red = random.nextInt(255);
        int green = random.nextInt(255);
        int blue = random.nextInt(255);
        return new Color(red,green,blue);
    }

    public static int nextInt(int min, int max)
    {
        return random.nextInt(max - min) + min;
    }
}

除此之外:“沙漏”的代码始终相同。在这种情况下,您应该考虑创建一个类似

的方法
private void paintHourglass(Graphics g, int x0, int x1) { 
    // Here the repeated code...
}

也许你应该更清楚地描述你想要达到的目标。如果您只想生成一组随机形状,则可以创建它们并将它们存储在列表中:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class RandomsTest extends JFrame {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.getContentPane().add(new TestPanel());
        frame.setSize(1000, 700);
        frame.setTitle("Random things");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        //frame.setResizable(false);
    }

}

class TestPanel extends JPanel
{
    private List<Geometry> geometries;

    TestPanel()
    {
        geometries = new ArrayList<Geometry>();
        int numGeometries = Randoms.nextInt(5, 8);
        for (int i=0; i<numGeometries; i++)
        {
            geometries.add(new Geometry());
        }
    }

    public void paintComponent(Graphics gr)
    {
        super.paintComponent(gr);
        Graphics2D g = (Graphics2D)gr;
        for (Geometry geometry : geometries)
        {
            geometry.draw(g);
        }
    }
}




class Geometry
{
    private final Color color;
    private final Shape shape;

    Geometry()
    {
        this.color = Randoms.createRandomColor();
        this.shape = Shapes.createRandomShape();
    }

    void draw(Graphics2D g)
    {
        g.setColor(color);
        g.fill(shape);
    }

}

class Shapes
{
    static Shape createRandomShape()
    {
        int type = Randoms.nextInt(2);
        switch (type)
        {
            case 0 : return createRandomOval();
            case 1 : return createRandomRectangle();
        }
        return createRandomOval();
    }

    private static Shape createRandomOval()
    {
        return new Ellipse2D.Double(
            Randoms.nextInt(800),
            Randoms.nextInt(800),
            Randoms.nextInt(800),
            Randoms.nextInt(800));
    }

    private static Shape createRandomRectangle()
    {
        return new Rectangle2D.Double(
            Randoms.nextInt(800),
            Randoms.nextInt(800),
            Randoms.nextInt(800),
            Randoms.nextInt(800));
    }
}

class Randoms
{
    private static final Random random =
        new Random(System.currentTimeMillis());

    public static Color createRandomColor()
    {
        int red = random.nextInt(255);
        int green = random.nextInt(255);
        int blue = random.nextInt(255);
        return new Color(red,green,blue);
    }

    public static int nextInt(int min, int max)
    {
        return random.nextInt(max - min) + min;
    }

    public static int nextInt(int max)
    {
        return random.nextInt(max);
    }
}

答案 2 :(得分:0)

每次需要新的随机数时,都应该使用Math.random()。