2016-10-21 4 views
1

C'est ma première question alors donnez-moi des critiques si je peux faire quelque chose de mieux.Java - setLocation/Bounds sur un JLabel avec un ImageIcon ne fonctionne pas à partir d'une autre classe

Lorsque j'appelle Hero.Move.MoveLeft(); le JLabel "labelHero" ne bougez pas visuellement. Mais le débogueur dit que posX change. La chose intéressante est que si j'appelle Hero.Move.MoveLeft(); avant que le gamePanel soit ajouté la première fois au Frame, cela fonctionne. (J'ai marqué la position au milieu de myWindow.)

Thx pour l'aide les gars.

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)); 
    } 
} 
+1

S'il vous plaît modifier votre question pour inclure un _single_ [mcve] qui se concentre sur le problème déclaré; vous pouvez accéder aux images publiées via 'URL', comme indiqué [ici] (http://stackoverflow.com/a/10862262/230513); utiliser des images synthétiques comme indiqué [ici] (http://stackoverflow.com/a/15982915/230513); ou utilisez les icônes 'UIManager', comme indiqué [ici] (http://stackoverflow.com/a/12228640/230513). – trashgod

+1

Salut! Bienvenue sur Stackoverflow. S'il vous plaît voir [how-to-ask] (http://stackoverflow.com/help/how-to-ask) pour apprendre à poser des questions de haute qualité à l'avenir que d'autres peuvent facilement répondre. Pour l'instant, s'il vous plaît voir le commentaire 'trashgod' et apporter des ajustements à cette question en conséquence. Bonne chance! – Ortund

+1

Trop de variables statiques. "statique" n'est pas nécessaire. De même, vous ne devez PAS utiliser une mise en page nulle. Swing a été conçu pour être utilisé avec les gestionnaires de disposition. – camickr

Répondre

0

Thx les gars,

J'ai trouvé le problème.

PaintObjects.PaintNew(); a été appelée deux fois. Une fois dans la méthode principale et une fois dans le constructeur myWindow. Cependant, le programme a perdu la connexion avec l'objet visible.