在我制作的游戏中,我有一个显示您的分数的结束屏幕,但JLabel
只有在我调整窗口大小时才会显示该结束屏幕。但是当我参加游戏课程并且只在没有主要课程的情况下玩它时,文本显示得很好。
这是我的代码:
package Game;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.media.j3d.AmbientLight;
import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.Material;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.*;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;
public class GameTest extends JFrame{
private PAPITest2 papi;
public TestPane panel;
private boolean inMenu = true;
private ImageIcon ii;
private Image image;
public static void main(String[] args){
final GameTest gt = new GameTest();
}
public GameTest(){
setFocusable(true);
requestFocusInWindow();
requestFocus();
ii = new ImageIcon("Ball Jump.png");
image = ii.getImage();
papi = new PAPITest2();
panel = new TestPane();
add(panel);
setSize(600,600);
setVisible(true);
}
public void paint(Graphics g){
if(inMenu == true){
Graphics2D g2D = (Graphics2D) g;
g2D.drawImage(image, 0, 0, this);
repaint();
}
}
public class TestPane extends JPanel {
public TestPane() {
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false), "pressed");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true), "released");
am.put("pressed", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if(inMenu == true){
inMenu = false;
remove(panel);
papi.setSize(600,600);
papi.setAlive(true);
add(papi);
setVisible(true);
}
if (papi.isAlive() != true){
setSize(60,60);
setVisible(true);
repaint();
remove(papi);
papi = new PAPITest2();
papi.setSize(600,600);
add(papi);
repaint();
}
}
});
am.put("released", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if(inMenu == true){
inMenu = false;
remove(panel);
papi.setSize(600,600);
papi.setAlive(true);
add(papi);
setVisible(true);
}
if (papi.isAlive() != true){
remove(papi);
papi = new PAPITest2();
papi.setSize(600,600);
add(papi);
setVisible(true);
repaint();
}
}
});
setFocusable(true);
requestFocusInWindow();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(600, 600);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
g2D.drawImage(image, 0, 0, this);
repaint();
}
}
}
class PAPITest2 extends Applet implements ActionListener,KeyListener{
private TransformGroup objTrans,objTrans2, objTrans3, objTrans4, objTrans5, objTrans6, objTrans7;
private Transform3D trans = new Transform3D();
private BranchGroup objRoot = new BranchGroup();
private BranchGroup objRoot2 = new BranchGroup();
private BranchGroup objRoot3 = new BranchGroup();
private BranchGroup objRoot4 = new BranchGroup();
private BranchGroup objRoot5 = new BranchGroup();
private BranchGroup objRoot6 = new BranchGroup();
private BranchGroup objRoot7 = new BranchGroup();
private SimpleUniverse u;
private Canvas3D c;
private BranchGroup scene, scene2, scene3, scene4, scene5, scene6, scene7;
private Sphere sphere, sphere2, sphere3, sphere4, sphere5, sphere6, sphere7;
private float height = 0.0f, sign = 1.0f, xloc = 0.0f;
private float height2 = 0.0f, sign2 = -1.0f;
private float rightX, right, rightDx, leftX, left, leftDx, rightX2, leftX2;
private boolean isAlive = true;
private JLabel scoreLabel;
private JLabel label;
private int score = -400;
private Timer timer;
private int sumScore;
private JPanel panel2;
private boolean isEndscreen = true;
public boolean isAlive(){
return isAlive;
}
public void setAlive(boolean isAlive){
this.isAlive = isAlive;
}
public BranchGroup createSceneGraph(){
Color3f ambientColourRSphere = new Color3f(0.6f,0.0f,0.0f);
Color3f emissiveColourRSphere = new Color3f(0.0f,0.0f,0.0f);
Color3f diffuseColourRSphere = new Color3f(0.8f,0.0f,0.4f);
Color3f specularColourRSphere = new Color3f(0.3f,0.0f,0.0f);
float shininessRSphere = 20.0f;
Appearance redSphereApp = new Appearance();
redSphereApp.setMaterial(new Material(ambientColourRSphere,emissiveColourRSphere,
diffuseColourRSphere,specularColourRSphere,shininessRSphere));
objRoot.setCapability(BranchGroup.ALLOW_DETACH);
objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot.addChild(objTrans);
sphere = new Sphere(0.15f,redSphereApp);
objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D pos1 = new Transform3D();
pos1.setTranslation(new Vector3f(0.0f,0.0f,0.0f));
objTrans.setTransform(pos1);
objTrans.addChild(sphere);
objRoot.addChild(objTrans);
BoundingSphere bounds = new BoundingSphere
(new Point3d(0.0,0.0,0.0),100.0);
Color3f light1Color = new Color3f(0.2f,0.2f,1.0f);
Vector3f light1Direction = new Vector3f(+4.0f,-7.0f,-12.0f);
DirectionalLight light1 = new DirectionalLight
(light1Color,light1Direction);
light1.setInfluencingBounds(bounds);
objRoot.addChild(light1);
Color3f ambientColor = new Color3f(1.0f,1.0f,1.0f);
AmbientLight ambientLightNode = new AmbientLight(ambientColor);
ambientLightNode.setInfluencingBounds(bounds);
objRoot.addChild(ambientLightNode);
Color3f bgColor = new Color3f(0.0f, 0.2f, 1.0f);
Background bg = new Background(bgColor);
bg.setApplicationBounds(bounds);
objRoot.addChild(bg);
return objRoot;
}
public BranchGroup createSceneGraph2(){
objRoot2.setCapability(BranchGroup.ALLOW_DETACH);
objTrans2 = new TransformGroup();
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot2.addChild(objTrans2);
sphere2 = new Sphere(0.18f);
objTrans2 = new TransformGroup();
objTrans2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D pos1 = new Transform3D();
pos1.setTranslation(new Vector3f(0.0f,0.0f,0.0f));
objTrans2.setTransform(pos1);
objTrans2.addChild(sphere2);
objRoot2.addChild(objTrans2);
return objRoot2;
}
public BranchGroup createSceneGraph3(){
objRoot3.setCapability(BranchGroup.ALLOW_DETACH);
objTrans3 = new TransformGroup();
objTrans3.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot3.addChild(objTrans3);
sphere3 = new Sphere(0.18f);
objTrans3 = new TransformGroup();
objTrans3.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D pos1 = new Transform3D();
pos1.setTranslation(new Vector3f(0.0f,0.0f,0.0f));
objTrans3.setTransform(pos1);
objTrans3.addChild(sphere3);
objRoot3.addChild(objTrans3);
return objRoot3;
}
public BranchGroup createSceneGraph4(){
objRoot4.setCapability(BranchGroup.ALLOW_DETACH);
objTrans4 = new TransformGroup();
objTrans4.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot4.addChild(objTrans4);
sphere4 = new Sphere(0.45f);
objTrans4 = new TransformGroup();
objTrans4.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D pos1 = new Transform3D();
pos1.setTranslation(new Vector3f(0.0f,0.0f,0.0f));
objTrans4.setTransform(pos1);
objTrans4.addChild(sphere4);
objRoot4.addChild(objTrans4);
return objRoot4;
}
public BranchGroup createSceneGraph5(){
objRoot5.setCapability(BranchGroup.ALLOW_DETACH);
objTrans5 = new TransformGroup();
objTrans5.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot5.addChild(objTrans5);
sphere5 = new Sphere(0.45f);
objTrans5 = new TransformGroup();
objTrans5.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D pos1 = new Transform3D();
pos1.setTranslation(new Vector3f(0.0f,0.0f,0.0f));
objTrans5.setTransform(pos1);
objTrans5.addChild(sphere5);
objRoot5.addChild(objTrans5);
return objRoot5;
}
public BranchGroup createSceneGraph6(){
objRoot6.setCapability(BranchGroup.ALLOW_DETACH);
objTrans6 = new TransformGroup();
objTrans6.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot6.addChild(objTrans6);
sphere6 = new Sphere(0.18f);
objTrans6 = new TransformGroup();
objTrans6.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D pos1 = new Transform3D();
pos1.setTranslation(new Vector3f(0.0f,0.0f,0.0f));
objTrans6.setTransform(pos1);
objTrans6.addChild(sphere6);
objRoot6.addChild(objTrans6);
return objRoot6;
}
public BranchGroup createSceneGraph7(){
objRoot7.setCapability(BranchGroup.ALLOW_DETACH);
objTrans7 = new TransformGroup();
objTrans7.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRoot7.addChild(objTrans7);
sphere7 = new Sphere(0.18f);
objTrans7 = new TransformGroup();
objTrans7.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Transform3D pos1 = new Transform3D();
pos1.setTranslation(new Vector3f(0.0f,0.0f,0.0f));
objTrans7.setTransform(pos1);
objTrans7.addChild(sphere7);
objRoot7.addChild(objTrans7);
return objRoot7;
}
PAPITest2(){
setFocusable(true);
setLayout(new BorderLayout());
setVisible(true);
setSize(505,525);
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
c = new Canvas3D(config);
Color color = new Color(0.0f, 0.2f, 1.0f);
setBackground(color);
setSize(600,600);
Font font = new Font("Helvatica",Font.BOLD,24);
scoreLabel = new JLabel("Score: 0");
add(BorderLayout.PAGE_START,scoreLabel);
add("Center",c);
c.addKeyListener(this);
c.setSize(600,600);
timer = new Timer(60,this);
timer.start();
scene = createSceneGraph();
scene2 = createSceneGraph2();
scene3 = createSceneGraph3();
scene4 = createSceneGraph4();
scene5 = createSceneGraph5();
scene6 = createSceneGraph6();
scene7 = createSceneGraph7();
u = new SimpleUniverse(c);
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
u.addBranchGraph(scene2);
u.addBranchGraph(scene3);
u.addBranchGraph(scene4);
u.addBranchGraph(scene5);
u.addBranchGraph(scene6);
u.addBranchGraph(scene7);
rightX = 2.5f;
right = 4.0f;
float randomSpawn = (float) Math.random() * -2.7f + -3.1f;
float randomSpawn2 = (float) Math.random() * -5.7f + -15.1f;
float randomSpawn3 = (float) Math.random() * 5.7f + 18.1f;
leftX = randomSpawn;
left = randomSpawn;
rightX2 = randomSpawn3;
leftX2 = randomSpawn2;
rightDx = -.05f;
leftDx = .05f;
}
public void keyPressed(KeyEvent e){
if(e.getKeyChar() == 'd'|e.getKeyChar() == 'D'|e.getKeyCode() == e.VK_RIGHT){
xloc = xloc + .1f;
}
if(e.getKeyChar() == 'a'|e.getKeyChar() == 'A'|e.getKeyCode() == e.VK_LEFT){
xloc = xloc - .1f;
}
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
public void actionPerformed(ActionEvent e){
height += .1f * sign;
if(Math.abs(height * 2) >= 1)
sign = -1.0f * sign;
height2 += .1f * sign2;
if(Math.abs(height2 * 2) >= 1)
sign2 = -1.0f * sign2;
Vector3d vec;
int thing = 1;
int thing2 = 2;
if(height < -.4f){
vec = new Vector3d(1.0,.8,1.0);
}else{
vec = new Vector3d(1.0,1.0,1.0);
thing = 2;
}
trans.setScale(vec);
trans.setTranslation(new Vector3f(xloc,height - .15f,0.0f));
objTrans.setTransform(trans);
if(height < -.10f){
vec = new Vector3d(1.0,1.0,1.0);
thing2 = 1;
}
if(height < -.4f){
trans.setScale(new Vector3d(1.0,1.0,1.0));
}
if(score >= 1000){
rightDx = -.06f;
leftDx = .06f;
}
if(score >= 2000){
rightDx = -.07f;
leftDx = .07f;
}
if(score >= 4000){
rightDx = -.08f;
leftDx = .08f;
}
if(score >= 5000){
rightDx = -.1f;
leftDx = .1f;
}
if(score >= 10000){
rightDx = -.2f;
leftDx = .2f;
}
if(score >= 15000){
rightDx = -.3f;
leftDx = .3f;
}
Vector3f vector = new Vector3f(rightX += rightDx,-.7f,0.0f);
trans.setTranslation(vector);
objTrans2.setTransform(trans);
Vector3f vector2 = new Vector3f(leftX += leftDx,-.7f,0.0f);
trans.setTranslation(vector2);
objTrans3.setTransform(trans);
Vector3f vector3 = new Vector3f(rightX2 += rightDx,-.4f,0.0f);
trans.setTranslation(vector3);
objTrans4.setTransform(trans);
Vector3f vector4 = new Vector3f(leftX2 += leftDx,-.4f,0.0f);
trans.setTranslation(vector4);
objTrans5.setTransform(trans);
Vector3f vector5 = new Vector3f(right += rightDx,-.7f,0.0f);
trans.setTranslation(vector5);
objTrans6.setTransform(trans);
Vector3f vector6 = new Vector3f(left += leftDx,-.7f,0.0f);
trans.setTranslation(vector6);
objTrans7.setTransform(trans);
float leftXDistance = vector2.x - xloc - .22f;
float xDistance = vector.x - xloc + .22f;
float yDistance = -.7f - height;
float xDistance2 = vector3.x - xloc - .05f;
float yDistance2 = -.4f - height + .22f;
float xDistance3 = vector5.x - xloc + .22f;
float leftXDistance2 = vector4.x - xloc + .05f;
float leftXDistance3 = vector6.x - xloc - .22f;
double distance = Math.sqrt((xDistance * xDistance) + (yDistance * yDistance));
double distance2 = Math.sqrt((xDistance2 * xDistance2) + (yDistance2 * yDistance2));
double distance3 = Math.sqrt((xDistance3 * xDistance3) + (yDistance * yDistance));
double leftDistance = Math.sqrt((leftXDistance * leftXDistance) + (yDistance * yDistance));
double leftDistance2 = Math.sqrt((leftXDistance2 * leftXDistance2) + (yDistance2 * yDistance2));
double leftDistance3 = Math.sqrt((leftXDistance3 * leftXDistance3) + (yDistance2 * yDistance2));
if(distance < vec.x / 2 + .09f){
if(thing == 2){
score += 100;
scoreLabel.setText("Score: " + score);
float randomSpawn = (float) Math.random() * 2.5f + 3.1f;
rightX = randomSpawn;
trans.setTranslation(vector);
objTrans2.setTransform(trans);
}
if(thing == 1){
objRoot.detach();
setAlive(false);
isAlive = false;
isEndscreen = false;
}
}
if(distance3 < vec.x / 2 + .09f){
if(thing == 2){
score += 100;
scoreLabel.setText("Score: " + score);
float randomSpawn = (float) Math.random() * 2.5f + 3.1f;
right = randomSpawn;
trans.setTranslation(vector5);
objTrans6.setTransform(trans);
}
if(thing == 1){
score -= 100;
objRoot.detach();
isAlive = false;
isEndscreen = false;
}
}
if(distance2 < vec.x / 2 + .09f){
if(thing2 == 2){
score += 200;
scoreLabel.setText("Score: " + score);
float randomSpawn = (float) Math.random() * 4.1f + 20.9f;
rightX2 = randomSpawn;
trans.setTranslation(vector3);
objTrans4.setTransform(trans);
}
if(thing2 == 1){
score -= 200;
objRoot.detach();
isAlive = false;
isEndscreen = false;
}
}
if(leftDistance < vec.x / 2 + .09f){
if(thing == 2){
score += 100;
scoreLabel.setText("Score: " + score);
float randomSpawn = (float) Math.random() * -1.5f + -2.7f;
leftX = randomSpawn;
trans.setTranslation(vector2);
objTrans3.setTransform(trans);
}
if(thing == 1){
score -= 100;
objRoot.detach();
isAlive = false;
isEndscreen = false;
}
}
if(leftDistance3 < vec.x / 2 + .09f){
if(thing == 2){
score += 100;
scoreLabel.setText("Score: " + score);
float randomSpawn = (float) Math.random() * -1.5f + -2.7f;
left = randomSpawn;
trans.setTranslation(vector6);
objTrans7.setTransform(trans);
}
if(thing == 1){
score -= 100;
objRoot.detach();
isAlive = false;
isEndscreen = false;
}
}
if(leftDistance2 < vec.x / 2 + .09f){
if(thing2 == 2){
score += 200;
scoreLabel.setText("Score: " + score);
float randomSpawn = (float) Math.random() * -3.1f + -17.9f;
leftX2 = randomSpawn;
trans.setTranslation(vector4);
objTrans5.setTransform(trans);
}
if(thing2 == 1){
score -= 200;
objRoot.detach();
isAlive = false;
isEndscreen = false;
}
}
if(rightX < -1.5f){
float randomSpawn = (float) Math.random() * 1.5f + 2.1f;
rightX = randomSpawn;
trans.setTranslation(vector);
objTrans2.setTransform(trans);
}
if(leftX > 1.5f){
float randomSpawn = (float) Math.random() * -1.5f + -2.1f;
leftX = randomSpawn;
trans.setTranslation(vector2);
objTrans3.setTransform(trans);
}
if(right < -1.5f){
float randomSpawn = (float) Math.random() * 1.5f + 2.1f;
right = randomSpawn;
trans.setTranslation(vector);
objTrans2.setTransform(trans);
}
if(left > 1.5f){
float randomSpawn = (float) Math.random() * -1.5f + -2.1f;
left = randomSpawn;
trans.setTranslation(vector2);
objTrans3.setTransform(trans);
}
if(rightX2 < -1.5f){
float randomSpawn = (float) Math.random() * 1.5f + 2.1f;
rightX2 = randomSpawn;
trans.setTranslation(vector3);
objTrans4.setTransform(trans);
}
if(leftX2 > 1.5f){
float randomSpawn = (float) Math.random() * -1.5f + -2.1f;
leftX2 = randomSpawn;
trans.setTranslation(vector4);
objTrans5.setTransform(trans);
}
if(xloc > .8f){
xloc = .8f;
}
if(xloc < -.8f){
xloc = -.8f;
}
sumScore = score;
if(!isAlive && !isEndscreen){
endScreen();
}
}
public void endScreen(){
isEndscreen = true;
remove(scoreLabel);
remove(c);
setSize(600,600);
setVisible(true);
setLayout(new BorderLayout(1,1));
Font font = new Font("Helvatica",Font.BOLD,24);
if(sumScore < 0){
sumScore = 0;
}
label = new JLabel("Your Score: " + sumScore);
JLabel label2 = new JLabel(" Press Space");
label2.setFont(font);
label.setFont(font);
setBackground(Color.white);
add(BorderLayout.NORTH,label);
add(BorderLayout.CENTER,label2);
repaint();
}
}