Java 如何显示两侧的球数(球数)?
我写了一个程序来扮演麦克斯韦的恶魔。它应该有一个有两个边和几个边的窗户 到处跑的圆点从墙上弹下来。中间有一堵墙。当您单击 按钮,墙上会出现一个缺口,直到您松开为止。这应该允许一个人尝试 在一边得到比另一边更多的球。您可以在每一侧显示计数 这是主要的课程,我有两个其他的课程,一个是圆点,我让圆点移动,还有 告诉我速度有多快,另一个是蓝点,我在那里创建蓝点 所以我想要的是显示两边的球数?或者我们有多少人 墙壁两边都有球 主程序Java 如何显示两侧的球数(球数)?,java,Java,我写了一个程序来扮演麦克斯韦的恶魔。它应该有一个有两个边和几个边的窗户 到处跑的圆点从墙上弹下来。中间有一堵墙。当您单击 按钮,墙上会出现一个缺口,直到您松开为止。这应该允许一个人尝试 在一边得到比另一边更多的球。您可以在每一侧显示计数 这是主要的课程,我有两个其他的课程,一个是圆点,我让圆点移动,还有 告诉我速度有多快,另一个是蓝点,我在那里创建蓝点 所以我想要的是显示两边的球数?或者我们有多少人 墙壁两边都有球 主程序 package dotChaser; //DotChaser.java
package dotChaser;
//DotChaser.java
//This program does some simple animation where a dot move around
//the blue dots.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DotChaser extends JFrame implements ActionListener, KeyListener
{
javax.swing.Timer time; // generates ticks that drive the animation
protected double deltaT = 0.020; // how much time for each tick
BlueDot[] blues; // holds the blue dots
int blueCounter = 0; // number of blue dots
//Label howMany = new Label();
JTextField tf;// The text field for the key listener
boolean open; // open= true part. pass through, open = false part. dont pass through
public static void main(String[] args)
{
DotChaser d = new DotChaser();
}
// constructor
public DotChaser()
{
Dot.theDotChaser = this;
//time managment
time = new javax.swing.Timer((int)(1000*deltaT), this);
time.start();
blues= new BlueDot[200] ;
tf = new JTextField("---------------");
add(tf);
tf.addKeyListener(this);
for(int i = 0; i<20; i++)
{blues[blueCounter++] = new BlueDot();
//count++;
//howMany.setText(ballReport());
setLayout( new FlowLayout());
setTitle("Dot Chaser!");
setSize(new Dimension(600,600));
setVisible(true);}
}
// the work its doing
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==time){doTime();}
repaint();
}
// key listener mouse motion
public void keyTyped( KeyEvent k ) { }
public void keyPressed( KeyEvent k )
{
System.out.println("you pressed key="+k.getKeyChar());
open=true;
}
public void keyReleased( KeyEvent k )
{
System.out.println("you released key="+k.getKeyChar());
open=false;
}
// the clock ticked, so tell all the dots to move a little
public void doTime()
{
for (int i= 0; i<blueCounter; i++)
{
blues[i].move(deltaT);
}
}
// make another blue dot (and add it to the blues array)
public void doMoreDots()
{blues[blueCounter++] = new BlueDot();}
// paint all the dots
public void paint(Graphics g)
{
super.paint(g);
for (int i= 0; i<blueCounter; i++)
{
blues[i].drawMe(g);
}
{
// draw the wall
g.setColor( Color.pink );
g.fillRect( 250, 40, 90, 200 );
g.fillRect( 250, 400, 90, 300 );
}
// close wall when false
if ( open == false)
{
g.fillRect(250, 200, 90, 200);
g.fillRect(250, 400, 90, 300);
}
}
}
当我得到的时候,你需要计算每一面的圆点总数 从保存的数组中可以获得的点数总数。在下面的方法中添加一个计数器,以保持点数总数
static int count=0;
public void doMoreDots()
{
blues[blueCounter++] = new BlueDot();
count++;
}
现在,你需要找出每边的计数,这里我建议在这个类中使用另外两个单独的计数器,在那里你可以跟踪移动的点
希望能对您有所帮助。嗯,您所说的另外两个独立柜台是什么意思,是什么意思?在同一个类中,我的主类是否会添加其他两个方法?就跟多莫点一样?如果你不介意给我看的话。我加上了计数++;但它没有给我点的总数?