0

I have a program that should load 2 panels next to each other. On one I show a grid of 10x10 images using an array of panels:

private JPanel imagePanels[][] = new JPanel[10][10];

Then I load a custom panel with the image like this:

imagePanels[i][j] = new ImagePanel("https://i.stack.imgur.com/W3RMa.jpg",Color.WHITE);

This is the Image I tried to load (32x32 pixels):

enter image description here

ImagePanel:

public class ImagePanel extends JPanel{
    private BufferedImage image;
    public ImagePanel(String path,Color background){ //TODO!!:set background color
        try{
            URL url = new URL(path);
            image =  = ImageIO.read(url);
        }catch(IOException ex){
            System.out.println("Image Not Found");
        }
    }
    @Override
    protected void paintComponent(Graphics g){
        super.paintComponents(g);
        g.drawImage(image,0,0,null);
    }
}

This works and the panel shows the 32x32 pixel image that I passed it. Now I want to display another image inside another panel. I decided to use the ImagePanel class as a GoTo class for loading images on panels throughout the program. So I made another class for the second panel:

public class LeftPlayerPanel extends JPanel{
    private JPanel PlayerPicturePanel = new ImagePanel("https://i.stack.imgur.com/sjfvC.jpg",Color.GRAY);
    public LeftPlayerPanel(Player thisPlayer){
        this.add(PlayerPicturePanel);
        //TODO!!:Display Player Stats
    }

}

This is the image that I tried to load this time (128x128 pixels):

enter image description here

The program however looks like this:

enter image description here

As you can see the right side works fine showing the entire image 100 times.(don't worry about the red dot. that's what it's supposed to do) but on the left of the program the image only shows up partially (a little bit of the left top corner.)

Can there be a problem in my layout? Do I have to make another class for each image? Or for each image size? is this happening because I use a SplitPane?

Here is the class that displays the stuff:

public class test {
    private static Positition p;
    public static void main(String[] v){
        Field f = new Field(2, 100);
        p = f.getRandomFieldPosition();
        printPosition(p);

        JPanel panel = new FieldPanel(p); 
        JPanel player = new LeftPlayerPanel(null);// add player later
        JFrame f1 = new TestFrame("TESTFRAME");

        panel.setPreferredSize(new Dimension(326,309));
        player.setPreferredSize(new Dimension(135,309));

        JSplitPane splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,player,panel);


        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());
        content.add(splitpane,BorderLayout.CENTER);

        f1.setContentPane(content);
        f1.setPreferredSize(new Dimension(326+135,309));
        f1.pack();
        f1.show();
    }
}

ps. I'm still working on the exact sizing of the program and the size of each panel. I know that the way it's displayed now it's not going to fit but I also tried sliding the SplitPane to the right to make the panel bigger.

EDIT: I made a version that has all the important stuff in one file but I can't test it because i'm on a proxy and it won't load the images here. (I think I got the url right.)

URL for the LeftPlayerPanel: https://i.stack.imgur.com/sjfvC.jpg

URL for the LoadSquare method: https://i.stack.imgur.com/W3RMa.jpg

BRHSM
  • 854
  • 3
  • 13
  • 48
  • 5
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Feb 24 '16 at 09:02
  • 1
    I will try to get an SSCCE up as soon as posibile but why should I chainge the image to one for that q&a? – BRHSM Feb 24 '16 at 09:29
  • 1
    *"why should I chainge the image to one for that q&a?"* Change it to an URL to one of those images linked above, and use that URL instead of the file mentioned in `image = ImageIO.read(new File(path));` and the **example will work for *other* people** who don't have that image located in `Recourses/Images/PlayerTile.jpg`. ..And BTW - did you mean `Recourses` (not a word, though 'recourse' is) or `Resources` there? – Andrew Thompson Feb 24 '16 at 10:16
  • I don't know how to make it work with an url. Can you fix that for me. Just as an edit here so others can reproduce? – BRHSM Feb 24 '16 at 11:06
  • The accepted answer to that question links to a number of MCVEs, but most of them simply generate the image. On the other hand [this answer](http://stackoverflow.com/a/18825844/418556) includes a self contained example (an MCVE) that *shows how to create an URL and use it to load the image* in the first two lines of the `main` method. – Andrew Thompson Feb 24 '16 at 11:41
  • I will try to edit them ASAP – BRHSM Feb 24 '16 at 12:11
  • `I'm still working on the exact sizing of the program and the size of each panel.` - Each component should determine its own preferred size. You should NOT be using the "setPreferredSize()" method. Instead of doing custom painting you can just use a JLabel with an IconImage and the label will determine its own preferred size. If you do custom painting, then you need to override the `getPreferredSize()` method to return the size of the image. – camickr Feb 24 '16 at 15:58
  • `super.paintComponents(g);` - get rid of the "s". The method you invoke should be the same as the method your override. – camickr Feb 24 '16 at 15:59
  • @camickr how did that not error in the first place haha. I will get to to it – BRHSM Feb 24 '16 at 16:01
  • @AndrewThompson got you some URL's. can you see if this works? i don't have internet at the moment (only on my phone) – BRHSM Feb 25 '16 at 10:17
  • 2
    *..got you some URL's. can you see if this works?" Mix those URLs into a single MCVE and let me know when it is ready. It should include imports, a `main` method, the image loading etc. - it must be 'complete'. – Andrew Thompson Feb 25 '16 at 15:27
  • @AndrewThompson I got it ready but's it's long and I could not test it because I don't have internet on my pc (posting from phone) – BRHSM Mar 02 '16 at 08:59
  • *"I got it ready but's it's long.."* What are you referring to? Code? If it is a [mcve] it will, by definition, be **minimal**. While some MCVEs of useful problems might go to several hundred lines of code, please be sure that everything irrelevant to seeing the problem on-screen has been removed. – Andrew Thompson Mar 02 '16 at 09:03
  • There might be 1 or 2 lines left of which i'm not shure. But it's highly optimized in comparising to the full code. Mostly because i crushed everything into one file – BRHSM Mar 02 '16 at 10:14
  • Your code makes no sense. Your `Position` class is can't be used for anything (`private fields`) and it isn't being used anywhere anyway. Correct your code, post it in this question by editing it (remove everything that is not the question), and then use the @ symbol to ping someone in the comments. – user1803551 Jul 01 '16 at 15:40

0 Answers0