Skip to main content
gamedev.net
🔒 Locked

[java] JLabel fun... :(

Started by ABCDEFG123 4 replies 1.6k views
Original Post
ABCDEFG123
ABCDEFG123
Hey All, Yes this is a NUB of a question, I'm trying to make a GUI with a JPanel that will have a 10x10 grid layout, in that grid i want to place a 10x10 array of labels containing animated GIFs. Is there some reason that this won't work? if not please take a look at my code and tell me what Im doing wrong. thanks DanLatimer package gameGUIView; import java.applet.*; import java.awt.*; import java.net.*; import javax.swing.*; import javax.swing.JButton; import javax.swing.JPanel; import java.awt.BorderLayout; import java.awt.Graphics; import java.awt.GridLayout; import javax.swing.JLabel; import javax.swing.ImageIcon; public class GameGUView extends JFrame { // @jve:decl-index=0:visual-constraint="161,29" private JPanel gridPane = null; private JPanel canvas = null; private JPanel controlPanel = null; private JPanel framePane = null; private JLabel[][] pic = null; private JButton turnLeft = null; private JButton walkForword = null; private JButton turnRight = null; private JButton Sensor = null; public GameGUView() { this.setAlwaysOnTop(true); this.show(); initialize(); repaint(); this.show(); } /** * This method initializes this * */ private void initialize() { try { this.setSize(new java.awt.Dimension(400,550)); this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); this.setContentPane(getFramePane()); this.setTitle("Maze V.1b"); this.setPreferredSize(new java.awt.Dimension(400,550)); this.setResizable(false); this.setName("mainFrame"); } catch (java.lang.Throwable e) { // Do Something } } public void paint (Graphics g) { //g.drawImage (new ImageIcon("background.gif").getImage(), 0, 32, canvas); //g.drawImage(new ImageIcon("MogFinger.gif").getImage(), 200,200, mainPanel); } /** * This method initializes jContentPane * * @return javax.swing.JPanel */ private JPanel getGridPane() { if (gridPane == null) { try { for(int i = 0; i < 10; i++) { for(int j = 0; j < 10; j++) { pic[j] = new JLabel(); pic[j].setText("A"); pic[j].setVisible(true); //pic[j].setIcon(new ImageIcon(getClass().getResource("/MogFinger.gif"))); } } GridLayout gridLayout = new GridLayout(); gridLayout.setRows(10); gridLayout.setColumns(10); gridPane = new JPanel(); gridPane.setPreferredSize(new java.awt.Dimension(400,400)); gridPane.setLayout(gridLayout); for(int i = 0; i < 10; i++) { for(int j = 0; j < 10; j++) { gridPane.add(pic[j], null); } } } catch (java.lang.Throwable e) { // TODO: Something } } return gridPane; } /** * This method initializes canvas * * @return javax.swing.JPanel */ private JPanel getCanvas() { if (canvas == null) { try { canvas = new JPanel(); canvas.add(getGridPane(), null); } catch (java.lang.Throwable e) { // TODO: Something } } return canvas; } /** * This method initializes controlPanel * * @return javax.swing.JPanel */ private JPanel getControlPanel() { if (controlPanel == null) { try { controlPanel = new JPanel(); controlPanel.add(getTurnLeft(), null); controlPanel.add(getWalkForword(), null); controlPanel.add(getTurnRight(), null); controlPanel.add(getSensor(), null); } catch (java.lang.Throwable e) { // TODO: Something } } return controlPanel; } /** * This method initializes framePane * * @return javax.swing.JPanel */ private JPanel getFramePane() { if (framePane == null) { try { framePane = new JPanel(); framePane.setLayout(new BorderLayout()); framePane.add(getCanvas(), java.awt.BorderLayout.CENTER); framePane.add(getControlPanel(), java.awt.BorderLayout.SOUTH); } catch (java.lang.Throwable e) { // TODO: Something } } return framePane; } /** * This method initializes label * * @return javax.swing.JLabel */ /** * This method initializes turnLeft * * @return javax.swing.JButton */ private JButton getTurnLeft() { if (turnLeft == null) { try { turnLeft = new JButton(); turnLeft.setName(""); turnLeft.setText("Turn Left"); } catch (java.lang.Throwable e) { // TODO: Something } } return turnLeft; } /** * This method initializes walkForword * * @return javax.swing.JButton */ private JButton getWalkForword() { if (walkForword == null) { try { walkForword = new JButton(); walkForword.setText("Walk Forword"); } catch (java.lang.Throwable e) { // TODO: Something } } return walkForword; } /** * This method initializes turnRight * * @return javax.swing.JButton */ private JButton getTurnRight() { if (turnRight == null) { try { turnRight = new JButton(); turnRight.setText("Turn Right"); } catch (java.lang.Throwable e) { // TODO: Something } } return turnRight; } /** * This method initializes Sensor * * @return javax.swing.JButton */ private JButton getSensor() { if (Sensor == null) { try { Sensor = new JButton(); Sensor.setText("Sensor"); } catch (java.lang.Throwable e) { // TODO: Something } } return Sensor; } } // @jve:decl-index=0:visual-constraint="73,17"
Son of Cain
Son of Cain
First of all, when posting code, use the following tags (without the spaces):
[ source lang="Java"] [/ source]

Second, why not instantiate each JLabel with an ImageIcon instance?
JLabel label = new JLabel(new ImageIcon("../../image.png"));


That's not how you would do in a game, surely, but for a small grid of 10 images...

Son Of Cain
a.k.a javabeats at yahoo.ca
Haffa
Haffa
You need to initialize your array of JLabels before you use it:
JLabel pic[][] = new JLabel[10][10];
CaptainJester
CaptainJester
The only gif animation I could get working is the one show here: http://java.sun.com/products/java-media/2D/samples/suite/. Look for animated gif with transparent background.

This one uses the old Toolkit.getImage() instead of ImageIO. When I tried to follow the source of the demo and use ImageIO, the animation did not work. It only worked when it was loaded the old way. I then tried putting an image on a JLabel, but using the old getImage() to load the image and the animation worked. It looks like there might be a bug with ImageIO.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
ABCDEFG123
ABCDEFG123
thanks CaptainJester Thats very helpful.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.