1

I'm trying to use Matisse in NetBeans to create a simple game...like Myst. I just want it to display a Frame that has things like a text field and buttons/inventory on the right. I can do that. Then I created a Jpanel that displays on the Frame. I've input some pictures and created a set of cards to make visible and invisible when needed. Each card/scene needs to have buttons that will be different for each scene. Basically I want to make transparent buttons for the user to press that do things (i.e. move to next picture/scene, find a key, etc...). I can create different buttons in each picture, but I can't seem to move them anywhere. I have a suspicion it's due to Matisse, but I don't know.

Here is some of the code, I'm sure it's crude as it's my first implementation of anything in Java. At the very bottom in the Scene to display I've added a button, that displays. I pretty much know now that the setBounds won't work due to the way position works...how would I then place it somewhere. Right now it just displays at the top mid of the picture.

SceneFrame:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;
import scene.SecondPanel;
import scene.FirstPanel;
import scene.Scene;
import javax.swing.JButton;



public class SceneFrame extends javax.swing.JFrame {  

    private FirstPanel sceneP;
    private SecondPanel sceneSP;
    /**
     * Creates new form SceneFrame
     */
    public SceneFrame() {        
        initComponents();
        addCards();      
    }

    public void addCards() {
        sceneP = new FirstPanel();
        SceneManager.add("SecondPanel", sceneP);               
        sceneSP = new SecondPanel();
        SceneManager.add("FirstPanel", sceneSP);
        sceneP.setVisible(false);
    }


    /**
     * 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() {

        SceneManager = new scene.ScenePanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout SceneManagerLayout = new javax.swing.GroupLayout(SceneManager);
        SceneManager.setLayout(SceneManagerLayout);
        SceneManagerLayout.setHorizontalGroup(
            SceneManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 517, Short.MAX_VALUE)
        );
        SceneManagerLayout.setVerticalGroup(
            SceneManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 421, Short.MAX_VALUE)
        );

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("jButton2");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(SceneManager, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(139, 139, 139)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(SceneManager, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 97, Short.MAX_VALUE))
        );

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       sceneP.setVisible(false);
       //SceneManager.showScene("SecondPanel");
       sceneSP.setVisible(true);




    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        sceneSP.setVisible(false);
        //SceneManager.showScene("FirstPanel");
        sceneP.setVisible(true);
    }                                        

    /**
     * @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(SceneFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SceneFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SceneFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SceneFrame.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() {
                new SceneFrame().setVisible(true);                
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JPanel SceneManager;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    // End of variables declaration                   
}

Scene:

package scene;

import java.awt.Color;
import javax.swing.JPanel;
import java.awt.Image;
import javax.swing.ImageIcon;


public abstract class Scene extends JPanel {

    public Image image;
    private String sceneName;

    public Scene(String cn) {
        setName(cn);
        this.setSize(600, 600);
        this.setBackground(Color.WHITE);
    }

    public String getCardName() {
        return sceneName;
    }

  public abstract void addControlButtons();
 // public abstract void implementControlButtons();
}

A Panel to display:

/**
 * Creates a new ForestScene Object.
 */
    public SecondPanel() {
        super("SecondPanel");
        JButton Button2 = new JButton("SwagCity");        
        try {
            image = (new ImageIcon(getClass().getResource("/resources/ForestPath.jpg"))).getImage();
        } catch (Exception e) {/*How to handle?*/
        }
        this.addControlButtons();
    }

    @Override
    public void addControlButtons(){
       JButton bButton = new JButton("bButton");
       //bButton.setBounds(200, 300, 500, 600); //How to display where I want?
       bButton.setAlignmentX(1000);
       this.add(bButton);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        //Graphics2D g2 = (Graphics2D) g;
        if (image != null) {
            boolean val = g.drawImage(image, 0, 0, 800, 600, this);            
        } else {
            System.out.println("Image not found");
        }
    }                               

}
user2079828
  • 645
  • 1
  • 8
  • 31
  • Provide ASCII art of the GUI as it should appear in smallest size and (if resizable) with extra width/height. – Andrew Thompson Nov 25 '13 at 00:34
  • As an aside, the only question there is `} catch (Exception e) {/*How to handle?*/` to which the answer is `Change code of the form `catch (Exception e) { ..` to `catch (Exception e) { e.printStackTrace(); // very informative! ..`.. Do you have another question? What is it? – Andrew Thompson Nov 25 '13 at 00:36
  • The question is at the top. bButton, how do I display it where I want instead of where the GUI places it? I'm also really not sure what your asking. It's a panel, which has a size...within that size I should be able to display a button. The button should display in a position relative to the size of the panel always right? – user2079828 Nov 25 '13 at 00:38
  • @user2079828 since you haven't posted running code, we have no idea where it's currently placed, also we don't know where you want it placed. Maybe a photo example of current/desired position would help. – Paul Samsotha Nov 25 '13 at 01:12
  • I can add code to make it executable, but it will be quite a lot due to generation. I admit I may need to learn more about hand coding this, but it seems like what I want to do is attainable here. Here is some questions, why is "where" it is placed meaningful when the "method" of placing it anywhere at all is not known to me. Lets say I'd like it snapped to the very bottom/left of the panel for arguments sake. – user2079828 Nov 25 '13 at 01:22

2 Answers2

1

I've figured out that it must be the layoutManager confusing something. If i set: this.setLayout(null); I can then set the bounds of the button and it displays where and how I want. I apologize if the question did not have enough information, but I did not know myself anymore than I told you. If anyone has anything to add about what I did, I'll be glad to select that as my answer.

user2079828
  • 645
  • 1
  • 8
  • 31
1

You need to add JFrame.frame.setLayout(null) so that you can adjust your fields(button,textfields) on to the frame.I too had a similar problem

Santino 'Sonny' Corleone
  • 1,735
  • 5
  • 25
  • 52