[英]How to repaint over images in Java game
大家好,我在繪制與用戶相交的硬幣時遇到麻煩。 我要這段代碼要做的是,當用戶精靈與它繪制在硬幣上的十個硬幣圖像中的任何一個相交時,它不再出現在屏幕上(還需要添加某種計數器,以便當用戶收集時所有十枚硬幣將停止線程,並說“ You Win!”)。 我的問題是我將如何去做,因為我已經嘗試過在if語句中使用repaint(),並且它現在無法正確編譯。 非常感謝您提供有關如何在硬幣上繪畫的幫助,甚至可能添加某種計數器(認為for循環會派上用場)! 這是代碼:
public void paint(Graphics g)
{
g.clearRect(0, 0, this.getWidth(), this.getHeight());
one.paintComponent(g);
two.paintComponent(g);
three.paintComponent(g);
four.paintComponent(g);
five.paintComponent(g);
six.paintComponent(g);
seven.paintComponent(g);
eight.paintComponent(g);
nine.paintComponent(g);
ten.paintComponent(g);
monster.setLocation(r.nextInt(10) - 5 + monster.x, r.nextInt(10 - 5 + monster.y));
monster.paintComponent(g);
user.paintComponent(g);
if(user.intersects(one))
{
}
if(user.intersects(two))
{
}
if(user.intersects(three))
{
}
if(user.intersects(four))
{
}
if(user.intersects(five))
{
}
if(user.intersects(six))
{
}
if(user.intersects(seven))
{
}
if(user.intersects(eight))
{
}
if(user.intersects(nine))
{
}
if(user.intersects(ten))
{
}
if(user.intersects(monster))
{
g.setFont(new Font("Serif", Font.BOLD, 35));
g.drawString("YOU HAVE DIED, YOU LOSE!", 100, 100); //Prints this when you lose
thread.interrupt(); //Stopping the thread if you die
}
}
首先,您應該將硬幣放入列表中。 然后,如果有干擾,您只需刪除列表中的項目(硬幣)。 像這樣:
private List<Coin> coins;
public ClassConstructor() {
coins = new ArrayList();
coins.add(new Coin(type value,...)); //this ten times if every Coin is different from other
//or this if every Coin are same:
for (int i = 0; i < 10; i++) {
coins.add(new Coin(type value,...));
}
}
public void update() {
//whatever
for (int i = coins.size(); i >= 0; i--) {
if (user.intersects(coins.get(i))) {
coins.remove(i);
//...
}
}
}
public void paint(Graphics g) {
for (int i = coins.size(); i >= 0; i--) {
coins.get(i).paintComponent(g);
}
}
現在,您不應該使用thread.interrupt()結束線程。 如果該線程是您的游戲循環,則應發送結束循環的標志。 您的游戲循環線程:
@Override
public void run() {
while (running) { //boolean running
update();
paint(g);
}}
最后,您的更新將如下所示:
public void update() {
//whatever
for (int i = coins.size(); i >= 0; i--) {
if (user.intersects(coins.get(i))) {
coins.remove(i);
//...
}
}
if (user.intersects(monster)) {
youThread.setRunning(false);
try {
gameLoopThread.join(); //"wait" until thread ends
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
在我的腦海中,將while(true)
放入Run()
方法(您說過您正在使用線程)中,並在循環內調用repaint(),在循環結束時(在try / catch中)放入類似於//thread//.sleep(allotted time)
這將使線程停止設置的毫秒數。 在repaint()
的Javadoc中,您會看到它調用了最近的paint(Graphics g)
方法。 這樣,您可以從程序中的任何位置訪問paint()
方法。
我對硬幣的建議是使其成為自己的硬幣。 該類將類似於以下內容:
public class Coin{
int x;
int y;
public void Coin(int x, int y, Graphics g){
Graphics2D g2 = (Graphics2D) g;
Image img1 = (//File Location);
g2.drawImage(img1, x, y, //this);
//If you have trouble with the ImageObserver (this part) try //class name of the applet.this
this.x = x;
this.y = y;
g2.finalize();
}
public int getX(){
return x;
}
public int getY(){
return y;
}
}
之后,在繪畫方法中,您可以制作10個Coin
對象並使用在參數中實例化的g
。 然后,您可以制作一個Coin
對象數組,並手動添加您在paint
方法中創建的10個Coin
(是的,這很痛苦)(必須在paint中使它們通過以傳遞Graphics變量)
Coin[] coins = new Coin[10];
順便說一句,是如何制作數組。 制作硬幣陣列后,在paint
方法中創建一個for
循環。 它看起來應該像這樣:
for(int i = 0; i < coins.length; i++){
if(coins[i].getX == //user X && coins[i].getY == //user Y){
g.clearRect(coins[i].getX, coins[i].getY, //Coin radius, //Coin radius);
}
}
如果用戶的X和Y等於硬幣的X和Y,則將在硬幣所在的位置繪制一個清晰的矩形。
該代碼取自我當前的項目(正在運行),我嘗試將其調整為適合您的項目。
private void render () {
BufferStrategy bufferstrategy = getBufferStrategy ();
if (bufferstrategy == null) {
createBufferStrategy(3);
return;
}
Graphics g = bufferstrategy.getDrawGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getHeight());
paint (g);
g.dispose();
bufferstrategy.show();
}
從Thread
所在的run()中調用render()
。
讓我們分解一下,看看它是否可以幫助您。
BufferStrategy
為Graphics
。 paint (Graphics g)
而在我的情況下,它遍歷所有可繪制對象。 希望您從中得到一些啟示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.