Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Display image inside JPanel inside JScrollPane

807580Feb 24 2010 — edited Feb 24 2010
I'm trying to get it to paint the full image inside the scroll pane, so it fits in the program (300x300 or so) and the image is 1000x1000 or bigger and i want to be able to scroll around on it. It's just painting the outline of the image for some reason. I have no idea how to do it using netbeans GUI builder, which is what i used for the other classes in the program and the main frame for this one, so i attempted to do it manually but that my be the problem.
package rs;

import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class MapDisplay extends javax.swing.JFrame {

    private final Image map;

    public MapDisplay(final String mapPath) {
        map = grabImage(mapPath);
        initComponents();

        JPanel panel = new JPanel() {

            {
                setSize(map.getWidth(null), map.getHeight(null));
            }

            @Override
            public void paint(Graphics g) {
                g.drawImage(map, 0, 0, null);
            }
        };
        mapPane.add(panel);

    }

    public Image grabImage(String path) {
        return new ImageIcon(path).getImage();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        mapPane = new javax.swing.JScrollPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mapPane, javax.swing.GroupLayout.DEFAULT_SIZE, 645, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mapPane, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MapDisplay("C:\\world.png").setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JScrollPane mapPane;
    // End of variables declaration
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 24 2010
Added on Feb 24 2010
3 comments
1,681 views