Java applet中的多个paint()方法?

时间:2016-03-25 17:23:21

标签: java applet paint collision repaint

所以我试图生成玩家必须避免的随机矩形。我的碰撞方法是使用一个随机生成的矩形。我想画10个左右,然后我会添加一个终点线和一个计时器。 现在,我理解我的问题,但我不知道如何解决它。通过将x或y坐标改变10然后重新绘制圆圈来执行球/球员的移动。我目前在相同的绘制方法中使用矩形,因此每次玩家移动时,矩形都会重新生成。我希望他们在最初的随机生成后留在同一个地方。虽然我真的不知道如何做到这一点...... 此外,如果我可以让矩形停留在同一个地方,我的碰撞方法是否仍然适用于多个矩形?或者我也需要修改它?

我只是要发布整个程序的代码,因为我不确定哪些部分需要修改。

import java.awt.*;
import java.awt.Rectangle;
import java.awt.Shape;

import javafx.scene.shape.*;
import java.awt.event.*;
import java.util.Random;
import java.awt.geom.Area;
import javax.swing.JOptionPane;

import java.applet.Applet;

public class Main extends Applet
implements ActionListener{
boolean end = false;
private Rectangle rectangle;
//creates buttons to move player
    private Button run = new Button("Run");
    private Button jump = new Button("Jump");
private Button fall = new Button("Fall");
//creates player and obstacles
private Circle player = new Circle(110,110,20);
private makeRect block = new makeRect(150, 120, 30, 10);
//initiates the buttons with actionListener
public void init(){
    add(run);
    add(jump);
    add(fall);
    run.addActionListener(this);
    jump.addActionListener(this);
    fall.addActionListener(this);
}
//draws the player and blocks on the screen 

    public void paint(Graphics g){
    for(int numBlocks = 0; numBlocks<11; numBlocks++){
    block.draw(g);}
    player.draw(g);
}
//if methods to be control movement 
public void actionPerformed(ActionEvent e){


        if(e.getSource() instanceof Button){
        if(e.getSource() == run)
            player.horiz(10);
        else if (e.getSource()== jump){
            player.vert(-10);
            }
        else if (e.getSource()== fall){
            player.down(10);
            }
        repaint();
        collision();
    }

}
public void collision(){
    if(player.getBounds().intersects(block.getBounds())){
        JOptionPane.showMessageDialog(this, "Game Over", "Game Over", JOptionPane.YES_NO_OPTION);
        System.exit(ABORT);
        end = true;
    }

}


class Circle{
private final Color theColor = Color.BLUE;
private int radius;
private int x,y;
public Circle(){
    x = 110; y = 110;
    radius = 20;
}

public Circle(int x0, int y0, int rad){
    x = x0; y = y0; radius = rad;
}
public void draw(Graphics g){
    g.fillOval(x - radius,  y-radius,  2*radius,  2*radius);
    g.setColor(theColor);
}
public void horiz(int val){

    for(int c = 0; c<val+1; c++){
        x++;
        repaint();}
}
public void vert(int val){
    y += val;
}
public void down(int val){
    y += val;
}
public Rectangle getBounds(){
    return new Rectangle(x-radius, y-radius, 2*radius, 2*radius);
}


}
class makeRect{
private int Xmax = 250;
private int Xmin = 140;
private int Wmax = 50;
private int Hmax = 25;
private int Wmin = 10;
private int Hmin = 5;
Random rand = new Random();
private int randx;
private int randh;

private int x, y, width, height;
public makeRect(){
    x = 150; y = 120;
    width = 30; height = 10;
}


public makeRect(int x0, int y0, int w0, int h0){
    x = x0; y = y0; width = w0; height = h0;
}
public void draw(Graphics g) {

int randx = rand.nextInt((Xmax-Xmin)+1)+Xmin;
int randh = rand.nextInt((Hmax-Hmin)+1)+Hmin;
int randw = rand.nextInt((Wmax-Wmin)+1)+Wmin;
g.drawRect(randx, 110+randh, randh, randw);

}
public Rectangle getBounds(){
return new Rectangle(randx, 110+randh, 30, 10);
}
}
}

谢谢!

1 个答案:

答案 0 :(得分:0)

为此,您需要在初始化时首先构造10个rects(考虑使用数组),每个都有随机的postition。我的意思是,在构建rects时会发生postition随机化,而不是在绘制它时。

每次调用paint()时,你所拥有的是在同一个地方绘制10次相同的矩形。