简体   繁体   中英

JFrame not displaying all images in fullscreen

So, I am working on a project which requires switching between images. The images need to be in fullscreen mode. There seem to be 2 problems that I am facing. First is with the image switching. When I switch between images, some images appear ok when I switch. Others dont seem to show up on the screen at all. I just seem to be getting an empty frame. Second is that the right key seems to work everytime but just for my sanity, I have put system prints. The system outs dont seem to show up on the console but it switches images in the frame(Even though, I get an empty frame sometimes). Any suggestions/solutions would be highly appreciated.

Note about the code: The draw strings that I have are for testing purposes of the coordinates. I am using an Eyetribe, so just to show where I am looking. The drawstring seems to work perfectly. Also, I am calling switchImage very quickly, almost 22 times in a second. Could that be an issue? Although it makes me wonder why it works for some images and not for others. UPDATE: The problem seems to be in g.drawImage. It doesnt seem to be drawing for some images but I can't seem to figure out why thats happening. Currently this is my code for fullscreen images.

public void showFrame(){

    //jL -> JLabel
    //jF -> JFrame
    //jP -> Panel
    jF.setTitle("Test");
    jF.setUndecorated(true);
    jF.setResizable(false);
    jF.setVisible(true);

    Toolkit tk = Toolkit.getDefaultToolkit();
    int xsize = (int)tk.getScreenSize().getWidth();
    int ysize = (int)tk.getScreenSize().getHeight();
    jF.setSize(xsize, ysize);
    jF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    if(testImagesList != null){
        img = new ImageIcon(testImagesList.get(0));
    }
    Image imag = img.getImage();
    bi = new BufferedImage(1280, 800, BufferedImage.TYPE_INT_ARGB);
    Graphics g = bi.createGraphics();
    g.drawImage(imag, 0, 0, 1280, 800, null);

    ImageIcon newIcon = new ImageIcon(bi);
    jL.setIcon(newIcon);
    jF.add(jL);
    jP.add(jL);
    jF.add(jP);
    jF.validate();  
}

To switch between images I am using a key listener. The keyListner calls the switch image code. Both are given below.

public void switchImage(ImageIcon image, JFrame jF, JLabel jL, JPanel jP, GazeData gazeData){
    Image imag = image.getImage();

    BufferedImage bi = new BufferedImage(1280, 800, BufferedImage.TYPE_INT_ARGB);
    Graphics g = bi.getGraphics();

    g.drawImage(imag, 0, 0, 1280, 800, null);
    g.setColor(Color.black);
    int x = (int) gazeData.smoothedCoordinates.x;
    int y = (int) gazeData.smoothedCoordinates.y;
    Font font = new Font("Verdana", Font.BOLD, 16);
    g.setFont(font);


    String text = "hello(" + gazeData.smoothedCoordinates.x + gazeData.smoothedCoordinates.y + ")";
    g.drawString(text, x, y);
    g.drawString("Fixed Coordinate at (400, 500)", 400, 500);
    ImageIcon newIcon = new ImageIcon(bi);
    jL.setIcon(newIcon);
    jP.add(jL);
    jF.validate();  
}

@Override
public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    if(e.getKeyCode() == KeyEvent.VK_RIGHT){
        //  j.switchImage(image, jF, jL, jP, gazeData);
        System.out.println("RightDetected");
        imageIndex++;

        ImageIcon newImage = new ImageIcon(imageList.get(imageIndex));

        if(newImage != null){
            this.currentImage = newImage;
            System.out.println("IMAGE SWITCHED!!!! Current Image="+imageList.get(imageIndex));
            j.switchImage(currentImage, j.jF, j.jL, j.jP, currentGazeData);


        }

    }
}

Probably won't fix your problem but you have incorrect Swing coding practices:

jL.setIcon(newIcon);
//jF.add(jL); // remove
jP.add(jL);
jF.add(jP);
jF.validate(); 

A Swing component can only have a single parent. First you add the label to the frame and the to the panel. So the label will only ever appear on the panel. Get rid of the first statement.

All the Swing components should be added to the frame BEFORE the frame is made valid. So get rid of the validate() statement and move the setVisible(...) to the bottom.

jL.setIcon(newIcon);
//jP.add(jL);
//jF.validate();  

When you change a property of a Swing component the component is smart enough to repaint itself. All you need to do is change the icon of the label. There is no need to add the label back to the panel or validate() the frame.

Others dont seem to show up on the screen at all

Maybe the image isn't found?

The drawstring seems to work perfectly.

Instead of creating all the BufferedImages and doing custom painting you can just display a label on top your label.

So the basic code would be:

imageLabel = new JLabel();
imageLabel.setLayout( new FlowLayout() );
textLabel = new JLabel();
imageLabel.add(textLabel);
frame.add(imageLabel);

Note:

  1. The imageLabel can be added directly to the frame. I don't see any reason for your panel.
  2. To change the image you just invoke the setIcon() method on the imageLabel
  3. To change the text your just invoked the setText() method on the textLabel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM