Question

So my problem is i'm painting to many images to see and i can't get the JScrollPane bars to show up so i can look though all the images.

My code is this.

You will need a image 40 by 40 or get a error. The file name are "Wall.gif", "DefualtTileBackup.gif", "Character.gif", "metal3.jpg" and "DefualtTileBackup.gif"

import java.awt.*;
import java.util.*;
import javax.swing.*;

import java.net.*;

public class DrawImage extends JPanel {
    private Image image;
    Toolkit tk = Toolkit.getDefaultToolkit();
    private char[][] fieldMap = {
            {'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
            {'$','S','M','@','Z','#','#','#','#','#','#','#','#','#','#','#','#','$'},
            {'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
            {'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
            {'$','S','M','@','Z','#','#','#','#','#','#','#','#','#','#','#','#','$'},
            {'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
            {'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
            {'$','S','M','@','Z','#','#','#','#','#','#','#','#','#','#','#','#','$'},
            {'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
            {'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
            {'$','S','M','@','Z','#','#','#','#','#','#','#','#','#','#','#','#','$'},
            {'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
            {'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
            {'$','S','M','@','Z','#','#','#','#','#','#','#','#','#','#','#','#','$'},
            {'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
            {'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
            {'$','S','M','@','Z','#','#','#','#','#','#','#','#','#','#','#','#','$'},
            {'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
            {'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
            {'$','S','M','@','Z','#','#','#','#','#','#','#','#','#','#','#','#','$'},
            {'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
            {'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},
            {'$','S','M','@','Z','#','#','#','#','#','#','#','#','#','#','#','#','$'},
            {'$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$','$'},

        };
    public static void main(String[] args) {
        JFrame frame = new JFrame("DrawImage");
        frame.setSize(600,600);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JScrollPane(new DrawImage()));
    }

public DrawImage() {
    super();
    setVisible(true);   

}


private URL getURL(String filename) {
    URL url = null;
    try {
        url = this.getClass().getResource(filename);
    }

    catch (Exception e) { }
    return url;
}
public void paint(Graphics g) {

    //create an instance of Graphics2D
    Graphics2D g2d = (Graphics2D) g;
    //fill the background with black
    g2d.setColor(Color.GREEN);
    g2d.fillRect(0, 0, getSize().width, getSize().height);

    //draw the image
    for(int K = 0; K < this.fieldMap.length ; K = K+1){
    for(int J = 0; J < this.fieldMap[K].length; J = J + 1){
    g2d.drawImage(imagePlacer(this.fieldMap[K][J]),(J*40),(K*40), this );
        } // loop Y end
    }// loop X end
}// paint end

public Image imagePlacer(char tiles){


            try {
                switch(tiles){

                case('$'):
                    System.out.println("Wall, $");
                     image = tk.getImage(getURL("Wall.gif"));
                     break;


                case('F'):
                    System.out.println("Floor, #");
                     image = tk.getImage(getURL("DefualtTileBackup.gif"));
                     break;


                case('@'):
                    System.out.println("Character, @");
                     image = tk.getImage(getURL("Character.gif"));
                     break;


                case('M'):

                    System.out.println("Monster, !");
                     image = tk.getImage(getURL("metal3.jpg"));
                     break;

                default:
                        System.out.println("defualt,?");
                        image = tk.getImage(getURL("DefualtTileBackup.gif"));
                      break;

                    // end default
                }// end switch
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    return image;
}//end method
}// end class

The program seems to be working correctly but I don't get scroll bars.

Was it helpful?

Solution

You need to provide a size for the panel. As far as I can see the panel doesn't contain any other components, thus the layoutmanager would normally try to fit it into the available space.

If I counted correctly you have a 18x18 grid, thus the size of your panel would need to be at least 720x720 (I assume you have 18x18 tiles of 40x40 images). Try calling setMinimumSize(new Dimension(720,720)) on your panel.

Edit:

As @camickr correctly pointed out, JScrollPane will use the preferred size of the panel. Thus you might try to either set it externally (setPreferredSize(...)) or, since you're already extending the panel and the subclass has the information it needs, override the panel's getPreferredSize() to return the calculated preferred size.

OTHER TIPS

another issues

1) please change public void paint(Graphics g) { to the public void paintComponent(Graphics g) {,

for painting to the Swing JComponent use method paintComponent()

2) frame.setVisible(true); must be last code line in the main method

3) better would be put Image as Icon to the JLabel and by using GridLayout place to the JPanel

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top