2013-10-08 87 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); 
}