Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java小程序定时器和跳转_Java_Swing_Timer_Applet_Awt - Fatal编程技术网

Java小程序定时器和跳转

Java小程序定时器和跳转,java,swing,timer,applet,awt,Java,Swing,Timer,Applet,Awt,好的,我需要很多帮助,所以任何人的建议都将非常感谢!我正在写一个跑步/跳跃游戏,玩家/球必须避开障碍物才能尽快到达终点线。现在,我的两个主要问题是跳跃动作和创建计时器。当我的玩家跳跃时,它会先上升,然后下降。 我在Circle类中包含了以下代码: public void horiz(int val){ for(int c = 0; c<val+1; c++){ x++; repaint();} } public void vert(int val)

好的,我需要很多帮助,所以任何人的建议都将非常感谢!我正在写一个跑步/跳跃游戏,玩家/球必须避开障碍物才能尽快到达终点线。现在,我的两个主要问题是跳跃动作和创建计时器。当我的玩家跳跃时,它会先上升,然后下降。 我在Circle类中包含了以下代码:

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 void actionPerformed(ActionEvent e){
    if(e.getSource() instanceof Button){
        if(e.getSource() == run)
            player.horiz(10);
        else if (e.getSource()== jump){
            player.vert(-10);
            repaint();
            player.down(10);
            }
        repaint();
        collision();
}
但我想我需要让它等几秒钟再下来。我读过很多关于swing、util和robot计时器的文章,但我不知道在这里应该实现哪些/如何实现它们

我的另一个问题是创建一个计时器,显示游戏开始后经过的时间。我认为这可以类似于跳转计时器来执行,但是,再一次,我不确定应该使用哪种类型的计时器。 如果我是对的,我不能使用swing,因为我的程序是用AWT编写的?请原谅这些糟糕的颜色,我还在想基本的。非常感谢您给我的任何帮助建议!:)

以下是完整的代码:

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.util.Timer;
import javax.swing.JOptionPane;
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;

import java.applet.Applet;

public class TryingAgain extends Applet
implements ActionListener{

//creates arrays of rect values
int[]xA = new int[10];
int[]yA = new int[10];
int[]widthA = new int[10];
int[]heightA = new int[10];

private Rectangle rectangle;
//creates buttons to move player
private Button run = new Button("Run");
private Button jump = new Button("Jump");
//creates player and obstacles
private Circle player = new Circle(110,110,20);
private makeRect block = new makeRect();
private finishLine line = new finishLine();
//initiates the buttons with actionListener
public void init(){
    this.setSize(new Dimension(1300,500));
    setBackground(Color.GREEN);
    add(run);
    add(jump);
    run.addActionListener(this);
    jump.addActionListener(this);
}
//draws the player and blocks on the screen 
public void paint(Graphics g){
    player.draw(g);
    block.draw(g);
    line.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);
            }
        repaint();
        collision();
}


    }
public void collision(){
    if(crashTest() == true){
        JOptionPane.showMessageDialog(this, "Game Over", "Game Over", JOptionPane.YES_NO_OPTION);
        System.exit(ABORT);
    }
    if(winTest() == true){
        JOptionPane.showMessageDialog(this, "You Finished!", "You Finished!", JOptionPane.PLAIN_MESSAGE);
    }

}
class Circle{

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.setColor(Color.CYAN);
    g.fillOval(x - radius,  y-radius,  2*radius,  2*radius);

}
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 = 150;
private int Xmin = 50;
private int Wmax = 50;
private int Hmax = 25;
private int Wmin = 10;
private int Hmin = 5;
Random rand = new Random();
int randx = rand.nextInt((Xmax-Xmin)+1)+Xmin;
int randh = rand.nextInt((Hmax-Hmin)+1)+Hmin;
int randw = rand.nextInt((Wmax-Wmin)+1)+Wmin;


//fills arrays
{for(int i = 0; i < 10; i++){
    Random rand = new Random();
    int randx = rand.nextInt((Xmax-Xmin)+1)+Xmin;
    int randh = rand.nextInt((Hmax-Hmin)+1)+Hmin;
    int randw = rand.nextInt((Wmax-Wmin)+1)+Wmin;
    if(i>0)
        xA[i] = (randx+xA[i-1]);
    else
        xA[i] = 160;
    yA[i] = randh+110;
    widthA[i] = randw;
    heightA[i] = randh;
}   
}
private int x, y, width, height;
public makeRect(){
x = 150; y = 120; width = 30; height = 10;
}
public void makeRect(int x0, int y0, int w0, int h0){
x = x0; y = y0; width = w0; height = h0;
}
public void draw(Graphics g) {
g.setColor(Color.ORANGE);
{for(int i = 0; i < 10; i++){
    g.fillRect(xA[i], yA[i], heightA[i], widthA[i]);
    }}
}
public Rectangle getBounds(){
return new Rectangle(randx, 110+randh, 30, 10);
}
}
class finishLine{
private int x, y, width, height;
public void finishLine(int x0, int y0, int w0, int h0){
    x = x0; y = y0; width = w0; height = h0;    
}
public void draw(Graphics g){
    g.setColor(Color.MAGENTA);
    g.fillRect(1200, 80, 30, 30);
}
}
public boolean crashTest(){
boolean end = false;
{for(int i = 0; i<10; i++){
if(player.getBounds().intersects(new Rectangle(xA[i], yA[i], heightA[i], widthA[i])))
    end = true;
}
return end;
}
}
public boolean winTest(){
boolean win = false;
if(player.getBounds().intersects(new Rectangle(1200, 80, 30, 30)))
    win = true;
return win;
}
}
import java.awt.*;
导入java.awt.Rectangle;
导入java.awt.Shape;
导入javafx.scene.shape.*;
导入java.awt.event.*;
导入java.util.Random;
导入java.util.Timer;
导入javax.swing.JOptionPane;
导入java.awt.Toolkit;
导入java.util.Timer;
导入java.util.TimerTask;
导入java.applet.applet;
公共类尝试再次扩展小程序
实现ActionListener{
//创建rect值的数组
int[]xA=新的int[10];
int[]yA=新的int[10];
int[]宽度a=新的int[10];
int[]heightA=新int[10];
私有矩形;
//创建按钮来移动播放器
专用按钮运行=新按钮(“运行”);
私有按钮跳转=新按钮(“跳转”);
//制造玩家和障碍
私人圈子玩家=新圈子(110110,20);
private makeRect block=new makeRect();
私有finishLine line=新finishLine();
//使用actionListener启动按钮
公共void init(){
此.设置尺寸(新尺寸(1300500));
挫折背景(颜色:绿色);
添加(运行);
加(跳);
run.addActionListener(this);
jump.addActionListener(this);
}
//在屏幕上绘制玩家和方块
公共空间涂料(图g){
球员平局(g);
方块图(g);
线图(g);
}
//要控制移动的if方法
已执行的公共无效操作(操作事件e){
if(如getSource()instanceof按钮){
如果(如getSource()==运行)
霍利兹(10);
else if(例如getSource()==跳转){
玩家。垂直(-10);
}
重新油漆();
碰撞();
}
}
公众利益冲突(){
if(crashTest()==true){
showMessageDialog(这个“游戏结束”、“游戏结束”,JOptionPane.YES\u NO\u选项);
系统退出(中止);
}
如果(winTest()==true){
showMessageDialog(这是“youfinished!”,“youfinished!”,JOptionPane.PLAIN_消息);
}
}
班级圈子{
私有整数半径;
私有整数x,y;
公众圈(){
x=110;y=110;
半径=20;
}
公共圈(int x0、int y0、int rad){
x=x0;y=y0;半径=rad;
}
公共空间绘制(图g){
g、 setColor(Color.CYAN);
g、 圆角(x-半径,y-半径,2*半径,2*半径);
}
公共空间水平线(内部值){
对于(int c=0;c0)
xA[i]=(randx+xA[i-1]);
其他的
xA[i]=160;
yA[i]=randh+110;
宽度a[i]=randw;
heightA[i]=randh;
}   
}
私有整数x,y,宽度,高度;
公共makeRect(){
x=150;y=120;宽度=30;高度=10;
}
公共void makeRect(int x0、int y0、int w0、int h0){
x=x0;y=y0;宽度=w0;高度=h0;
}
公共空间绘制(图g){
g、 setColor(颜色为橙色);
{for(int i=0;i<10;i++){
g、 fillRect(xA[i],yA[i],heightA[i],widthA[i]);
}}
}
公共矩形getBounds(){
返回新矩形(randx,110+randh,30,10);
}
}
班级终点线{
私有整数x,y,宽度,高度;
公共无效完成线(int x0、int y0、int w0、int h0){
x=x0;y=y0;宽度=w0;高度=h0;
}
公共空间绘制(图g){
g、 setColor(颜色为洋红色);
g、 fillRect(1200,80,30,30);
}
}
公共布尔crashTest(){
布尔结束=假;

{对于(int i=0;iFor,仅供参考:
Applet
在16年前已经过时,但是
JApplet
s已经正式成为一种过时的技术,有关更多详细信息,请参阅和
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.util.Timer;
import javax.swing.JOptionPane;
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;

import java.applet.Applet;

public class TryingAgain extends Applet
implements ActionListener{

//creates arrays of rect values
int[]xA = new int[10];
int[]yA = new int[10];
int[]widthA = new int[10];
int[]heightA = new int[10];

private Rectangle rectangle;
//creates buttons to move player
private Button run = new Button("Run");
private Button jump = new Button("Jump");
//creates player and obstacles
private Circle player = new Circle(110,110,20);
private makeRect block = new makeRect();
private finishLine line = new finishLine();
//initiates the buttons with actionListener
public void init(){
    this.setSize(new Dimension(1300,500));
    setBackground(Color.GREEN);
    add(run);
    add(jump);
    run.addActionListener(this);
    jump.addActionListener(this);
}
//draws the player and blocks on the screen 
public void paint(Graphics g){
    player.draw(g);
    block.draw(g);
    line.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);
            }
        repaint();
        collision();
}


    }
public void collision(){
    if(crashTest() == true){
        JOptionPane.showMessageDialog(this, "Game Over", "Game Over", JOptionPane.YES_NO_OPTION);
        System.exit(ABORT);
    }
    if(winTest() == true){
        JOptionPane.showMessageDialog(this, "You Finished!", "You Finished!", JOptionPane.PLAIN_MESSAGE);
    }

}
class Circle{

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.setColor(Color.CYAN);
    g.fillOval(x - radius,  y-radius,  2*radius,  2*radius);

}
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 = 150;
private int Xmin = 50;
private int Wmax = 50;
private int Hmax = 25;
private int Wmin = 10;
private int Hmin = 5;
Random rand = new Random();
int randx = rand.nextInt((Xmax-Xmin)+1)+Xmin;
int randh = rand.nextInt((Hmax-Hmin)+1)+Hmin;
int randw = rand.nextInt((Wmax-Wmin)+1)+Wmin;


//fills arrays
{for(int i = 0; i < 10; i++){
    Random rand = new Random();
    int randx = rand.nextInt((Xmax-Xmin)+1)+Xmin;
    int randh = rand.nextInt((Hmax-Hmin)+1)+Hmin;
    int randw = rand.nextInt((Wmax-Wmin)+1)+Wmin;
    if(i>0)
        xA[i] = (randx+xA[i-1]);
    else
        xA[i] = 160;
    yA[i] = randh+110;
    widthA[i] = randw;
    heightA[i] = randh;
}   
}
private int x, y, width, height;
public makeRect(){
x = 150; y = 120; width = 30; height = 10;
}
public void makeRect(int x0, int y0, int w0, int h0){
x = x0; y = y0; width = w0; height = h0;
}
public void draw(Graphics g) {
g.setColor(Color.ORANGE);
{for(int i = 0; i < 10; i++){
    g.fillRect(xA[i], yA[i], heightA[i], widthA[i]);
    }}
}
public Rectangle getBounds(){
return new Rectangle(randx, 110+randh, 30, 10);
}
}
class finishLine{
private int x, y, width, height;
public void finishLine(int x0, int y0, int w0, int h0){
    x = x0; y = y0; width = w0; height = h0;    
}
public void draw(Graphics g){
    g.setColor(Color.MAGENTA);
    g.fillRect(1200, 80, 30, 30);
}
}
public boolean crashTest(){
boolean end = false;
{for(int i = 0; i<10; i++){
if(player.getBounds().intersects(new Rectangle(xA[i], yA[i], heightA[i], widthA[i])))
    end = true;
}
return end;
}
}
public boolean winTest(){
boolean win = false;
if(player.getBounds().intersects(new Rectangle(1200, 80, 30, 30)))
    win = true;
return win;
}
}