简体   繁体   English

Java小程序中的多个paint()方法?

[英]Multiple paint() methods in Java applet?

So I am trying to generate random rectangles that the player must avoid.所以我试图生成玩家必须避免的随机矩形。 My collision method was working with a single, randomly generated rectangle.我的碰撞方法是使用单个随机生成的矩形。 I want to draw 10 or so of these and then I will add a finish line and a timer.我想画 10 个左右,然后我将添加一个终点线和一个计时器。 Right now, I understand my problem, but I am not sure how to fix it.现在,我明白我的问题,但我不知道如何解决它。 The ball/player's movement is executed by changing the x or y coordinates by 10 and then repainting the circle.球/玩家的移动是通过将 x 或 y 坐标更改 10 然后重新绘制圆圈来执行的。 I currently have the rectangles in the same paint method, so each time the player moves the rectangles are regenerated.我目前在相同的绘制方法中有矩形,所以每次玩家移动矩形都会重新生成。 I would like them to stay in the same place after the initial random generation.我希望它们在初始随机生成后留在同一个地方。 I don't really know how to do this though... Also, if I can get the rectangles to stay in the same place, will my collision method still work with multiple rectangles?不过,我真的不知道该怎么做……另外,如果我能让矩形保持在同一个位置,我的碰撞方法是否仍然适用于多个矩形? or would I need to revise that as well?还是我也需要修改?

I am just going to post the whole program's code because I'm not sure which parts will need to be revised.我只是要发布整个程序的代码,因为我不确定哪些部分需要修改。

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);
}
}
}

Thanks!谢谢!

For that you will need to construct 10 rects (consider using array) first upon initialization, with random postition for each.为此,您需要在初始化时首先构造 10 个矩形(考虑使用数组),每个矩形都有随机位置。 I mean, postition randomization occurs when the rects are constructed, not when it's drawn.我的意思是,位置随机化发生在构造矩形时,而不是绘制时。

What you have there is a same rectangle drawn 10 times at different places each time the paint() gets called.每次调用paint()时,都会在不同的地方绘制10次相同的矩形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM