随机生成形状 [英] Randomly Generating a shape

查看:139
本文介绍了随机生成形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Java屏幕保护程序项目,到目前为止我有一个很好的部分。我需要代码在随机位置生成随机颜色的随机形状。我相信我有所有的随机方面照顾,但现在我只需要使用一个计时器创建这些形状在500毫秒的间隔。我还需要创建一个计数器来计数30个形状,然后清除屏幕,然后重新开始。 (我有背景和keylistener为项目的其他部分添加,但他们工作完美,以防任何人想知道为什么他们在那里)。

I am working on a Java screen saver project and so far I have a good portion done. I need the code to generate a random shape of random color in a random position. I believe I have all of the random aspects taken care of, but now I just need to use a timer to create these shapes at 500 ms intervals. I also need to create a counter to count 30 shapes and then clear the screen and start again. (I have the background and the keylistener added for other parts of the project, but they are working perfect, in case anyone was wondering why they were there).

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

public class ScreenSaver1 extends JPanel implements ActionListener {
    private JFrame frame = new JFrame("FullSize");
    private Rectangle rectangle;
    Timer t;
    int x1, y1;
    boolean full;

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        int shape;
        shape = (int)(Math.random() * 4);
    }

    ScreenSaver1() {
        t = new Timer(500, this);
        t.setDelay(500);
        t.start();
        // Remove the title bar, min, max, close stuff
        frame.setUndecorated(true);
        // Add a Key Listener to the frame
        frame.addKeyListener(new KeyHandler());
        // Add this panel object to the frame
        frame.add(this);
        // Get the dimensions of the screen
        rectangle = GraphicsEnvironment.getLocalGraphicsEnvironment()
        .getDefaultScreenDevice().getDefaultConfiguration().getBounds();
        // Set the size of the frame to the size of the screen
        frame.setSize(rectangle.width, rectangle.height);
        frame.setVisible(true);
        // Remember that we are currently at full size
        full = true;
    }


// This method will run when any key is pressed in the window
class KeyHandler extends KeyAdapter {
    public void keyPressed(KeyEvent e) {
        // Terminate the program.
        if (e.getKeyChar() == 'x') {
            System.out.println("Exiting");
            System.exit(0);
        }
        else if (e.getKeyChar() == 'r') {
            System.out.println("Change background color");
            setBackground(new Color((int)(Math.random() * 256), (int)(Math.random() * 256), (int)(Math.random() * 256)));
            repaint();
        }
        else if (e.getKeyChar() == 'z') {
            System.out.println("Resizing");
            frame.setSize((int)rectangle.getWidth() / 2, (int)rectangle.getHeight());
        }
    }

}

private void makeLine(Graphics g) {
    int x = (int)(Math.random() * rectangle.getWidth());
    int y = (int)(Math.random() * rectangle.getHeight());
    int x1 = (int)(Math.random() * 100);
    int y1 = (int)(Math.random() * 100);
    g.setColor(new Color((int)(Math.random() * 256), (int)(Math.random() * 256), (int)(Math.random() * 256) ));
    g.drawLine(x, y, x1, y1);
}

private void makeRect(Graphics g) {
    int x = (int)(Math.random() * rectangle.getWidth());
    int y = (int)(Math.random() * rectangle.getHeight());
    int width = (int)(Math.random() * 100);
    int height = (int)(Math.random() * 100);
    g.setColor(new Color((int)(Math.random() * 256), (int)(Math.random() * 256), (int)(Math.random() * 256) ));
    g.drawRect(x, y, width, height);
}

private void makeOval(Graphics g) {
    int x = (int)(Math.random() * rectangle.getWidth());
    int y = (int)(Math.random() * rectangle.getHeight());
    int width = (int)(Math.random() * 100);
    int height = (int)(Math.random() * 100);
    g.setColor(new Color((int)(Math.random() * 256), (int)(Math.random() * 256), (int)(Math.random() * 256) ));
    g.drawOval(x, y, width, height);
}

private void makeRoundRect(Graphics g) {
    int x = (int)(Math.random() * rectangle.getWidth());
    int y = (int)(Math.random() * rectangle.getHeight());
    int width = (int)(Math.random() * 100);
    int height = (int)(Math.random() * 100);
    int arcWidth = (int)(Math.random() * width);
    int arcHeight = (int)(Math.random() * height);
    g.setColor(new Color((int)(Math.random() * 256), (int)(Math.random() * 256), (int)(Math.random() * 256) ));
    g.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
}

public static void main(String[] args) {
        ScreenSaver1 obj = new ScreenSaver1();
    }
}


推荐答案

不会喜欢我,但我建议你稍微备份...

You're not going to like me, but I would suggest that you back up slightly...

首先,Java提供了一个非常好的基本 Shape 接口,它定义了一个shape应该如何渲染(除了别的以外),所以完全重新发明轮子,我建议你从这开始。

To start with, Java provides a really good basic Shape interface, which defines how a "shape" should be rendered (amongst other things), so rather the completely reinventing the wheel, I would suggest you start with this.

接下来,你需要一个包含 Shape (其中包含位置和大小信息)和颜色,例如...

Next, you need some kind of object that wraps both the Shape (which has position and size information) and the Color, for example...

public class RandomShape {

    private final Color color;
    private final Shape shape;

    public RandomShape(Color color, Shape shape) {
        this.color = color;
        this.shape = shape;
    }

    public Color getColor() {
        return color;
    }

    public Shape getShape() {
        return shape;
    }

    public void paint(Graphics2D g2d) {
        g2d.setColor(color);
        g2d.draw(shape);
        g2d.fill(shape);
    }

}

接下来,我将创建一个列表来包含这些形状...

Next, I would create a List to contain these shapes...

private List<RandomShape> shapes;

这不仅充当您的计数器,而且让更新屏幕变得非常简单。当形状 列表包含30个或更多项目时,您只需清除它并重新绘制屏幕...

This not only acts as your counter, but makes it incredibly simple to update the screen. When the shapes List contains 30 or more items, you simply clear it and repaint the screen...

接下来,需要一个 javax.swing.Timer ,用于触发更新...

Next, you need a javax.swing.Timer, which is used to trigger the updates...

此计时器应该...

检查形状列表需要清除...

Check to see if the shapes list needs to be cleared...

随机生成 Color ...

int r = (int) (Math.random() * 255);
int g = (int) (Math.random() * 255);
int b = (int) (Math.random() * 255);
Color color = new Color(r, g, b);

随机生成形状的大小和位置...

Randomly generate the size and position of the shape...

int width = 10 + (int) (Math.random() * 40);
int height = 10 + (int) (Math.random() * 40);
int x = (int) (Math.random() * (getWidth() - width));
int y = (int) (Math.random() * (getHeight() - height));

随机生成底层基本形状...

Randomly generate the underlying basic shape...

Shape shape = null;
switch (whichShape) {
    case 0:
        shape = new Line2D.Double(x, y, x + width, y + height);
        break;
    case 1:
        shape = new Rectangle2D.Double(x, y, width, height);
        break;
    case 2:
        shape = new Ellipse2D.Double(x, y, width, height);
        break;
}

创建 RandomShape ,将其添加到列表并重新绘制组件...

Create the RandomShape, add it to the list and repaint the component...

RandomShape randomShape = new RandomShape(color, shape);
shapes.add(randomShape);
repaint();

Simple;)

你需要绘制组件,你只需要遍历形状列表...

Now, when you need to paint the component, you simply iterate over the list of shapes...

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    for (RandomShape shape : shapes) {
        shape.paint(g2d);
    }
    g2d.dispose();
}

查看如何使用Swing计时器使用几何

这篇关于随机生成形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆