Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Trying to add a button to my Title Screen...

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trying to add a button to my Title Screen...

    Alright, so I'm not too terribly fluent in Java.

    I've been working on a small game to help me pick up the language, and I've been (for some time now) trying to figure out how to implement a title screen. I've tackled numerous issues with it this far and, well, feel proud of myself for that. xD But I can't seem to figure out this small thing.

    I'm trying to place buttons onto my canvas. However, when I launch the game and it goes to the title screen, the buttons are no where to be seen. Changing the JFrame to allow re-sizing reveals that the buttons are being displayed under everything else on my canvas. Which, I'm going to assume, has something, if not everything, to do with my render methods.

    The title screen is composed of two images being repeated painted to the canvas via a render method. So those images are being repainted over and over again, which means, while I have the sequence of the code correct, the repainting is simply covering up the buttons. P

    So my question is this; How might I go about displaying the buttons ABOVE those rendering images? Or can I at all? And, if I can't, how can I paint the images statically so that they don't keep repainting?

    If this has already been asked, then I apologize, and I thank you for taking the time to read this.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trying to add a button to my Title Screen...

    You should extend JPanel, override paintComponent, do your drawing in that method, and add your JButton to the JPanel using a LayoutManager.

    Otherwise, I recommend posting an SSCCE that demonstrates exactly what you're doing.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to add a button to my Title Screen...

    Yeah...

    This is what I'm trying to do:
    import java.applet.*;
    import java.awt.*;
     
    public class Component extends Applet implements Runnable{
        private static final long serialVersionUID = 1L;
     
        public static int winDimX = 800;
        public static int winDimY = 600;
     
        public static int winDimXDef = 800;
        public static int winDimYDef = 600;
     
        public static Dimension size = new Dimension(winDimX, winDimY);
     
        public static Font titleFont = new Font("Fixedsys",Font.TRUETYPE_FONT,10);
     
        public static String windowTitle = "Blockles";
     
        public static boolean isRunning = false;
        public static boolean threadActive = false;
     
        public static boolean isOnTitle = false;
        public static boolean isRunningTitle = false;
     
        private Image mainTitle;
     
        public static titleScreen title;
     
        public Component(){
        	setPreferredSize(size);
        	//Left Empty for SSCCE
        }
     
        @Override
        public void start(){
        	//Left Empty for SSCCE
        }
     
        public void startTitle(){
            requestFocus();
     
            isRunningTitle = true;
     
            if(!threadActive){
                new Thread(this).start();
                threadActive = true;
            }
        }
     
        @Override
        public void stop(){
            isRunning = false;
            isRunningTitle = false;
        }
     
        //Program execution path begins here
        public static void main(String[] args){
            //Calls Title Screen Constructor
            title = new titleScreen();
        }
     
        //Methods removed for SSCCE
     
        public void renderTitle(){
            Graphics g = mainTitle.getGraphics();
     
            //Drawing Title Screen
            title.render(g);
     
            g = getGraphics();
     
            g.drawImage(mainTitle, 0, 0, size.width, size.height, 0, 0, size.width, size.height, null);
     
            g.dispose();
        }
     
        @Override
        public void run(){
            mainTitle = createVolatileImage(size.width, size.height);
     
            //WHILE loop removed for SSCCE
     
            while(isRunningTitle){
                renderTitle();
     
                try{
                    Thread.sleep(5);              
                } catch(Exception e) {
                    System.err.println("Exception occurred while attempting Thread.sleep! \n" + e + "\n");
                }                        
            } 
        }
    }

    And then the actual title screen class:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
     
    public class titleScreen{
     
        public static BufferedImage titleBackground;
        public static BufferedImage titleLogo;
     
        public static Font titleFont = new Font("Verdana",Font.BOLD,16);
     
        public static JFrame frame;
     
        public titleScreen(){
        	Component component = new Component();
     
            //This I/O Code removed for purposes of SSCCE
     
            frame = new JFrame();
            frame.setResizable(true);
            frame.add(component);
            frame.pack();
     
            frame.setTitle(Component.windowTitle);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
     
            JButton startButton = new JButton("Start");//The JButton name.
            startButton.setBounds(10, 10, 100, 40);
            frame.add(startButton);//Add the button to the JFrame.
     
            component.startTitle();
        }
     
        public void render(Graphics g){
            g.drawImage(titleScreen.titleBackground, 0, 0, Component.winDimXDef, Component.winDimYDef, null);
            g.drawImage(titleScreen.titleLogo, 14, 0, Component.winDimXDef - 25, Component.winDimYDef - 420, null);
     
            g.setFont(titleFont);
            g.setColor(Color.WHITE);
            g.drawString("Copyright © 2013 Jacob Keep. Please do not reproduce. :(", (Component.size.width / 2) - 395, Component.size.height - 6);
        }
     
        //This method removed for purposes of SSCCE
    }

    As you can see, if you drag the frame's edges and make them larger, you can see the button underneath. (I wasn't going to worry about doing setBounds until after I got the layering right.)

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Trying to add a button to my Title Screen...

    I see many problems with your code including
    • using static variables where not needed,
    • Getting the Graphics object by calling getGraphics() on a component rather than drawing in a JComponent's paintComponent method
    • Mixing AWT with Swing components
    • Mixing applets with JFrames
    • Using absolute positioning rather than layout managers
    • Giving your class the same name as a core Java class, Component


    I suggest you read the Oracle Swing tutorial drawing section, and that you scrap this code above and re-start from a pre-thought out plan rather than by free association,...

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to add a button to my Title Screen...

    Okay.. well I am a beginner, so, I suppose that's to be expected. :3 (That's what I get for listening to YouTube.)

    So, yeah, I will do that.

    One thing I do want to ask, though, is how are you seeing my code in the first place? o_0 I submitted it, and it said it would be approved for viewing by a moderator. And, I still don't see the post myself.. strange.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Trying to add a button to my Title Screen...

    You should see it now.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Title screen Glitch (Cant Bust ERROR)
    By ErrorBuster in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 3rd, 2012, 02:45 PM
  2. Radio button .. display empty screen ?? plz Help
    By smasm in forum AWT / Java Swing
    Replies: 4
    Last Post: April 7th, 2012, 07:21 PM
  3. Adding add/remove button to add/remove tab
    By JMtrasfiero in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 27th, 2012, 11:24 AM
  4. Trying to add a close button
    By coyboss in forum Java Theory & Questions
    Replies: 5
    Last Post: February 12th, 2011, 03:28 PM
  5. need to add a Double Button to the code
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: August 5th, 2010, 11:19 AM