2013-12-14 2 views
-5

У меня есть этот код// ЧЕРЕЗ ЗДЕСЬ .... Ну, что я здесь рисую?

public void paint(Graphics g) { super.paint(g); // DRAW HERE } 

Но, что же я на самом деле сделать там?

Я делаю игру в Java, вот мой исходный код GAME.JAVA

package com.cmnatic.cmnatic; 


import java.awt.Canvas; 
import java.awt.Color; 

import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.image.BufferStrategy; 
import java.awt.image.BufferedImage; 
import java.awt.image.DataBufferInt; 

import javax.swing.JFrame; 

import com.cmnatic.cmnatic.graphics.Screen; 


public class Game extends Canvas implements Runnable { 



private static final long serialVersionUID = 1L; 
    private static final Screen Screen = null; 
    public static int width = 300; 
    public static int height = width/16 * 9; // 168 
    public static int scale = 3; 

    private Thread thread; 
    private JFrame frame; 
    private boolean running = false; 

    public void paint(Graphics g) { super.paint(g); } // 1280x720 } 

    private Screen screen; 

    private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
    private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData(); 

    public Game() { 
     Dimension size = new Dimension(width * scale, height * scale); 
     setPreferredSize(size); 

     screen = new Screen(width, height); 

     frame = new JFrame(); 
    } 

    public synchronized void start() { 
     running = true; 
     thread = new Thread(this, "Display"); 
     thread.start(); 
    } 

    public synchronized void stop() { 
     running = false; 
     try { 
      thread.join(); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 

    public void run() { 
     while (running == true) { 
      update(); 
      render(); 
     } 
    } 

    public void update() { 

    } 

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

     screen.clear(); 

     screen.render(); 

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

     Graphics g = bs.getDrawGraphics(); 
     g.setColor(Color.BLACK); 
     g.fillRect(0, 0, getWidth(), getHeight()); 
     g.drawImage(image, 0, 0, getWidth(), getHeight(), null); 
     g.dispose(); 
     bs.show(); 
    } 

    public static void main(String[] args) { 
     Game game = new Game(); 
     game.frame.setResizable(false); 
     game.frame.setTitle("The Last Hit"); 
     game.frame.add(game); 
     game.frame.pack(); 
     game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     game.frame.setLocationRelativeTo(null); 
     game.frame.setVisible(true); 

     game.start(); 
    } 

    public static Screen getScreen() { 
     return Screen; 
    } 

} 

Кроме того, здесь мой Screen.java

package com.cmnatic.cmnatic.graphics; 

public class Screen { 


    private int width, height; 
    public int[] pixels; 

    int xtime = 0, ytime = 50; 
    int counter = 0; 

    public Screen(int width, int height) { 
     this.width = width; 
     this.height = height; 
     pixels = new int[width * height]; // 0 - 50,399 = 50,400 
    } 

    public void clear() { 
     for (int i = 0; i < pixels.length; i++) { 
      pixels[i] = 0; 
     } 
    } 

    public void render() { 
     counter++; 
     if (counter % 100 == 0) { 
      xtime--; 
     if (counter % 100 == 0) { 
      ytime--; 

     for (int y = 0; y < height; y++) { 
      if (ytime >= height) break; 
      for (int x = 0; x < width; x++) { 
       if (xtime >= width) break; 
       pixels[xtime + ytime * width] = 0xff00ff; 
      } 


     } 
    } 
} 
} 
} 

Так как вопрос спрашивает, что я ставлю в // DRAW HERE

Thanks, CMNatic ​​

+4

* "// ЧЕРЕЗ ЗДЕСЬ ... Ну, что я здесь 'рисую?" * Нарисуй пони ... Нет! Клоун! –

+0

Но я вижу '1280x720', позвольте мне перепроверить его '921600'. –

+2

Взгляните на это, пожалуйста: http://sscce.org/ –

ответ

1

Первый взгляд на ваш метод render, вы видите эти три линии -

g.setColor(Color.BLACK); 
g.fillRect(0, 0, getWidth(), getHeight()); 
g.drawImage(image, 0, 0, getWidth(), getHeight(), null); 

Я хотел бы попробовать переместить их в краску, как так -

public void paint(Graphics g) { 
    super.paint(g); 
    g.setColor(Color.BLACK); 
    g.fillRect(0, 0, getWidth(), getHeight()); 
    // Java GUI components implement ImageObserver (_need_ this). 
    g.drawImage(image, 0, 0, getWidth(), getHeight(), this); 
} 
+0

'g.drawImage (изображение, 0, 0, getWidth(), getHeight(), null);' должно быть 'g.drawImage (image, 0 , 0, getWidth(), getHeight(), this); '. Компоненты GUI Java ** реализуют ** 'ImageObserver' .. –

Смежные вопросы