Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Swing and paintComponent
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
evogre3n




PostPosted: Sat Dec 24, 2005 5:45 pm   Post subject: Swing and paintComponent

Hey all,

for my final project I am to create a battleship game. And we've been operating in Console (Ready to Program) all year, and although my teacher doesnt expect anything beyond Console, Im going ahead and trying to do it with Swing.

Now, I have all the concepts down, and the logic of the game is covered for now. Im trying to create menus and buttons etc with Swing now and its quite a pain because i just started.

Anyway, below is the code I have so far for a simple JFrame, and some buttons with a background image (overriding paintComponent).

code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class MainMenu extends JFrame
{
    private static JPanel panel_mainMenu, panel_buttons, panel_image;
    private static JButton button_newGame, button_loadGame, button_highScore, button_exit;

    public MainMenu ()
    {
        super ("Main Menu");
        this.setSize (694, 697);

        panel_mainMenu = new JPanel ();
        panel_buttons = new JPanel (new GridLayout (1, 4));

        button_newGame = new JButton ("New Game");
        button_loadGame = new JButton ("Load Game");
        button_highScore = new JButton ("High Scores");
        button_exit = new JButton ("Exit");

        panel_buttons.add (button_newGame);
        panel_buttons.add (button_loadGame);
        panel_buttons.add (button_highScore);
        panel_buttons.add (button_exit);

        panel_mainMenu.add (panel_buttons);

        panel_image = new JPanel ()
        {
            public void paintComponent (Graphics g)
            {
                super.paintComponent (g);

                Image img = Toolkit.getDefaultToolkit ().
                    getImage ("bs.gif");

                g.drawImage (img, 0, 0, null);
            }
            ;
        }
        ;

        panel_image.setPreferredSize (new Dimension (694, 697));

        this.getContentPane ().add (panel_image);
        this.getContentPane ().add (panel_mainMenu, BorderLayout.SOUTH);

        //show the frame
        this.pack ();
        this.show ();
    }


    public static void main (String[] args)
    {
        new MainMenu ();
    }
}


(the image is attached, stick both in the same dir)

The problem with this is that the image does NOT draw, UNTIL I minimize the window, and then maximize it again, or cover the window with another larger window and then move it back.

Does anyone have any experience with this?

Perhaps there is another way for me to draw this to the JPanel?

I was considering using just an Icon, but for the actual grid and things, I need the drawing functions etc, so im using the Graphics2D instead.

Help me out here?
Thanks alot!
Sponsor
Sponsor
Sponsor
sponsor
evogre3n




PostPosted: Sat Dec 24, 2005 6:17 pm   Post subject: (No subject)

Here is the alternative I've come up with using Icon.

code:

// The "MainMeny" class.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class MainMenu extends JFrame
{
    private static JPanel panel_mainMenu, panel_buttons, panel_image, panel_Splash;
    private static JButton button_newGame, button_loadGame, button_highScore, button_exit;
    private static JLabel label_splashImage;

    public MainMenu ()
    {
        super ("Main Menu");

        // Create a new object that holds the splash image
        ImageIcon icon_Splash = new ImageIcon ("bs.gif");

        // Panel Initializations
        // mainMenu panel, button panel, and splash panel
        panel_mainMenu = new JPanel ();
        panel_buttons = new JPanel (new GridLayout (1, 4));
        panel_Splash = new JPanel ();

        // Button Initializations
        // newgame button, loadgame button
        // highscore button, exit button
        button_newGame = new JButton ("New Game");
        button_loadGame = new JButton ("Load Game");
        button_highScore = new JButton ("High Scores");
        button_exit = new JButton ("Exit");

        // Label Initializations
        // splash image panel
        label_splashImage = new JLabel (icon_Splash);

        // add the buttons to button panel
        panel_buttons.add (button_newGame);
        panel_buttons.add (button_loadGame);
        panel_buttons.add (button_highScore);
        panel_buttons.add (button_exit);

        // add the splash label to the splash panel
        panel_Splash.add (label_splashImage);

        // add internal panels to main panel
        panel_mainMenu.add (panel_buttons);

        // add the panels to the frame (container)
        this.getContentPane ().add (panel_Splash);
        this.getContentPane ().add (panel_mainMenu, BorderLayout.SOUTH);

        //show the frame
        this.pack ();
        this.setVisible (true);



        // panel_image = new JPanel ()
        // {
        //     public void paintComponent (Graphics g)
        //     {
        //         super.paintComponent (g);
        //
        //         Image img = Toolkit.getDefaultToolkit ().
        //             getImage ("bs.gif");
        //
        //         g.drawImage (img, 0, 0, null);
        //     }
        //     ;
        // }
        // ;
        //
        // panel_image.setPreferredSize (new Dimension (694, 697));
    }



    public static void main (String[] args)
    {
        new MainMenu ();
    }
}
wtd




PostPosted: Sat Dec 24, 2005 6:21 pm   Post subject: (No subject)

This might give you an idea of how to better structure your program.

code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class MainMenu extends JFrame {
    private static JPanel panel_mainMenu, panel_image;

    public static void main(String[] args) {
        new MainMenu();
    }

    public MainMenu() {
        super("Main Menu");
       
        ButtonsPanel controlButtons = new ButtonsPanel();
       
        setSize(694, 697);

        panel_mainMenu = new JPanel();

        panel_mainMenu.add(controlButtons);

        panel_image = new JPanel() {
            public void paintComponent(Graphics g) {
                super.paintComponent(g);

                Image img = Toolkit.getDefaultToolkit().getImage("bs.gif");

                g.drawImage(img, 0, 0, null);
            }
        }

        panel_image.setPreferredSize(new Dimension(694, 697));

        this.getContentPane().add(panel_image);
        this.getContentPane().add(panel_mainMenu, BorderLayout.SOUTH);

        // Show the frame
        this.pack ();
        this.show ();
    }
}

class ButtonsPanel extends JPanel {
   private JButton newGameButton;
   private JButton loadGameButton;
   private JButton highScoresButton;
   private JButton exitGameButton;

   public ButtonsPanel() {
      super(new GridLayout(1, 4));
     
      newGameButton = new JButton("New Game");
      loadGameButton = new JButton("Load Game");
      highScoresButton = new JButton("High Scores");
      exitGameButton = new JButton("Exit"):
     
      add(newGameButton);
      add(loadGameButton);
      add(highScoresButton);
      add(exitGameButton);
     
      pack();
   }
   
   public JButton getNewGameButton() {
      return newGameButton;
   }
   
   public JButton getLoadGameButton() {
      return loadGameButton;
   }
   
   public JButton getHighScoresButton() {
      return highScoresButton;
   }
   
   public JButton getExitGameButton() {
      return exitGameButton;
   }
}


I say this in place of an immediate answer to your problem because you really have some issues with program design that you need to consider. Such liberal use of "static" is usually a sign that something is not right.
evogre3n




PostPosted: Sat Dec 24, 2005 6:47 pm   Post subject: (No subject)

Hey,

thanks a lot for that Smile

Its interesting what youve done, are there any particular benifits of sticking the panels into their own class?

And also

The program wont work, it doesnt seem to find
code:
pack();
within the ButtonsPanel class

Crying or Very sad
evogre3n




PostPosted: Sat Dec 24, 2005 6:54 pm   Post subject: (No subject)

where in the heck is the edit post button anyway :S

Also, could you explain the use of:

code:

    public JButton getNewGameButton ()
    {
        return newGameButton;
    }


    public JButton getLoadGameButton ()
    {
        return loadGameButton;
    }


    public JButton getHighScoresButton ()
    {
        return highScoresButton;
    }


    public JButton getExitGameButton ()
    {
        return exitGameButton;
    }
wtd




PostPosted: Sat Dec 24, 2005 7:40 pm   Post subject: (No subject)

evogre3n wrote:
Hey,

thanks a lot for that Smile

Its interesting what youve done, are there any particular benifits of sticking the panels into their own class?

And also

The program wont work, it doesnt seem to find
code:
pack();
within the ButtonsPanel class

Crying or Very sad


Ah yes. That's a brain hiccup on my part.
wtd




PostPosted: Sat Dec 24, 2005 7:43 pm   Post subject: (No subject)

evogre3n wrote:
where in the heck is the edit post button anyway :S

Also, could you explain the use of:

code:

    public JButton getNewGameButton ()
    {
        return newGameButton;
    }


    public JButton getLoadGameButton ()
    {
        return loadGameButton;
    }


    public JButton getHighScoresButton ()
    {
        return highScoresButton;
    }


    public JButton getExitGameButton ()
    {
        return exitGameButton;
    }


These are accessors. They allow me to gain access to the buttons from outside of a ButtonsPanel object. If I just made the buttons themselves public, I'd have to deal with the possibility that somenody could do something like:

code:
myButtonsPanel.exitGameButton = new JButton("foo");


As for wrapping all of this up into a separate class... it keeps things cleaner, and more controlled.
evogre3n




PostPosted: Sat Dec 24, 2005 7:46 pm   Post subject: (No subject)

ahh

Alright, thats interesting, I must say although Java is OOP, the way we've been learning it is very "procedural".

Anyway thanks for that about the accessors, learn something new every day Smile

Looking forward to your message about the painting in the frame Smile

Thanks a lot Very Happy
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sat Dec 24, 2005 7:48 pm   Post subject: (No subject)

evogre3n wrote:
ahh

Alright, thats interesting, I must say although Java is OOP, the way we've been learning it is very "procedural".


Then, with all due respect, your teacher has done you a disservice.
wtd




PostPosted: Sat Dec 24, 2005 8:14 pm   Post subject: (No subject)

What happens if you explicitly call the image panel's paintComponent method in the constructor for MainMenu?
evogre3n




PostPosted: Sun Dec 25, 2005 9:19 am   Post subject: (No subject)

nope, not working
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: