2013-07-07 36 views
0
//************************************************************************** 
// PP 6.11 
// 
// Design and implement a program that draws 20 horizontal, evenly spaced 
// parallel lines of random length. 
//************************************************************************** 


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

public class PP6_11 
{ 
    public static void main (String[] args) 
    { 
     JFrame frame = new JFrame ("Lines"); 
     frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 

     LinesPanel panel = new LinesPanel(); 

     frame.getContentPane().add(panel); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

class LinesPanel extends JPanel 
{ 
    private final int WIDTH = 400,HEIGHT = 300, LENGTH = WIDTH/2; 
    private final int SPACE = HEIGHT/20, NUM_LINES = 20; 
    private Random generator; 

我已完成作業,它工作得很好。編譯並運行時,代碼繪製了20行,因爲我使用了「SPACE」變量。我想知道是否有任何方法可以告訴程序我希望使用「NUM_LINES」變量繪製多少行。有沒有一種方法可以確定要使用for循環在頁面上繪製的線的確切數量?

//----------------------------------------------------------------------- 
    // Sets up the drawing panel. 
    //----------------------------------------------------------------------- 

    public LinesPanel() 
    { 
     generator = new Random(); 

     setBackground (Color.black); 
     setPreferredSize (new Dimension (WIDTH, HEIGHT)); 
    } 

    //----------------------------------------------------------------------- 
    // Paints evenly spaced Horizontal lines of random length. 
    // lines that are half the width are highlighted with a re color. 
    //----------------------------------------------------------------------- 

    public void paintComponent (Graphics page) 
    { 

每次我嘗試使用NUM_LINES = 20和空間= 20個變量在for循環中,只汲取幾行。這裏的for循環我用之前 「的for(int i = 0;我< = NUM​​_LINES;我+ = SPACE)」

 for (int i = 0; i <= HEIGHT; i += SPACE) 
     { 
      int y = generator.nextInt(WIDTH) + 1; 

      if (y <= LENGTH) 
      { 
       page.setColor(Color.red); 
       page.drawLine(0,i,y,i); 
      } 
      else 
      { 
       page.setColor(Color.blue); 
       page.drawLine (0,i,y,i); 
      } 
     } 


    } 



} 

有沒有一種方法,以確定有多少行繪製和均勻地間隔開,或我做到這一點的最好方法是什麼?

回答

1

簡單的答案是改變你的for循環:

for (int i = 0; i < HEIGHT; i += (HEIGHT - 1)/(NUM_LINES - 1)) 

請注意,我已經改變了對比,排除了equals(因爲高度實際上是你的窗口外),我們動態地計算之間的距離線。

HEIGHT的減法修正了我們的線位於零座標系中的事實。從NUM_LINES中減去第一行繪製爲零的事實進行調整。請注意,您的解決方案沒有考慮到最後一個因素,但由於您將最後一行畫在屏幕外,因此碰巧「工作」。如果您將HEIGHT更改爲401,則實際上會看到21行。

這是一個更好的實現,它考慮了許多因素,例如重新繪製高度和行間距的奇數倍以及實際的面板大小。它也使線條垂直居中以更美觀。

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

public class PP6_11 
{ 
    public static void main (String[] args) 
    { 
     JFrame frame = new JFrame ("Lines"); 
     frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 

     LinesPanel panel = new LinesPanel(20, 0.5); 
     panel.setPreferredSize (new Dimension (400, 300)); 

     frame.getContentPane().add(panel); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

class LinesPanel extends JPanel 
{ 
    private final int numLines; 
    private final double colorSplit; 
    private final Random generator; 

    //----------------------------------------------------------------------- 
    // Sets up the drawing panel. 
    //----------------------------------------------------------------------- 

    public LinesPanel(int numLines, double colorSplit) 
    { 
     this.numLines = numLines; 
     this.colorSplit = colorSplit; 

     this.generator = new Random(); 

     setBackground (Color.black); 
    } 

    //----------------------------------------------------------------------- 
    // Paints evenly spaced Horizontal lines of random length. 
    // lines that are half the width are highlighted with a re color. 
    //----------------------------------------------------------------------- 

    public void paintComponent (Graphics page) 
    { 
     // Clear the off-screen bitmap and set the background. 
     super.paintComponent(page); 

     final int pageWidth = getWidth(); 
     final int pageHeight = getHeight(); 

     // Calculate the line spacing and center vertically. 
     final int lineSpace = (pageHeight - 1)/(numLines - 1); 
     final int margin = (pageHeight - (lineSpace * (numLines - 1)))/2; 

      final int splitWidth = (int)(pageWidth * colorSplit); 

     for (int y = margin; y < pageHeight; y += lineSpace) 
     { 
      int width = generator.nextInt(pageWidth) + 1; 
      page.setColor(width <= splitWidth ? Color.red : Color.blue); 
      page.drawLine(0, y, width, y); 
     } 
    } 
} 
相關問題