【问题标题】:Multiple paint() methods in Java applet?Java小程序中的多个paint()方法?
【发布时间】:2016-03-25 17:23:21
【问题描述】:

所以我试图生成玩家必须避免的随机矩形。我的碰撞方法是使用一个随机生成的矩形。我想画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);
}
}
}

谢谢!

【问题讨论】:

    标签: java applet paint collision repaint


    【解决方案1】:

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

    每次调用paint() 时,您都会在不同的地方绘制10 次相同的矩形。

    【讨论】:

      猜你喜欢
      • 2016-06-02
      • 1970-01-01
      • 2011-12-25
      • 2017-03-17
      • 2010-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多