Question

For some reason, only 1/5 (or less) of each image is displayed. Since it is a Layered Pane at least 1 should be fully displayed. Layout is null because this is a learning program Why is this happening?

Here is the code: Class test1

import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.awt.Image;
import java.net.URL;


public class test extends JPanel
{
    private JFrame frame=new JFrame("");
    private JLayeredPane layered =new JLayeredPane();
    private BufferedImage image;
    private test2 image2=new test2();



    public test()
    {
         try
        {               
       image=ImageIO.read(new File("im1.png"));    

        }
        catch (IOException e)
        {
            //Nothing
            e.printStackTrace();
        }           
    }

    public void draw()
    {
        frame.setLayout(null);
        image2.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setResizable(true);
        frame.setSize(800,800);
        frame.add(layered);
        this.setBounds(30,50,70,80);
        layered.add(this,new Integer(1));
        image2.setBounds(100,120,170,400);
        layered.add(image2,new Integer(1000));
        frame.setContentPane(layered);
        frame.setVisible(true); 
   }

    @Override
    protected void paintComponent(Graphics g)
    {
      super.paintComponent(g);
      g.drawImage(image,0,0,null);
    }
    public static void main()
    {
        test t=new test();
        t.draw();
    }
}

Class test2

import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.awt.Image;
import java.net.URL;


public class test2 extends JPanel
{
    private JPanel thepanel=new JPanel();;
    private BufferedImage image2;



    public test2()
    {
      try{image2=ImageIO.read(new File("im2.png"));}
      catch(IOException e){e.printStackTrace();}
    }

    @Override
    protected void paintComponent(Graphics g)
  {    super.paintComponent(g);
       g.drawImage(image2,0,0,this);
  }
 }

I also do not get what setOpaque(true/false) exactly is and when it is necessary.

Was it helpful?

Solution

Only 1/5 of each image is displayed as you're using a null layout and and setting the bounds of the containers that paint the images to sizes smaller then the images

this.setBounds(30,50,70,80);

and

image2.setBounds(100,120,170,400);

Use a layout manager, add the JPanels test and test2 to the JFrame and invoke JFrame#pack. Ensure that both panels have a preferred size by overriding getPreferredSize.

Aside: Java naming conventions show that class names start with uppercase letters such as Test and Test2.

OTHER TIPS

setOpaque(false) basically will remove the background of the Jcomponent, essentially making it have no background, so if you have one component on top of another then by called setOpaque on the top component will make the bottom visible

Check out this for more detailed information on what setOpaque() does

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