2013-10-18 143 views
0

)請幫忙解決問題。我正在做一個小遊戲並將它畫成一枚硬幣)給玩家去收集它們。在JFRame中繪製圖片

這裏是如何收集的過程(即代碼):

if (x + w3 == 190) { 
       coins++; 
       mon = null; 
      } else { 
       g.drawImage(mon, 190 - x, 265, wm, hm, this); 
      } 

其中190 - 是其中硬幣

其中x + W3玩家

的座標即位置,當觸摸硬幣的座標消失時,但隨着此硬幣消失,等等,其實 - mon = null;

我該怎麼辦?真的爲每個硬幣做出你的照片?

在此先感謝。

UPD

全部代碼:

package GameTs; 

import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.GraphicsEnvironment; 
import java.awt.Image; 
import java.awt.Toolkit; 
import java.net.URL; 

import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 

public class Game extends JPanel { 

    private static final long serialVersionUID = 1L; 

    public static final String NAME = "DrakGo Game"; 
    public static final int WIDTH = 500; 
    public static final int HEIGHT = 460; 

    private static final int MAP_SIZE_X = 100; 
    private Image img; 
    private Image img2; 
    private Image mon; 
    private Image mon2; 
    private Image mon3; 
    private Image pl; 
    private Image mag; 
    private Image drak; 
    private Input input = new Input(this); 
    static JFrame frame; 
    static int x = 0; 
    int y = 0; 
    int xp = 110; 
    int yp = 180; 
    int coins = 0; 
    URL url4; 
    URL url7; 

    public Game() { 
     URL url2 = getClass().getResource("images/Grass.png"); 
     img = Toolkit.getDefaultToolkit().getImage(url2); 

     URL url = getClass().getResource("images/Cloud.png"); 
     img2 = Toolkit.getDefaultToolkit().getImage(url); 

     URL url3 = getClass().getResource("images/Mon.png");//THIS 
     mon = Toolkit.getDefaultToolkit().getImage(url3); 

     mon2 = Toolkit.getDefaultToolkit().getImage(url3);//THIS 
     mon3 = Toolkit.getDefaultToolkit().getImage(url3);//THIS 

     url4 = getClass().getResource("images/Right_Pic1.png"); 
     URL url5 = getClass().getResource("images/Mag.png"); 
     URL url6 = getClass().getResource("images/Dr.png"); 
     url7 = getClass().getResource("images/Right_Pic2.png"); 

     pl = Toolkit.getDefaultToolkit().getImage(url4); 
     mag = Toolkit.getDefaultToolkit().getImage(url5); 
     drak = Toolkit.getDefaultToolkit().getImage(url6); 

    } 

    public void paint(Graphics g) { 
     render(g); 
     repaint(); 
    } 

    private void move(Graphics g) { 
     /* 
     * if (input.left) { x--; if (x == -1) { x++; } 
     * 
     * } 
     */ 
     if (input.right) { 
      x++; 
      if (x == -1) { 
       x--; 
      } 
      if ((x % 2) == 0) { 
       pl = Toolkit.getDefaultToolkit().getImage(url7); 
      } else { 
       pl = Toolkit.getDefaultToolkit().getImage(url4); 
      } 
     } 

    } 

    public void render(Graphics g) { 
     super.paint(g); 
     move(g); 

     g.setColor(Color.BLUE); 
     g.fillRect(0, 0, 500, 360); 

     int w = 180; 
     int h = 180; 

     for (int i = 0; i < MAP_SIZE_X * 2; i++) { 
      g.drawImage(img, i * w - x, 260, (int) w, (int) h, this); 
     } 

     int w2 = 90; 
     int h2 = 50; 

     for (int c = 0; c < MAP_SIZE_X * 100; c += 200) { 
      g.drawImage(img2, 100 + c - x, 70, w2, h2, this); 

     } 

     GraphicsEnvironment ge = GraphicsEnvironment 
       .getLocalGraphicsEnvironment(); 
     Font[] allFonts = ge.getAllFonts(); 
     Font font = allFonts[4].deriveFont(30.0F); 
     g.setFont(font); 
     g.setColor(Color.DARK_GRAY); 
     g.drawString("DrakGo - Game", 10, 40); 
     g.drawString("X: " + x, 300, 40); 
     g.drawString("Coins: " + coins, 400, 40); 

     int w3 = 50; 
     int h3 = 50; 

     int wm = 30; 
     int hm = 30; 
     if (x + w3 == 190) { 
      coins++; 
      mon = null; 
     } else { 
      g.drawImage(mon, 190 - x, 265, wm, hm, this); 
     } 

      //THIS!!!!!!! 
     if (x + w3 == 350) { 
      coins++; 
      mon2 = null; 
     } else { 
      g.drawImage(mon2, 350 - x, 265, wm, hm, this); 
     } 

     if (x + w3 == 650) { 
      coins++; 
      mon3 = null; 
     } else { 
      g.drawImage(mon3, 650 - x, 265, wm, hm, this); 
     } 

     int w4 = 50; 
     int h4 = 50; 

     if (x + w3 == 3500) { 
      drak = null; 
     } else { 
      g.drawImage(drak, 3500 - x, 260, w4, h4, this); 
     } 

     g.drawImage(mag, 1000 - x, 260, w3, h3, this); 

     g.drawImage(pl, 0, 260, w3, h3, this); 

    } 

    public static void main(String[] args) { 
     Game game = new Game(); 
     game.setSize(WIDTH, HEIGHT); 
     frame = new JFrame(NAME); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setSize(WIDTH, HEIGHT); 
     frame.add(game); 
     frame.setVisible(true); 
     frame.setResizable(false); 

     frame.setLocationRelativeTo(null); 

    } 
} 
+2

這段代碼(雖然很難猜到上下文)看起來非常錯誤。您正在混合兩種截然不同的東西:您的模型(收集的硬幣數量)和繪圖(繪製到「圖形」對象)。這些屬於不同的方法(甚至經常是不同的類)。 'g'從哪裏來?向我們展示您的其他代碼。嘗試製作[SSCCE](http://sscce.org) –

回答

0

刪除此:

public void paint(Graphics g) { 
    render(g); 
    repaint(); 
} 

而是使用此:

@Override 
public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    render(g); 
} 

還有c焊割:

public void render(Graphics g) { 
    super.paint(g); 
    move(g); 
    // .. 

要:

public void render(Graphics g) { 
    move(g); 
    // .. 

這兩種方法,因爲他們,往往會導致無限循環。