Results 1 to 4 of 4
  1. #1
    Dark Knight is offline Member
    Join Date
    Feb 2014
    Posts
    20
    Rep Power
    0

    Default Trying to create my own program with great difficulty.

    Hi guys,

    I Was wondering if any of you could help me out. Im trying to create a program, I thought it was a simple enough program but my Lack of Java knowledge is showing and I am having difficulty achieving what I Want. Basically my aim is this: I want to create a program which draws lottery numbers. I started with creating a new Jframe form so as I could use the GUI to place the button. I then have 49 Images for the numbers ranging 1-49 stored in a resources folder. What I want to do is simply draw 1 random image from the resources folder every time the button is pressed and display it on the screen. Firstly I will link my overall code, most of which has been generated automatically as I have used a JFrame form.

    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package lottonumberdraw;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.io.*;
    import javax.imageio.*;
    import javax.swing.*;
    /**
     *
     * @author Caitlyn
     */
    public class LottoGui extends javax.swing.JFrame {
        
        /**
         * Creates new form LottoGui
         */
        public LottoGui() {
            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() {
    
            jPanel1 = new javax.swing.JPanel();
            jButton1 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jButton1.setText("jButton1");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addContainerGap(210, Short.MAX_VALUE)
                    .addComponent(jButton1)
                    .addGap(27, 27, 27))
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addContainerGap(204, Short.MAX_VALUE)
                    .addComponent(jButton1)
                    .addGap(30, 30, 30))
            );
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(23, 23, 23)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(67, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(19, 19, 19)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(75, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
        public void displayImage(){
         JLabel image1;   
         BufferedImage img = null;
         
            
            try {
               //img = ImageIO.read(new File("resources/images/numbers/2.png"));
               img = ImageIO.read(getClass().getResource("/resources/images/numbers/2.png"));
           } catch (IOException e) {
               System.out.println("error is "+ e);
           }
           image1 = new JLabel(new ImageIcon(img)) ;
           image1.setOpaque(true);
           jPanel1.add(image1);
           image1.setVisible(true);
           jPanel1.setVisible(true);
          this.repaint();
        }
        
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
           displayImage();
            
            
        }                                        
    
        /**
         * @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(LottoGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(LottoGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(LottoGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(LottoGui.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 LottoGui().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JPanel jPanel1;
        // End of variables declaration                   
    }

    The two bits of code I have added are:

    Java Code:
    public void displayImage(){
         JLabel image1;   
         BufferedImage img = null;
         
            
            try {
               //img = ImageIO.read(new File("resources/images/numbers/2.png"));
               img = ImageIO.read(getClass().getResource("/resources/images/numbers/2.png"));
           } catch (IOException e) {
               System.out.println("error is "+ e);
           }
           image1 = new JLabel(new ImageIcon(img)) ;
           image1.setOpaque(true);
           jPanel1.add(image1);
           image1.setVisible(true);
           jPanel1.setVisible(true);
          this.repaint();
        }
        
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
           displayImage();
            
            
        }
    I am getting no errors when the button is pressed but the image is also not getting displayed. Is it not possible to mix a jFrameform with other code that displays components as the code generated when a button is placed via the GUI is not editable or am I just being stupid.

    Any help would be greatly appreciated.
    Last edited by Dark Knight; 03-23-2014 at 10:42 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Trying to create my own program with great difficulty.

    Can you make a small, complete program that compiles, executes and shows the problem? It shouldn't use 49 images. 2 images would be enough for testing.

    I have no idea what the IDE generated code does, but it could be part of the problem.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Dark Knight is offline Member
    Join Date
    Feb 2014
    Posts
    20
    Rep Power
    0

    Default Re: Trying to create my own program with great difficulty.

    Thanks for the response, unfortunately if I dont use the GUI to add the components I cant even get them to display on a screen by just writing the code as I cant even figure out what goes where. It seems like an awful lot of code just to get a button to display on a screen :( JFrames, JPanels, then the button and then I need to know about layouts etc just to get my button positioned where I want it on the screen and then the question of doe this code go inthe main method? a separate method etc. Just getting slightly frustrated that I am struggling with even the basic things after I have already read the tutorials. Thanks for the response though.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Trying to create my own program with great difficulty.

    Not many people like working with the IDE's generated code.

    It will be better in the long run if you learn how to build a GUI yourself.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Having difficulty executing a series of loops in program
    By serenityN0W in forum New To Java
    Replies: 16
    Last Post: 07-28-2011, 01:54 PM
  2. difficulty making java program, Help Me
    By 3c4hy0 in forum New To Java
    Replies: 11
    Last Post: 08-13-2010, 12:08 PM
  3. Replies: 2
    Last Post: 08-13-2010, 11:42 AM
  4. Replies: 8
    Last Post: 11-24-2009, 09:03 PM
  5. need help to create a GUI for this program
    By placidphyll in forum AWT / Swing
    Replies: 1
    Last Post: 11-17-2009, 04:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •