Results 1 to 8 of 8
  1. #1
    smacker is offline Member
    Join Date
    Jan 2011
    Posts
    46
    Rep Power
    0

    Default rescaling an image

    hello ,
    i'm trying to make a small game ,
    and wanted to do it full screen with an image in the background
    also wanted to rescale it to fit the screen if it's too big or too small
    and that's what i'm having trouble with

    afterward i would like to add some buttons on it so if it's not the right way to go please let me know
    here's what i did
    Java Code:
        public Main() {
            initComponents();
           
            this.setExtendedState(Main.MAXIMIZED_BOTH);
    
       
            ImageIcon icon = new ImageIcon("image/main.jpg");
            Image image = icon.getImage().getScaledInstance(1000, 1000, Image.SCALE_DEFAULT);
            icon.setImage(image);
           JLabel pic = new JLabel(new ImageIcon(image));
           this.setContentPane(pic);
           this.pack();
        }
    for now i managed to make the window maximize but the picture doesn't come up ,
    before i tried rescale it showed the picture but off-scale

    thanks for all the helpers !

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: rescaling an image

    Can't tell much without the rest of your code. Did you setVisible(true)? Also, is your image size really x by x? I suggest you open the image and read it to get the actual size so you can preserve the aspect ratio.

    Jim
    Last edited by jim829; 03-04-2013 at 02:56 AM. Reason: added suggestion
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    smacker is offline Member
    Join Date
    Jan 2011
    Posts
    46
    Rep Power
    0

    Default Re: rescaling an image

    well , it's not x on x i'm just trying it out , if i want it to be on the whole screen size is there a var that will tell me how much ?

    that's pretty much the whole code the rest is netbeans automated
    the problem now is the jframe is full-screen but the picture is no where to be found..

    full code :

    Java Code:
    import java.awt.Image;
    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Level;
    
    import java.awt.Image;
    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();
         
           this.setExtendedState(Main.MAXIMIZED_BOTH);
           ImageIcon icon = new ImageIcon("image/main.jpg");
           Image image = icon.getImage().getScaledInstance(1300, 500, Image.SCALE_DEFAULT);
           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() {
    
            jButton1 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowActivated(java.awt.event.WindowEvent evt) {
                    formWindowActivated(evt);
                }
            });
    
            jButton1.setText("Full Screen");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            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(46, 46, 46)
                    .addComponent(jButton1)
                    .addContainerGap(269, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(123, 123, 123)
                    .addComponent(jButton1)
                    .addContainerGap(154, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>
    
        private void formWindowActivated(java.awt.event.WindowEvent evt) {
            // TODO add your handling code here:
            
        }
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent 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.JButton jButton1;
        // End of variables declaration
    }
    Last edited by smacker; 03-04-2013 at 03:38 AM. Reason: wrong code

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: rescaling an image

    Are you able to display the picture without rescaling it? In other words, are you sure that you're able to get the image in the first place? I usually test this by creating a small program that does nothing more than grab an image, put it into an ImageIcon and then displays the ImageIcon in a JOptionPane, and that's it, essentially nothing more than a main method with 3-4 lines of code.

  5. #5
    smacker is offline Member
    Join Date
    Jan 2011
    Posts
    46
    Rep Power
    0

    Default Re: rescaling an image

    ahh it works , the folder wasn't the right one :|
    sould start the try and catch for exeptions , or stop working on 4 am ..

    thanks to all helpers

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: rescaling an image

    To get your screen size, you can use:

    Java Code:
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize()
    However, your image may not fill your screen exactly without distortion. If the ratio of your screen width and height (or desired window width and height) equals the ratio of your image width and height then you are in luck as you can scale with minimal or no distortion. Otherwise, you are going to have to crop the image.

    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: rescaling an image

    I'm not sure, but I think the Rectangle returned by Toolkit#getScreenSize() includes desktop decorations such as a taskbar. To get the usable part of the screen, try
    Java Code:
    new Frame().getMaximizedBounds()
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    smacker is offline Member
    Join Date
    Jan 2011
    Posts
    46
    Rep Power
    0

    Default Re: rescaling an image

    oolkit.getDefaultToolkit().getScreenSize() worked for me , thanks

    but now i'm trying to put a jbutton on the picture and it won't show the button :\
    any ideas ?

Similar Threads

  1. Replies: 4
    Last Post: 08-09-2012, 07:18 PM
  2. Replies: 1
    Last Post: 07-28-2011, 09:48 AM
  3. ReScaling of Log axis using JFreeChart
    By The Vicky in forum AWT / Swing
    Replies: 1
    Last Post: 03-22-2011, 08:17 AM
  4. Replies: 14
    Last Post: 04-10-2009, 09:46 AM
  5. Replies: 0
    Last Post: 01-08-2008, 06:29 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
  •