Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Java array help

Name: Anonymous 2010-07-30 21:13

hey /prog/ just wondering if anyone could give me a hand with some java code. Basically I am trying to place labels from an array of JLabels into a JPanel using .add, but it doesn't want to work. I have never used an array of JLabels before, so i am certain that i fucked up somewhere.

Code:

public void setProp() {

        for (int i = 0; i < label.length; i++) {
            label[i] = new JLabel("");
            label[i].setPreferredSize(labelDim);
            label[i].setIcon(new ImageIcon(this.getClass().getResource("blank.gif")));
            label[i].setEnabled(true);
            label[i].setVisible(false);
            pnlMap.add(label[i]);

        }
    }


also blank.gif is just a black square and the dimensions are set to match the icon dimensions.

any input would be appreciated

Name: Anonymous 2010-07-31 0:19

>>20


import java.awt.Dimension;
import java.io.*;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class Test extends javax.swing.JFrame {
    //initializes arrays for map

    ImageIcon tiles[] = new ImageIcon[144];
    JLabel labels[] = new JLabel[144];
    JLabel label[] = new JLabel [4];
    int numArray[] = new int[144];
    //dimension object for setting label array properties
    Dimension labelDim = new Dimension(46, 27);

    public void setProp() {

        for (int i = 0; i < label.length; i++) {
            label[i] = new JLabel("");
            label[i].setPreferredSize(labelDim);
            label[i].setIcon(new ImageIcon(this.getClass().getResource("blank.gif")));
            label[i].setEnabled(true);
            label[i].setVisible(false);
            pnlMap.add(label[i]);

        }
    }
    //fix this shit
   public void placeTiles() {
        int x = 0;
        int y = 0;
        int k = 0;
            for (int row = 0; row < 12;) {
                for (int col = 0; col < 12; col++) {
                    if (row == 0) {
                        if (col == 0){
                        labels[col].setLocation(20, 20);
                        x = labels[col].getX();
                        y = labels[col].getY();
                        }

                    } else {
                     labels[col].setLocation(x-(k*22), (y+(col*13)));
                    }
                    if (col==11){
                        col = 12;
                        row++;
                        k++;
                    }
                }

            }

    }

    //This method translates integer values from the num array into
    //tile images in the image array
    public void defineTiles() {
        //for loop to go through entire num array
        for (int a = 0; a < 144; a++) {
            //places value from index of num array in temp variable
            int temp = numArray[a];
            //sets proper tile in corresponding index of image array
            switch (temp) {
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
            }
        }
    }

    public void openText() {
        String row;
        try {
            //initiated reader for text file
            BufferedReader fileRead = new BufferedReader(new FileReader("test.txt"));
            //because maps are saved in 1 line, just reads line into string
            row = fileRead.readLine();
            //loop to translate line into numerical values in array for each tile
            for (int b = 0; b < 144; b++) {
                //splits string into characters and then convertst to an integer
                char character = row.charAt(b);
                String string = Character.toString(character);
                numArray[b] = Integer.parseInt(string);
            }
            //closes stream
            fileRead.close();
            //catches exception
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);
        }
    }

    /** Creates new form Test */
    public Test() {
        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() {

        bntTile = new javax.swing.JButton();
        lblSprite = new javax.swing.JLabel();
        lbl1 = new javax.swing.JLabel();
        lbl2 = new javax.swing.JLabel();
        lbl3 = new javax.swing.JLabel();
        pnlMap = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(500, 500));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        bntTile.setText("tile");
        bntTile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntTileActionPerformed(evt);
            }
        });
        getContentPane().add(bntTile, new org.netbeans.lib.awtextra.AbsoluteConstraints(306, 32, -1, -1));

        lblSprite.setIcon(new javax.swing.ImageIcon(getClass().getResource("/knight.gif"))); // NOI18N
        getContentPane().add(lblSprite, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 40, 20, 30));

        lbl1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/0.gif"))); // NOI18N
        getContentPane().add(lbl1, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 50, -1, 30));

        lbl2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/0.gif"))); // NOI18N
        getContentPane().add(lbl2, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 80, -1, 30));

        lbl3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/0.gif"))); // NOI18N
        getContentPane().add(lbl3, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 120, -1, 30));

        pnlMap.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        pnlMap.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        getContentPane().add(pnlMap, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 140, 380, 360));

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

    private void bntTileActionPerformed(java.awt.event.ActionEvent evt) {                                       
        int x = lbl1.getX();
        int y = lbl1.getY();
        lbl2.setLocation((x - 22), (y + 13));
        lbl3.setLocation((x + 22), (y + 13));
        lblSprite.setLocation(((lbl2.getX()) + 10), ((lbl2.getY()) - 10));
        openText();
        setProp();
        //placeTiles();
    }                                      

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Test().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                    
    private javax.swing.JButton bntTile;
    private javax.swing.JLabel lbl1;
    private javax.swing.JLabel lbl2;
    private javax.swing.JLabel lbl3;
    private javax.swing.JLabel lblSprite;
    private javax.swing.JPanel pnlMap;
    // End of variables declaration                  
}


There is a lot of shit there that isn't really related, but you asked for code

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List