Java 管道游戏-PipeObject类中的PipeObject不能应用于给定类型
我一直收到错误“PipeObject类中的PipeObject无法应用于给定类型” 我有一个对象类,我想由一个类添加到Java 管道游戏-PipeObject类中的PipeObject不能应用于给定类型,java,swing,list,graphics,timer,Java,Swing,List,Graphics,Timer,我一直收到错误“PipeObject类中的PipeObject无法应用于给定类型” 我有一个对象类,我想由一个类添加到JPanel,该类控制对象并将其3个实例绘制到屏幕上。它需要能够为对象类赋予不同的值 我的第三个类是main()。还有一个菜单屏幕,上面有一个播放按钮,在boolean playerIsReady=true之前,对象不应移动。管道对象仅应在事件侦听器将面板切换到游戏屏幕后移动 而且,我以前在游戏课上没能让计时器工作。也许这将有助于让它发挥作用。我不知道在哪里可以声明boolean
JPanel
,该类控制对象并将其3个实例绘制到屏幕上。它需要能够为对象类赋予不同的值
我的第三个类是main()。还有一个菜单屏幕,上面有一个播放按钮,在boolean playerIsReady=true
之前,对象不应移动。管道对象仅应在事件侦听器将面板切换到游戏屏幕后移动
而且,我以前在游戏
课上没能让计时器工作。也许这将有助于让它发挥作用。我不知道在哪里可以声明boolean playerisredy
,但我知道在用户单击播放按钮后需要将其设置为true
这是所有的类文件
Game.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
import javax.swing.SwingUtilities;
public class Game {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
// the GUI as seen by the user (without frame)
final CardLayout cl = new CardLayout();
final JPanel gui = new JPanel(cl);
// remove if no border is needed
gui.setBorder(new EmptyBorder(10,10,10,10));
JPanel menu = new JPanel(new GridBagLayout());
JButton playGame = new JButton("Play!");
ActionListener playGameListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cl.show(gui, "game");
}
};
playGame.addActionListener(playGameListener);
Insets margin = new Insets(20, 50, 20, 50);
playGame.setMargin(margin);
menu.add(playGame);
gui.add(menu);
cl.addLayoutComponent(menu, "menu");
final JPanel pipes = new Pipes();
gui.add(pipes);
cl.addLayoutComponent(pipes, "game");
JFrame f = new JFrame("Pipes Game");
f.add(gui);
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See http://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
/*if (playerIsReady) {
Timer speed = new Timer(10, new ActionListener() { //pipe speed
@Override
public void actionPerformed(ActionEvent e) {
pipes.move();
}
});
speed.start();
Timer refresh = new Timer(30, new ActionListener() { //refresh rate
@Override
public void actionPerformed(ActionEvent e) {
pipes.repaint();
}
});
refresh.start();
}*/
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);
}
}
java
import java.util.*;
import java.awt.Graphics;
import java.awt.Dimension;
import javax.swing.JPanel;
public class Pipes extends JPanel {
int x1 = 754;
int y1 = setHeightVal();
int y2 = setHeightVal();
int y3 = setHeightVal();
List<Pipe> pipes = new ArrayList<Pipe>();
public Pipes() {
pipes.add(new PipeObject(x1, y1));
pipes.add(new PipeObject(x1 + 200, y2));
pipes.add(new PipeObject(x1 + 400, y3));
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.clearRect(0,0,751,501);
// Draw PipeObject1
// Draw PipeObject2
// Draw PipeObject3
}
public void move() {
x1--;
}
public int getMyX() { //To determine where the pipe is horizontally
return x1-3;
}
public int getMyY() { //To determine where the pipe is vertically
return y2+25;
}
public int setHeightVal() { //Get a random number and select a preset height
int num = (int)(9*Math.random() + 1);
int val = 0;
if (num == 9)
{
val = 295;
}
else if (num == 8)
{
val = 246;
}
else if (num == 7)
{
val = 216;
}
else if (num == 6)
{
val = 185;
}
else if (num == 5)
{
val = 156;
}
else if (num == 4)
{
val = 125;
}
else if (num == 3)
{
val = 96;
}
else if (num == 2)
{
val = 66;
}
else
{
val = 25;
}
return val;
}
@Override
public Dimension getPreferredSize() {
// adjust to need
return new Dimension(751,501);
}
}
大量代码,考虑到根据错误消息(以及您发布的代码),PipeObject
类只缺少一个构造函数:
public class PipeObject {
//Declare and initialiaze variables
int x2 = 75; //pipe width, total is 83
int y1 = -1; // Y should be -1
int gap = 130; //gap height
// Add this:
public PipeObject(int x, int y) {
this.x2 = x;
this.y1 = y;
}
...
}
然后您可以创建一个新的PipeObject,例如在调用中
pipes.add(new PipeObject(x1, y1));
大量代码,考虑到根据错误消息(以及您发布的代码),PipeObject
类只缺少一个构造函数:
public class PipeObject {
//Declare and initialiaze variables
int x2 = 75; //pipe width, total is 83
int y1 = -1; // Y should be -1
int gap = 130; //gap height
// Add this:
public PipeObject(int x, int y) {
this.x2 = x;
this.y1 = y;
}
...
}
然后您可以创建一个新的PipeObject,例如在调用中
pipes.add(new PipeObject(x1, y1));
大量代码,考虑到根据错误消息(以及您发布的代码),PipeObject
类只缺少一个构造函数:
public class PipeObject {
//Declare and initialiaze variables
int x2 = 75; //pipe width, total is 83
int y1 = -1; // Y should be -1
int gap = 130; //gap height
// Add this:
public PipeObject(int x, int y) {
this.x2 = x;
this.y1 = y;
}
...
}
然后您可以创建一个新的PipeObject,例如在调用中
pipes.add(new PipeObject(x1, y1));
大量代码,考虑到根据错误消息(以及您发布的代码),PipeObject
类只缺少一个构造函数:
public class PipeObject {
//Declare and initialiaze variables
int x2 = 75; //pipe width, total is 83
int y1 = -1; // Y should be -1
int gap = 130; //gap height
// Add this:
public PipeObject(int x, int y) {
this.x2 = x;
this.y1 = y;
}
...
}
然后您可以创建一个新的PipeObject,例如在调用中
pipes.add(new PipeObject(x1, y1));
“我修复了这些错误,你能发布一个链接或你的意思的示例吗?”
- 您的
drawPipe
方法应该如下所示
public void drawPipe(Graphics g) {
....
}
public PipeObject(int x, int y) {
this.x2 = x;
this.y1 = y;
}
您不需要将x
和y
值传递给它的原因是每个Pipe
对象已经有了值
int x2 = 75;
int y1 = -1;
构造函数的全部要点是创建一个具有不同值的管道
对象。正如Marco13所指出的,您的构造函数应该如下所示
public void drawPipe(Graphics g) {
....
}
public PipeObject(int x, int y) {
this.x2 = x;
this.y1 = y;
}
因此,如果创建一个新的PipedObject(100100)
,它将被分配x2=100的值;y1=-1代码>
不过,正如我从您以前的帖子中所记得的,我不确定这是否是您想要的构造函数。原因是y
轴应始终保持不变,因为您始终沿同一y
轴移动管道,即水平移动。相反,您可能希望为正在绘制的管道的不同点传递一个x1
和x2
- 要循环,正如我在前面的回答中所示,您需要这样做
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Pipe pipe : pipes){
pipe.drawPipe(g);
}
}
这是每个循环的基本。它表示,对于列表pipes
中的每个Pipe
对象(我们将调用一个随机名称Pipe
),调用Pipe
对象的drawPipe
方法
“我修复了这些错误,你能发布一个链接或你的意思的示例吗?”
- 您的
drawPipe
方法应该如下所示
public void drawPipe(Graphics g) {
....
}
public PipeObject(int x, int y) {
this.x2 = x;
this.y1 = y;
}
您不需要将x
和y
值传递给它的原因是每个Pipe
对象已经有了值
int x2 = 75;
int y1 = -1;
构造函数的全部要点是创建一个具有不同值的管道
对象。正如Marco13所指出的,您的构造函数应该如下所示
public void drawPipe(Graphics g) {
....
}
public PipeObject(int x, int y) {
this.x2 = x;
this.y1 = y;
}
因此,如果创建一个新的PipedObject(100100)
,它将被分配x2=100的值;y1=-1代码>
不过,正如我从您以前的帖子中所记得的,我不确定这是否是您想要的构造函数。原因是y
轴应始终保持不变,因为您始终沿同一y
轴移动管道,即水平移动。相反,您可能希望为正在绘制的管道的不同点传递一个x1
和x2
- 要循环,正如我在前面的回答中所示,您需要这样做
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Pipe pipe : pipes){
pipe.drawPipe(g);
}
}
这是每个循环的基本。它表示,对于列表pipes
中的每个Pipe
对象(我们将调用一个随机名称Pipe
),调用Pipe
对象的drawPipe
方法
“我修复了这些错误,你能发布一个链接或你的意思的示例吗?”
- 您的
drawPipe
方法应该如下所示
public void drawPipe(Graphics g) {
....
}
public PipeObject(int x, int y) {
this.x2 = x;
this.y1 = y;
}
您不需要将x
和y
值传递给它的原因是每个Pipe
对象已经有了值
int x2 = 75;
int y1 = -1;
构造函数的全部要点是创建一个具有不同值的管道
对象。正如Marco13所指出的,您的构造函数应该如下所示
public void drawPipe(Graphics g) {
....
}
public PipeObject(int x, int y) {
this.x2 = x;
this.y1 = y;
}
因此,如果创建一个新的PipedObject(100100)
,它将被分配x2=100的值;y1=-1代码>
不过,正如我从您以前的帖子中所记得的,我不确定这是否是您想要的构造函数。原因是y
轴应始终保持不变,因为您始终沿同一y
轴移动管道,即水平移动。相反,您可能希望为正在绘制的管道的不同点传递一个x1
和x2