2013-04-03 42 views
1

我花了最後一個小時仔細閱讀互聯網上如何創建一個超級簡單的顯示在我的程序。這裏的情況:最簡單的方法來顯示一個帶點的窗口

1.)我有一個循環運行任意次數。每次迭代,它會產生一個「城市」,即隨機生成的x座標和y座標(兩個整數)。

2.)我想創建可能的SIMPLEST顯示。沒有什麼花哨。也就是說,理想情況下,一個尺寸爲600x600的窗口爲每個迭代的for循環繪製一個黑點。

這是我到目前爲止有:

f.setPreferredSize(new Dimension(600, 600)); 
    f.pack(); 
    f.setVisible(true); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    for (int i = 0; i <= nodeArray.length-1; i++) 
    { 
     nodeArray[i] = new Node(); 
     nodeArray[i].x = 0 + (600 - 0) * r.nextDouble(); 
     nodeArray[i].y = 0 + (600 - 0) * r.nextDouble(); 

     //DRAW DOT HERE? 
    } 

對於這個for循環,我想提請每個迭代,在這個窗口中,黑點與x座標(INT)nodeArray [I] .x和y座標(int)nodeArray [i] .y

我真的很感謝任何幫助。這是一個有點高級的算法課程,我有點尷尬,我似乎無法弄清楚圖形在Java中如何工作...

回答

2

答案取決於你想達到什麼,但基本上,你需要一些方法將結果繪製到屏幕上。

如果數據不經常更改,最好使用後備緩衝區,因爲它對渲染器更快,而不必每次循環訪問數據集。

退房Performing Custom Painting

動畫

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Dotty { 

    public static void main(String[] args) { 
     new Dotty(); 
    } 

    public Dotty() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private int count = 0; 
     private int dotCount = 1000; 
     private BufferedImage background; 

     public TestPane() { 
      background = new BufferedImage(600, 600, BufferedImage.TYPE_INT_ARGB); 

      Timer timer; 
      timer = new Timer(40, new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        count++; 
        if (count < dotCount) { 
         int x = (int) Math.round((Math.random() * 600)); 
         int y = (int) Math.round((Math.random() * 600)); 
         Graphics2D g2d = background.createGraphics(); 
         g2d.setColor(Color.BLACK); 
         g2d.drawRect(x, y, 1, 1); 
         g2d.dispose(); 
         repaint(); 
        } else { 
         ((Timer) e.getSource()).stop(); 
        } 
       } 
      }); 
      timer.setRepeats(true); 
      timer.setCoalesce(true); 
      timer.start(); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(600, 600); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      int x = (getWidth() - background.getWidth())/2; 
      int y = (getHeight() - background.getHeight())/2; 
      g2d.drawImage(background, x, y, this); 
      g2d.dispose(); 
     } 
    } 
} 

直渲染

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Dotty { 

    public static void main(String[] args) { 
     new Dotty(); 
    } 

    public Dotty() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private int count = 0; 
     private int dotCount = 1000; 
     private BufferedImage background; 

     public TestPane() { 
      background = new BufferedImage(600, 600, BufferedImage.TYPE_INT_ARGB); 

      for (int count = 0; count < dotCount; count++) { 
       int x = (int) Math.round((Math.random() * 600)); 
       int y = (int) Math.round((Math.random() * 600)); 
       Graphics2D g2d = background.createGraphics(); 
       g2d.setColor(Color.BLACK); 
       g2d.drawRect(x, y, 1, 1); 
       g2d.dispose(); 
       repaint(); 
      } 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(600, 600); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      int x = (getWidth() - background.getWidth())/2; 
      int y = (getHeight() - background.getHeight())/2; 
      g2d.drawImage(background, x, y, this); 
      g2d.dispose(); 
     } 
    } 
} 
相關問題