2017-03-20 2 views
-6

Im folgenden ist der Code für Conways Spiel des Lebens, obwohl das Programm perfekt läuftFarbe ändern in JAVA GUI (Eclipse) für Game of Life

Die GUI druckt Blöcke in Schwarz und Weiß. Wie ändere ich den Code für sie stattdessen Red (tot) und Green (Lebend) auszudrucken

public class GameofLife { 
    private static final long serialVersionUID = 1L; 

    public static int frameSize = 360; 
    public static String title = "Game Of Life"; 

    public Random r = new Random(); 
    public int gridSize = 100; 
    public double generationSpeed = 20.0; 

    public BufferedImage image; 
    public int[] pixels; 

    public boolean[] cGrid; 
    public boolean[] pGrid; 

    public GameofLife() { 
     Dimension d = new Dimension(frameSize, frameSize); 
     setMinimumSize(d); 
     setMaximumSize(d); 
     setPreferredSize(d); 

     image = new BufferedImage(gridSize, gridSize, BufferedImage.TYPE_INT_RGB); 
     pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); 
    } 

    public void start() { 
     cGrid = new boolean[pixels.length]; 
     pGrid = new boolean[pixels.length]; 

     for (int i = 0; i < cGrid.length; i++) 
     cGrid[i] = r.nextInt(100)/100.0 > 0.8 ? true: false; 

     new Thread(this).start(); 
    } 

    public void run() { 
     double frameCut = 1000000000.0/generationSpeed; 

     long currentTime = System.nanoTime();; 
     long previouseTime = currentTime; 
     long passedTime = 0; 

     double unprocessedTime = 0.0; 

     long frameCounter = System.currentTimeMillis(); 
     int generations = 1; 

     while (true) { 
      previouseTime = currentTime; 
      currentTime = System.nanoTime(); 
      passedTime = currentTime - previouseTime; 

      unprocessedTime += passedTime; 

      if (unprocessedTime > frameCut) { 
       unprocessedTime = 0; 
       update(); 
       generations++; 
      } 

      if (System.currentTimeMillis() - frameCounter >= 1000) { 
       frameCounter = System.currentTimeMillis(); 

      } 

      render(); 
     } 
    } 

    public void update() { 
     for (int i = 0; i < pixels.length; i++) 
     pGrid[i] = cGrid[i]; 

     for (int y = 0; y < gridSize; y++) { 
      for (int x = 0; x < gridSize; x++) { 
       int res = 0; 

       int xx0 = x - 1; 
       int yy0 = y - 1; 
       int xx1 = x + 1; 
       int yy1 = y + 1; 

       if (x != 0) res += pGrid[xx0 + gridSize * y] ? 1 : 0; 
       if (y != 0) res += pGrid[x + gridSize * yy0] ? 1 : 0; 
       if (x != gridSize - 1) res += pGrid[xx1 + gridSize * y] ? 1 : 0; 
       if (y != gridSize - 1) res += pGrid[x + gridSize * yy1] ? 1 : 0; 
       if (x != 0 && y != 0) res += pGrid[xx0 + gridSize * yy0] ? 1 : 0; 
       if (x != 0 && y != gridSize - 1) res += pGrid[xx0 + gridSize * yy1] ? 1 : 0; 
       if (x != gridSize - 1 && y != 0) res += pGrid[xx1 + gridSize * yy0] ? 1 : 0; 
       if (x != gridSize - 1 && y != gridSize - 1) res += pGrid[xx1 + gridSize * yy1] ? 1 : 0; 

       if (! (pGrid[x + gridSize * y] && (res == 3 || res == 2))) cGrid[x + gridSize * y] = false; 
       if (!pGrid[x + gridSize * y] && res == 3) cGrid[x + gridSize * y] = true; 
      } 
     } 
    } 

    public void render() { 
     BufferStrategy bs = getBufferStrategy(); 
     if (bs == null) { 
      createBufferStrategy(3); 
      return; 
     } 

     Graphics g = bs.getDrawGraphics(); 

     for (int i = 0; i < pixels.length; i++) 
     pixels[i] = 0; 

     for (int i = 0; i < pixels.length; i++) 
     pixels[i] = cGrid[i] ? 0xffffff: 0; 

     g.drawImage(image, 0, 0, frameSize, frameSize, null); 
     g.dispose(); 
     bs.show(); 
    } 

    public static void main(String[] args) { 
     JFrame frame = new JFrame(); 
     frame.setTitle(title); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setResizable(false); 
     frame.setAlwaysOnTop(true); 

     GameofLife gol = new GameofLife(); 
     frame.add(gol); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 

     frame.setVisible(true); 

     gol.start(); 
    } 
} 
+0

Können Sie, oder besser gesagt, haben Sie versucht, die Farben zu erkennen? – Justas

+0

Wessen Code ist das? Wo hast du es gefunden? –

+1

Ich bin mir nicht sicher, aber ich habe Abe Lincolns Kopf nur mit einigen dieser Variablen angemacht. –

Antwort

0

Die Farbe wird in diesem Codeabschnitt gesetzt

for (int i = 0; i < pixels.length; i++) 
    pixels[i] = cGrid[i] ? 0xffffff : 0; 

Wenn Sie verschiedene off festlegen möchten/auf Farben, können Sie eine einfache Anpassung machen

int off = Color.red.getRGB(); 
int on = Color.green.getRGB(); 

for (int i = 0; i < pixels.length; i++) 
    pixels[i] = cGrid[i] ? on : off;