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

Thread: Java Image Displayer

  1. #1
    Member coderxx0's Avatar
    Join Date
    Feb 2013
    Location
    England, UK
    Posts
    58
    My Mood
    Daring
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Java Image Displayer

    I am having trouble with my code it is a java image dis-player and i cannot get it to work andi am completely stuck can somebody helpme

    /*
       Copyright 2013 Matthew Kowal 
    */
     
    package GUI;
     
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.Graphics2D;
     
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    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.swing.GroupLayout;
    import javax.swing.GroupLayout.Alignment;
     
     
    @SuppressWarnings("serial")
    public class MainAppFrame extends JFrame {
     
        private JPanel contentPane;
        File targetFile;
        BufferedImage targetImg;
        public JPanel panel,panel_1;
        private static final int baseSize = 128;
        private static final String basePath =![enter image description here][1]
                "C:\\Documents and Settings\\Administrator\\Desktop\\Images";
     
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        MainAppFrame frame = new MainAppFrame();
                        frame.setVisible(true);
                        frame.setResizable(false);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
     
        /**
         * Create the frame.
         */
        public MainAppFrame() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 550, 400);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(new BorderLayout(0, 0));
     
            panel = new JPanel();
            panel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
            contentPane.add(panel, BorderLayout.WEST);
     
            JButton btnBrowse = new JButton("Browse");
            btnBrowse.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    browseButtonActionPerformed(e);
                }
            });
     
            JLabel lblSelectTargetPicture = new JLabel("Select target picture..");
     
            JButton btnDetect = new JButton("Detect");
            btnDetect.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                }
            });
     
            JButton btnAddDigit = new JButton("Add Digit");
            btnAddDigit.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                }
            });
     
            JButton button = new JButton("Recognize");
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                }
            });
     
            panel_1 = new JPanel();
            panel_1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
     
            GroupLayout gl_panel = new GroupLayout(panel);
            gl_panel.setHorizontalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                    .addGroup(gl_panel.createSequentialGroup()
                        .addGap(6)
                        .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                            .addGroup(gl_panel.createSequentialGroup()
                                .addComponent(lblSelectTargetPicture)
                                .addGap(6)
                                .addComponent(btnBrowse))
                            .addGroup(gl_panel.createSequentialGroup()
                                .addGap(10)
                                .addComponent(btnDetect)
                                .addGap(18)
                                .addComponent(btnAddDigit))))
                    .addGroup(gl_panel.createSequentialGroup()
                        .addGap(50)
                        .addComponent(button))
                    .addGroup(gl_panel.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 182, GroupLayout.PREFERRED_SIZE))
            );
            gl_panel.setVerticalGroup(
                gl_panel.createParallelGroup(Alignment.LEADING)
                    .addGroup(gl_panel.createSequentialGroup()
                        .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                            .addGroup(gl_panel.createSequentialGroup()
                                .addGap(7)
                                .addComponent(lblSelectTargetPicture))
                            .addGroup(gl_panel.createSequentialGroup()
                                .addGap(3)
                                .addComponent(btnBrowse)))
                        .addGap(18)
                        .addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 199, GroupLayout.PREFERRED_SIZE)
                        .addGap(22)
                        .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                            .addComponent(btnDetect)
                            .addComponent(btnAddDigit))
                        .addGap(18)
                        .addComponent(button)
                        .addContainerGap())
            );
     
            panel.setLayout(gl_panel);
        }
        public BufferedImage rescale(BufferedImage originalImage)
        {
            BufferedImage resizedImage = new BufferedImage(baseSize, baseSize, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = resizedImage.createGraphics();
            g.drawImage(originalImage, 0, 0, baseSize, baseSize, null);
            g.dispose();
            return resizedImage;
        }
        public void setTarget(File reference)
        {
            try {
                targetFile = reference;
                targetImg = rescale(ImageIO.read(reference));
            } catch (IOException ex) {
                Logger.getLogger(MainAppFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
     
            panel_1.setLayout(new BorderLayout(0, 0));
            panel_1.add(new JLabel(new ImageIcon(targetImg))); 
            setVisible(true);
        }
        private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {
            JFileChooser fc = new JFileChooser(basePath);
            fc.setFileFilter(new JPEGImageFileFilter());
            int res = fc.showOpenDialog(null);
            // We have an image!
            try {
                if (res == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
                    setTarget(file);
                } // Oops!
                else {
                    JOptionPane.showMessageDialog(null,
                            "You must select one image to be the reference.", "Aborting...",
                            JOptionPane.WARNING_MESSAGE);
                }
            } catch (Exception iOException) {
            }
     
        }
    }
     
     
     
     
    //JPEGImageFileFilter.java
     
    package GUI;
     
    import java.io.File;
     
    import javax.swing.filechooser.FileFilter;
     
    /*
    * This class implements a generic file name filter that allows the listing/selection
    * of JPEG files.
    */
    public class JPEGImageFileFilter extends FileFilter implements java.io.FileFilter
     {
     public boolean accept(File f)
       {
       if (f.getName().toLowerCase().endsWith(".jpeg")) return true;
       if (f.getName().toLowerCase().endsWith(".jpg")) return true;
       if(f.isDirectory())return true;
       return false;
      }
     public String getDescription()
       {
       return "JPEG files";
       }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Java Image Displayer

    What do you mean "cannot get it to work" ?
    What is it supposed to do?

  3. #3
    Member coderxx0's Avatar
    Join Date
    Feb 2013
    Location
    England, UK
    Posts
    58
    My Mood
    Daring
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Java Image Displayer

    Quote Originally Posted by jps View Post
    What do you mean "cannot get it to work" ?
    What is it supposed to do?
    it is suppose to display JPG images when i run it just shuts down

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Java Image Displayer

    Use println statements to determine which lines of code are executing and which ones are not. This will help in finding clues to the cause of the problem.
    Where does it "shut down" ? ...and what does "shut down" mean? Does it close the application or the JVM? Is there an exception or any error message?
    As much as we might like to we do not have time to compile and run every code with troubles, it serves you best to provide as much information about the problem as possible in your first post.

  5. #5
    Member coderxx0's Avatar
    Join Date
    Feb 2013
    Location
    England, UK
    Posts
    58
    My Mood
    Daring
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Java Image Displayer

    Quote Originally Posted by jps View Post
    Where does it "shut down" ? ...and what does "shut down" mean? Does it close the application or the JVM? Is there an exception or any error message?
    As much as we might like to we do not have time to compile and run every code with troubles, it serves you best to provide as much information about the problem as possible in your first post.
    it shuts the application down and doesn't give an error message sorry i am quite new at this so if it makes no scene i am sorry

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Java Image Displayer

    The main method is called first, so start there. Right after this line of code:
    public static void main(String[] args) {
    add this line of code:
    System.out.println("main()");
    From there step through the flow of the program, adding more printlns each step of the way, and see how far it goes before things happen different than what you expected.
    Then explain here what happened and what you expected to happen.

  7. #7
    Member coderxx0's Avatar
    Join Date
    Feb 2013
    Location
    England, UK
    Posts
    58
    My Mood
    Daring
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Java Image Displayer

    Quote Originally Posted by jps View Post
    The main method is called first, so start there. Right after this line of code:
    public static void main(String[] args) {
    add this line of code:
    System.out.println("main()");
    From there step through the flow of the program, adding more printlns each step of the way, and see how far it goes before things happen different than what you expected.
    Then explain here what happened and what you expected to happen.
    ok thanks i will give it a go

Similar Threads

  1. Image Doesn't Display with Qualified Image Path????
    By swaginator in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 31st, 2012, 12:29 AM
  2. Create image Jpeg from an object of Image class.
    By Ramandeep in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 31st, 2011, 10:34 PM
  3. Replies: 2
    Last Post: February 14th, 2011, 04:36 PM
  4. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 09:50 AM
  5. Replies: 2
    Last Post: June 29th, 2009, 03:06 PM