这是我的第一个问题,如果我能做得更好,请给我评论。
当我致电Hero.Move.MoveLeft();
" JLabel
" labelHero
时不要在视觉上移动。但是调试器说posX
发生了变化。
有趣的是,如果我在第一次添加gamePanel之前调用Hero.Move.MoveLeft();
而不是它的工作。 (我在myWindow
中间标记了位置。)
请求帮助人员。
package com.company.Game;
import com.company.Characters.Hero;
public class Main
{
public static void main(String[] args)
{
Hero.Initialize();
myWindow myWindow = new myWindow();
PaintObjects.PaintNew();
myWindow.Repaint();
Hero.Move.MoveLeft();
Hero.Move.MoveLeft();
Hero.Move.MoveLeft();
Hero.Move.MoveLeft();
Hero.Move.MoveLeft();
Hero.Move.MoveLeft();
Hero.Move.MoveLeft();
Hero.Move.MoveLeft();
}
}
-
package com.company.Characters;
import com.company.Game.Environment;
import com.company.Game.PaintObjects;
import com.company.Game.myWindow;
import javax.swing.*;
import java.awt.*;
public class Hero
{
public Hero()
{
Initialize();
CreateHeroImg();
}
public static JLabel labelHero;
public static ImageIcon iconHero = new ImageIcon("/Users/MJulich/IdeaProjects/Spiel mit Gui/src/Imports/Hero.png");
public static class Life extends Hero
{
public static int actual;
public static int max;
}
public static class Mana extends Hero
{
public static int actual;
public static int max;
}
public static class Damage extends Hero
{
public static int actual;
}
public static class Move extends Hero
{
public static int width;
public static int height;
public static int speed;
public static int posX;
public static void MoveLeft()
{
if (Move.posX > Environment.posShop){
Move.posX--;
PaintObjects.MoveHero();
}
}
}
public static void Initialize()
{
Life.actual = 10;
Life.max = 10;
Mana.actual = 5;
Mana.max = 5;
Damage.actual = 1;
Move.width = 1;
Move.height = 3;
Move.speed = 1;
Move.posX = 10;
}
public static void CreateHeroImg()
{
iconHero.setImage(iconHero.getImage().getScaledInstance(Move.width*myWindow.scale,Move.height*myWindow.scale, Image.SCALE_DEFAULT));
labelHero = new JLabel(iconHero);
labelHero.setBackground(Color.red);
labelHero.setOpaque(true);
labelHero.setBounds(Move.width*myWindow.scale*(Move.posX-1),myWindow.heightLabelBackground-Move.height*myWindow.scale+myWindow.offsetPlayerY,Move.width*myWindow.scale,Move.height*myWindow.scale);
myWindow.gamePanel.add(labelHero);
}
}
-
package com.company.Game;
import com.company.Characters.Hero;
public class PaintObjects
{
public static void PaintNew()
{
Hero.CreateHeroImg();
}
public static void MoveHero()
{
Hero.labelHero.setLocation(Hero.Move.width*myWindow.scale*(Hero.Move.posX-1),myWindow.heightLabelBackground-Hero.Move.height*myWindow.scale+myWindow.offsetPlayerY);
}
}
-
package com.company.Game;
public class Environment
{
public static int posShop = 7;
public static int posSpawn = 40;
}
-
package com.company.Game;
import com.company.Characters.Hero;
import javax.swing.*;
import java.awt.*;
public class myWindow
{
public static int scale = 20;
public static JFrame meinFrame;
public static int heightMeinFrame = 800;
public static int heightGamePanel = 500;
public static int heightControlePanel = 300;
public static int heightLabelBackground = heightGamePanel;
public static int widthMeinFrame = 1000;
public static int widthGamePanel = 1000;
public static int widthControlePanel = 1000;
public static int widthLabelBackground = widthGamePanel;
public static int offsetPlayerY = -(5*scale);
public static JPanel gamePanel;
public static JPanel controlePanel;
public static JLabel labelBackground;
static ImageIcon backgroundIcon = new ImageIcon("/Users/MJulich/IdeaProjects/Spiel mit Gui/src/Imports/Background.png");
static JProgressBar myProgBar1;
static JButton myJBtn1;
static JButton myJBtn2;
static JButton myJBtn3;
static JButton myJBtn4;
static JButton myJBtn5;
static JButton myJBtn6;
static JButton myJBtn7;
static JButton myJBtn8;
public myWindow()
{
//create Frame
meinFrame = new JFrame("Mein JFrame Beispiel");
meinFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* Wir setzen die Breite unseres Fensters auf 1000 Pixel
und die Höhe unseres Fensters auf 800 Pixel */
meinFrame.setSize(widthMeinFrame, heightMeinFrame);
meinFrame.setResizable(false);
//create gamePanel
gamePanel = new JPanel();
gamePanel.setLayout(null);
gamePanel.setBounds(0, 0, widthGamePanel, heightGamePanel);
//create Background
backgroundIcon.setImage(backgroundIcon.getImage().getScaledInstance(widthLabelBackground, heightLabelBackground, Image.SCALE_DEFAULT));
labelBackground = new JLabel(backgroundIcon);
labelBackground.setOpaque(true);
labelBackground.setBounds(0,0,widthLabelBackground,heightLabelBackground);
PaintObjects.PaintNew();
***call "Hero.Move.MoveLeft();" here and it works***
gamePanel.add(labelBackground);
//create controlePanel
controlePanel = new JPanel();
controlePanel.setLayout(null);
controlePanel.setBounds(0, heightGamePanel+1, widthControlePanel, heightControlePanel);
controlePanel.setBackground(new Color(200,200,255));
//create Buttons
myJBtn1 = new JButton("Button 1");
myJBtn1.setBounds(25, 526, 200, 50);
myJBtn2 = new JButton("Button 2");
myJBtn2.setBounds(275, 526, 200, 50);
myJBtn3 = new JButton("Button 3");
myJBtn3.setBounds(525, 526, 200, 50);
myJBtn4 = new JButton("Button 4");
myJBtn4.setBounds(775, 526, 200, 50);
myJBtn5 = new JButton("Button 5");
myJBtn5.setBounds(25, 601, 200, 50);
myJBtn6 = new JButton("Button 6");
myJBtn6.setBounds(275, 601, 200, 50);
myJBtn7 = new JButton("Button 7");
myJBtn7.setBounds(525, 601, 200, 50);
myJBtn8 = new JButton("Button 8");
myJBtn8.setBounds(775, 601, 200, 50);
//create Progressbar
myProgBar1 = new JProgressBar(0, Hero.Life.max);
myProgBar1.setBounds(25, 676, 200, 50);
myProgBar1.setStringPainted(true);
/**einfärben funktioniert noch nicht*/
/*myProgBar1.setBackground(Color.orange);
myProgBar1.setForeground(Color.red);*/
myProgBar1.setString(String.valueOf(Hero.Life.actual) + "/" + String.valueOf(Hero.Life.max));
myProgBar1.setValue(Hero.Life.actual);
//add Buttons and Progressbar to controlePanel
controlePanel.add(myJBtn1);
controlePanel.add(myJBtn2);
controlePanel.add(myJBtn3);
controlePanel.add(myJBtn4);
controlePanel.add(myJBtn5);
controlePanel.add(myJBtn6);
controlePanel.add(myJBtn7);
controlePanel.add(myJBtn8);
controlePanel.add(myProgBar1);
//add Panels to Frame
meinFrame.add(gamePanel);
meinFrame.add(controlePanel);
//show Frame
meinFrame.setVisible(true);
}
public static void Repaint()
{
myProgBar1.setValue(Hero.Life.actual);
myProgBar1.setString(String.valueOf(Hero.Life.actual) + "/" + String.valueOf(Hero.Life.max));
}
}
答案 0 :(得分:0)
Thx家伙,
我找到了问题。
PaintObjects.PaintNew();
被召唤了两次。一次在main方法中,一次在myWindow构造函数中。程序如何失去与可见对象的连接。