2016-07-06 3 views
-1

아래 코드를 사용하면 어떤 그래픽을 사용해도 그래픽이 렌더링되지 않습니다.자바가 프레임에 그래픽을 렌더링하지 않습니다.

package local.ryan.grid; 

import java.awt.BufferCapabilities; 
import java.awt.Canvas; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.image.BufferStrategy; 

import javax.swing.JFrame; 

public class Game implements Runnable { 

    public boolean isRunning = false; 

    private long lastTime; 
    private double fps; 
    public int gamestage = 0; 
    public Frame frame = new Frame(2, 300, 300, "Ok"); 

    public static void main(String[] args) throws InterruptedException { 

     Thread game = new Thread(new Game()); 
     game.start(); 

    } 

    public void run() { 

     start(); 

     while(isRunning) { 

      lastTime = System.nanoTime(); 

      try { 

       // Limit Each Frame, to 60 fps. 
       // Prevents performance issues with multiple objects. 

      Thread.sleep(1000/60); // 1000 miliseconds (1 second)/60 frames-per-second ~ 17 ms. 



      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 

      render(); 

      fps = 1000000000D/(System.nanoTime() - lastTime); //one second(nano) divided by amount of time it takes for one frame to finish 
      lastTime = System.nanoTime(); 

      // Change to integer, to remove decimals. 
      System.out.println("FPS: " + (int) fps + "."); 

     } 

    } 

    public void render(){ 

      BufferStrategy bs = frame.getBufferStrategy(); 

      if (bs== null){ 

      frame.createBufferStrategy(3); 
      return; 

      } 

      Graphics2D g = (Graphics2D)bs.getDrawGraphics(); 

      //Draws a background and a line for testing 
      g.setColor(Color.GRAY); 
      g.drawRect(0, 0, 500, 500); 
      g.setColor(Color.BLACK); 
      g.drawLine(50, 50, 200, 50); 

      //Displays the graphics to the frame 
      frame.update(g); 
      g.dispose(); 
      bs.show(); 

     } 

    public void start() { 

     if(isRunning) 
      return; 

     isRunning = true; 


    } 

    public void stop() { 

     if(!isRunning) 
      return; 

     isRunning = false; 


    } 

} 

나는 또한 프레임을 만들 Frame라는 클래스가 : 아마

package local.ryan.grid; 

import javax.swing.JFrame; 

@SuppressWarnings("serial") 
public class Frame extends JFrame { 

    public Frame(int scale, int x, int y, String title) { 

     setTitle(title); 
     setSize(x, y); 
     setResizable(false); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     setVisible(true); 

    } 

    public JFrame getFrame() { 

     return this; 

    } 

} 
+0

일부 코드가 잘못 작성되었거나 비효율적 인 경우 귀찮게하지 마십시오. 실제 문제에 대해 자제하고 응답하십시오. 고맙습니다! – Ryan

+0

상단의 '프레임'을 JFrame으로 변경했습니다. – Ryan

+0

'frame.setVisible (true); '을 호출하지 마십시오. – SnakeDoc

답변

1
package local.ryan.grid; 

import java.awt.BufferCapabilities; 
import java.awt.Canvas; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Shape; 
import java.awt.geom.Ellipse2D; 
import java.awt.image.BufferStrategy; 

import javax.swing.JFrame; 

public class Game implements Runnable { 

    public boolean isRunning = false; 

    private long lastTime; 
    private double fps; 
    public int gamestage = 0; 
    public JFrame frame = new Frame(2, 300, 300, "Ok"); 

    public static void main(String[] args) throws InterruptedException { 

     Thread game = new Thread(new Game()); 
     game.start(); 

    } 

    public void run() { 

     start(); 

     while(isRunning) { 

      lastTime = System.nanoTime(); 

      try { 

       // Limit Each Frame, to 60 fps. 
       // Prevents performance issues with multiple objects. 

      Thread.sleep(1000/60); // 1000 miliseconds (1 second)/60 frames-per-second ~ 17 ms. 



      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 

      render(frame.getGraphics()); 

      fps = 1000000000D/(System.nanoTime() - lastTime); //one second(nano) divided by amount of time it takes for one frame to finish 
      lastTime = System.nanoTime(); 

      // Change to integer, to remove decimals. 
      System.out.println("FPS: " + (int) fps + "."); 

     } 

    } 

    public void render(Graphics g) {  

     Shape circle = new Ellipse2D.Float(10,10,100f,100f); 
     Graphics2D ga = (Graphics2D)g; 
     ga.draw(circle); 

    } 

    public void start() { 

     if(isRunning) 
      return; 

     isRunning = true; 


    } 

    public void stop() { 

     if(!isRunning) 
      return; 

     isRunning = false; 


    } 

} 

하지 그것을 할 수있는 최선의 방법을하지만 일!

관련 문제