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 4 of 4

Thread: Help with making a small game

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with making a small game

    hello ,
    i'm trying to make a small game ,
    i never done this before and got mixed up in the menus
    i tried to make a background image and it worked
    now i'm trying to put a jbutton on the background picture
    and it won't show i'm guessing it's not on top
    any ideas how to solve it ?

    here's what i did to get the background image
     
    public class Main extends javax.swing.JFrame {
     
        /**
         * Creates new form Main
         */
        public Main() {
           initComponents();
           jToggleButton1.setVisible(true);
     
           Dimension d= java.awt.Toolkit.getDefaultToolkit().getScreenSize();
           this.setExtendedState(Main.MAXIMIZED_BOTH);
           ImageIcon icon = new ImageIcon("images/c.jpg");
           Image image = icon.getImage();
           image = getScaledImage(image ,d.width ,d.height);
           icon.setImage(image);
           JLabel pic = new JLabel(new ImageIcon(image));      
           this.setContentPane(pic);
           this.pack();
        }

    thanks to all helpers !


  2. #2
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: Help with making a small game

    The posted code does not compile without errors.
    Can you make a small simple program that compiles, executes and shows the problem?

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

    Default Re: Help with making a small game

    yes the full code is

     
    import java.awt.*;
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
     
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author PIL
     */
    public class Main extends javax.swing.JFrame {
     
        /**
         * Creates new form Main
         */
        public Main() {
           initComponents();
           jToggleButton1.setVisible(true);
     
           Dimension d= java.awt.Toolkit.getDefaultToolkit().getScreenSize();
           this.setExtendedState(Main.MAXIMIZED_BOTH);
           ImageIcon icon = new ImageIcon("images/c.jpg");
           Image image = icon.getImage();
           image = getScaledImage(image ,d.width ,d.height);
           icon.setImage(image);
           JLabel pic = new JLabel(new ImageIcon(image));      
           this.setContentPane(pic);
           this.pack();
        }
     
        /**
         * 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() {
     
            jLayeredPane1 = new javax.swing.JLayeredPane();
            jToggleButton1 = new javax.swing.JToggleButton();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowActivated(java.awt.event.WindowEvent evt) {
                    formWindowActivated(evt);
                }
            });
     
            jToggleButton1.setText("jToggleButton1");
            jToggleButton1.setBounds(150, 90, 105, 23);
            jLayeredPane1.add(jToggleButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
            );
     
            pack();
        }// </editor-fold>                        
     
        private void formWindowActivated(java.awt.event.WindowEvent evt) {                                     
            // TODO add your handling code here:
     
        }                                    
     
        /**
         * @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(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Main.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 Main().setVisible(true);
     
     
                }
     
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JLayeredPane jLayeredPane1;
        private javax.swing.JToggleButton jToggleButton1;
        // End of variables declaration                   
     
     private Image getScaledImage(Image srcImg, int w, int h){
        BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = resizedImg.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(srcImg, 0, 0, w, h, null);
        g2.dispose();
        return resizedImg;
    }}

    only thing you need is the images folder and a c.jpg file in it

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with making a small game

    sure ,
    all u need is an "images" folder and a "c.jpg" file in it
    the full code :

     
     
    import java.awt.*;
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
     
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author PIL
     */
    public class Main extends javax.swing.JFrame {
     
        /**
         * Creates new form Main
         */
        public Main() {
           initComponents();
           jToggleButton1.setVisible(true);
     
           Dimension d= java.awt.Toolkit.getDefaultToolkit().getScreenSize();
           this.setExtendedState(Main.MAXIMIZED_BOTH);
           ImageIcon icon = new ImageIcon("images/c.jpg");
           Image image = icon.getImage();
           image = getScaledImage(image ,d.width ,d.height);
           icon.setImage(image);
           JLabel pic = new JLabel(new ImageIcon(image));      
           this.setContentPane(pic);
           this.pack();
        }
     
        /**
         * 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() {
     
            jLayeredPane1 = new javax.swing.JLayeredPane();
            jToggleButton1 = new javax.swing.JToggleButton();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowActivated(java.awt.event.WindowEvent evt) {
                    formWindowActivated(evt);
                }
            });
     
            jToggleButton1.setText("jToggleButton1");
            jToggleButton1.setBounds(150, 90, 105, 23);
            jLayeredPane1.add(jToggleButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
            );
     
            pack();
        }// </editor-fold>                        
     
        private void formWindowActivated(java.awt.event.WindowEvent evt) {                                     
            // TODO add your handling code here:
     
        }                                    
     
        /**
         * @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(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Main.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 Main().setVisible(true);
     
     
                }
     
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JLayeredPane jLayeredPane1;
        private javax.swing.JToggleButton jToggleButton1;
        // End of variables declaration                   
     
     private Image getScaledImage(Image srcImg, int w, int h){
        BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = resizedImg.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(srcImg, 0, 0, w, h, null);
        g2.dispose();
        return resizedImg;
    }}

    thanks again

Similar Threads

  1. Making a New Game Help
    By AndyLun in forum Java Code Snippets and Tutorials
    Replies: 2
    Last Post: December 5th, 2012, 03:25 AM
  2. Making Game, Need 1-3 People That Know JAVA
    By Stilez in forum Paid Java Projects
    Replies: 2
    Last Post: August 27th, 2012, 03:51 PM
  3. Game for Small Children Program
    By Javazoid in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2012, 04:02 PM
  4. Making a Craps game.
    By SOK in forum Object Oriented Programming
    Replies: 1
    Last Post: March 6th, 2010, 08:23 PM
  5. Theory behind 2d Game making?
    By DarrenReeder in forum Java Theory & Questions
    Replies: 3
    Last Post: January 28th, 2010, 02:54 AM