2013-10-08 58 views
1

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

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(); 
    } 
} 

回答

7

你不会喜欢我,但我会建议您备份咯...

首先,Java提供了一个非常好的基础Shape接口,它定义了如何“形状“应该被呈现(除其他事物之外),所以完全重新发明轮子,我会建议你从此开始。

接下来,你需要某种形式的一个包装对象既Shape(其位置和大小信息)和Color,例如...

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); 
    } 

} 

这甚至有油漆本身的能力。

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

private List<RandomShape> shapes; 

这不仅充当你的柜台,但使得它非常简单的更新屏幕。当shapesList含有30名或更多的项目,只需将其清除和重绘屏幕...

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

此计时器应。 ..看到

检查是否shapes列表需要被清除......

随机生成的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); 

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

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)); 

随机生成基础的基本形状...

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,将它添加到列表中,并重绘组件...

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

简单;)

现在,当您需要绘制组件时,只需遍历形状列表...

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

看看How to use Swing TimersWorking with Geometry

+0

一个循环,我不会恨你想帮助我。我相信你已经帮了我好几次了。唯一的问题是你使用了一个开关,这是一个循环,我不允许使用循环。 – Ryel

+1

'switch'不是一个循环,它是一种条件分支,比如'if'。事实上,如果你想把它写出来,你可以改变'if'语句的'switch'来代替。循环包括“for”,“do”和“while”。坦率地说,如果没有至少一个循环,你就不会得到这个结果 – MadProgrammer

+0

我不知道我是怎么开始考虑这个开关的,如果是其他循环的话。 – Ryel

0

的一种方法,它允许您使用形状作为一个真正的分量退房Playing With Shapes。然后,您只需将该组件添加到面板中,而不必担心自定义绘画。

0

你可以画各种形状,而不使用这样

private void paintAllShapes(Graphics g, int n) { 
    if(n < shapes.size()) { 
     shapes.get(n).paint((Graphics2D)g); 
     paintAllShapes(g, n+1); 
    } 
} 

protected void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    paintAllShapes(g, 0); 
}