Question

I have been trying for a couple days now to get this image called star1.png to show up. I am trying to "shoot" it out from my stick man but I can't even get the image to show up. Any help would be greatly appreciated. Thanks

    package my.testing1;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    /**
    *
    *  @author Jamie
    */
   public class GUI extends javax.swing.JFrame 
   {

   JLabel label = new JLabel(new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman.png"));
   JLabel cords = new JLabel();
   JLabel bg = new JLabel(new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\background.png"));
   JLabel star = new JLabel(new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\star1.png"));

   int locx = 0; 
   int locy = 0;
 int i = 0;
 int x = label.getX();
 int y = label.getY();
 String cor= Integer.toString(x) +" "+ Integer.toString(y);
 boolean ph = true;
 boolean left = true;
 boolean Sph = true;

 public GUI()
     {   
     this.setSize(800,600);
     this.cords.setText(cor);
     label.setBounds(x,y,120,120);
     cords.setBounds(20,170,50,50);
     bg.setBounds(0,0,800,600);
     //star.setBounds(x,y,50,50);


     cor= Integer.toString(x) + " "+ Integer.toString(y);
     label.addKeyListener(new KeyAdapter()

         {

         @Override
         public void keyPressed(KeyEvent ke)
          {
             if (ke.getKeyCode() == KeyEvent.VK_S)
             {
                 //locx=label.getX();
                 //locy=label.getY();
                 ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\star1.png");
                 star.setIcon(newIcon);

                 star.setLocation(300,300);


             }
             if(ke.getKeyCode() == KeyEvent.VK_DOWN)
             {
              if (y>0 | y==0)
                 {
                     y=y+10;
                 }
               if (y>440)
                 {
                     y=y-10;
                 }
                 label.setLocation(x,y);
                 if(ph == false)
                 {
                    if(left == true)
                    {

                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman2Left.png");
                        //newIcon.getImage().flush();
                        label.setIcon(newIcon);

                    }
                    else
                    {
                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman2.png");
                        label.setIcon(newIcon);
                    }

                    ph = true;
                 }
                 else
                 {
                     if(left == true)
                    {

                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickmanLeft.png");
                        //newIcon.getImage().flush();
                        label.setIcon(newIcon);

                    }
                    else
                    {
                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman.png");
                        label.setIcon(newIcon);
                    }
                    ph = false;
                 }
                 repaint();
                 cords.setLocation(20,170);
                 cor= Integer.toString(x) + " "+Integer.toString(y);
                 cords.setText(cor);
             }
             if(ke.getKeyCode() == KeyEvent.VK_UP)
             {
                if (y>440 | y>0)
                 {
                     y=y-10;
                 }
                if (y<0)
                {
                    y=y+10;
                }
                 label.setLocation(x,y);
                 if(ph == false)
                 {
                     if(left == true)
                    {

                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman2Left.png");
                        //newIcon.getImage().flush();
                        label.setIcon(newIcon);

                    }
                    else
                    {
                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman2.png");
                        label.setIcon(newIcon);
                    }
                    ph = true;
                 }
                 else
                 {
                     if(left == true)
                    {

                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickmanLeft.png");
                        //newIcon.getImage().flush();
                        label.setIcon(newIcon);

                    }
                    else
                    {
                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman.png");
                        label.setIcon(newIcon);
                    }
                    ph = false;
                 }
                repaint();
                cords.setLocation(20,170);
                cor= Integer.toString(x) +" "+ Integer.toString(y);
                cords.setText(cor);
             }
             if(ke.getKeyCode() == KeyEvent.VK_LEFT)
             {
                 if (x<0)
                 {
                     x=x+10;
                 }
                 if (x>0)
                 {
                     x=x-10;
                 }
                 label.setLocation(x,y);
                 left=true;
                 if(ph == false)
                 {

                     if(left == true)
                    {

                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman2Left.png");
                        //newIcon.getImage().flush();
                        label.setIcon(newIcon);

                    }
                    else
                    {
                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman2.png");
                        label.setIcon(newIcon);
                    }
                    ph = true;
                 }
                 else
                 {
                     if(left == true)
                    {

                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickmanLeft.png");
                        //newIcon.getImage().flush();
                        label.setIcon(newIcon);

                    }
                    else
                    {
                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman.png");
                        label.setIcon(newIcon);
                    }
                    ph = false;
                 }
                 repaint();
                 cords.setLocation(20,170);
                 cor= Integer.toString(x) + " "+Integer.toString(y);
                 cords.setText(cor);
             }

             if(ke.getKeyCode() == KeyEvent.VK_RIGHT)
             {
                 if (x<690)
                 {
                     x=x+10;
                 }
                 if (x>690)
                 {
                     x=x-10;
                 }
                 label.setLocation(x,y);
                 left=false;
                 if(ph == false)
                 {
                     if(left == true)
                    {

                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman2Left.png");
                        //newIcon.getImage().flush();
                        label.setIcon(newIcon);

                    }
                    else
                    {
                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman2.png");
                        label.setIcon(newIcon);
                    }
                    ph = true;
                 }
                 else
                 {
                     if(left == true)
                    {

                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickmanLeft.png");
                        //newIcon.getImage().flush();
                        label.setIcon(newIcon);

                    }
                    else
                    {
                        ImageIcon newIcon = new ImageIcon("C:\\Users\\Jamie\\Desktop\\Java\\stickman.png");
                        label.setIcon(newIcon);
                    }
                    ph = false;
                 }
                 repaint();
                 cords.setLocation(20,170);
                 cor= Integer.toString(x) + " "+Integer.toString(y);
                 cords.setText(cor);
             }
             //System.out.println(x+y);
         }
     });
     label.addMouseListener(new MouseAdapter()
         {
         //@Override
         public void mouseClicked(MouseEvent me)
             {
             if(me.getClickCount() == 1)
                 {
                 boolean dd = label.isOptimizedDrawingEnabled();
                 boolean ff = label.requestFocusInWindow();
                 repaint();
             }
         }

     });

     JPanel p = new JPanel();

     p.setSize(700,700);
     this.setDefaultCloseOperation(GUI.EXIT_ON_CLOSE );
     p.add(cords);
     p.setLayout(null);
     p.add(label);
     p.add(bg);
     p.add(star);
     JScrollPane js = new JScrollPane(p);
     getContentPane().add(js);
 }

/**
 * Creates new form GUI
 */
/*public GUI() {
    initComponents();
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setAlwaysOnTop(true);
    setBackground(new java.awt.Color(0, 102, 0));
    setBounds(new java.awt.Rectangle(10, 10, 700, 700));
    setPreferredSize(new java.awt.Dimension(700, 700));

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 604, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 376, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>                        

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
   java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
    GUI frame = new GUI();        
    new GUI().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
// End of variables declaration                   
}
Was it helpful?

Solution

You're going to kill yourself after this, but here is your problem

 p.add(label);
 p.add(bg);
 p.add(star);

You see? you place the stickman on top, then the background beneath, then star at the bottom, so no matter where it is, it is hidden behind the background image. Just replace with

 p.add(label);
 p.add(star);
 p.add(bg);    

(or add star before label if you want it to appear above it when they share position.)

By the way, there are many things there you could write better not related to your problem. If you want I can post them.

OTHER TIPS

Your KeyAdapter is not triggered when a key is pressed. Simply replace:

label.addKeyListener(new KeyAdapter()

with:

addKeyListener(new KeyAdapter()

in GUI() constructor so that the Frame handles KeyEvents instead of JLabel.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top