2012-11-21 13 views
6

Voglio mostrare un'immagine GameOver in un gioco di pacman dopo che le vite sono finite. Ma chiamo paintGameOverScreen (Graphics g) e quindi ho bisogno di inizializzare g. C'è un altro modo di fare questo?Come inizializzare la grafica g?

Questa è la mia vita di classe

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.image.BufferedImage; 

import javax.imageio.ImageIO; 
import javax.swing.ImageIcon; 


public class Lives{ 

private int lives; 


public Lives() { 
    lives = 1; 
} 

public void removeLife() { 

     lives--; 
     if(lives==0){ 
      System.out.println("END GAME"); 
      paintGameOverScreen(g); 
      System.exit(0); 
     } 
} 

public void paintGameOverScreen(Graphics g) { 


      ImageIcon i = new ImageIcon("src\image"); 
      Image image = i.getImage(); 
      int x = 0; 
      int y = 0; 
      g.drawImage(image, x, y, 100,100,null); 
} 


public void paint(Graphics g) { 
    g.setColor(Color.WHITE); 
    g.fillRect(5*20, 25*20, 100, 30); 
    g.setColor(Color.BLACK); 
    String result = "Lives: " + lives; 
    g.drawString(result, 6*20, 26*20); 
} 
} 
+0

Cosa intendi per "inizializzare g"? La variabile g dovrebbe essere già inizializzata da AWT quando entra nel metodo paint. – ekolis

+0

Invece mantieni lo stato del gioco e dipingi secondo esso –

+0

Se vuoi usare un buffer fuori dallo schermo (come BufferedImage), dipingilo e poi sincronizzalo con l'interfaccia utente quando sei pronto – MadProgrammer

risposta

6

Non si chiama mai paint() o paintComponent() da soli, si passa sempre attraverso repaint() che si occuperà di impostare l'appropriato Graphics

Proprio per mostrare ciò che @mKorbel si riferisce:

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.net.MalformedURLException; 
import java.net.URL; 

import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 
import javax.swing.Timer; 

public class Lives extends JPanel { 
    private int lives; 
    private ImageIcon gameOverImage; 

    public Lives() { 
     try { 
      gameOverImage = new ImageIcon(new URL("http://imgup.motion-twin.com/dinorpg/0/f/77acf80b_989624.jpg")); 
     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     lives = 5; 
    } 

    public void removeLife() { 
     if (lives > 0) { 
      lives--; 
      System.out.println("Left lives: " + lives); 
      repaint(); 
     } 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     if (lives > 0) { 
      System.out.println("Still have " + lives + " lives"); 
      g.setColor(Color.WHITE); 
      g.fillRect(5 * 20, 25 * 20, 100, 30); 
      g.setColor(Color.BLACK); 
      String result = "Lives: " + lives; 
      g.drawString(result, 6 * 20, 26 * 20); 
     } else if (gameOverImage != null) { 
      System.out.println("Game over"); 
      int x = (getWidth() - gameOverImage.getIconWidth())/2; 
      int y = (getHeight() - gameOverImage.getIconHeight())/2; 
      g.drawImage(gameOverImage.getImage(), x, y, gameOverImage.getIconWidth(), gameOverImage.getIconHeight(), this); 
     } 
    } 

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

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       JFrame frame = new JFrame(Lives.class.getSimpleName()); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       final Lives lives = new Lives(); 
       frame.add(lives); 
       frame.pack(); 
       frame.setVisible(true); 
       // Dummy timer that reduces the lives every second. For demo purposes only of course 
       Timer t = new Timer(1000, new ActionListener() { 

        @Override 
        public void actionPerformed(ActionEvent e) { 
         lives.removeLife(); 
        } 
       }); 
       t.start(); 
      } 
     }); 
    } 
} 
5
  • per pubblica void paint(Graphics g) {C'è mancato contenitore,

  • JPanel (in alcuni casi JComponent) potrebbe essere contenitore per oggi Java

  • utilizzare paintComponent anziché paint()

  • all'interno paintComponent è possibile contrassegnare per paintGameOverScreen, poi ci dipingere solo BufferedImage

  • preparare tutti Objects prima, come local variable, non caricare FileIO (immagini di carico) all'interno paint(), paintComponent()

+3

+1 alcuni buoni consigli signore :). Anche OP non deve dimenticare 'super.paintComponent (..)' chiama –

+1

Per riferimento, "I programmi Swing dovrebbero sovrascrivere' paintComponent() 'invece di sovrascrivere' paint() '." - [* Pittura in AWT e Swing: The Paint metodi *] (http://www.oracle.com/technetwork/java/painting-140037.html#callbacks). – trashgod

1

Ecco come ho fatto:

import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 

public class InitializeGraphics 
    { 
     static BufferedImage buffer = null; 
     static int height = 10; 
     static int width = 10; 
     static Graphics2D g2; 

     public InitializeGraphics() { 
       buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE); 
       g2 = buffer.createGraphics(); 
       g2.fillOval(2, 2, 2, 2); 
       g2.dispose(); 
     } 
     protected void paintComponent(Graphics g) { 
       int x = 0; 
       int y = 0; 
       g.drawImage(buffer, x, y, width, height, null); 
     } 
     public Graphics2D getGraphics(){ 
       return g2; 
     } 
    } 

Poi da qualche parte: InitializeGraphics istanza = new InitializeGraphics(); Graphics2D gG2 = instance.getGraphics();