JFrame没有全屏显示所有图像 [英] JFrame not displaying all images in fullscreen

查看:52
本文介绍了JFrame没有全屏显示所有图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在做一个需要在图像之间切换的项目.图像需要处于全屏模式.我似乎面临两个问题.首先是图像切换.当我在图像之间切换时,切换时有些图像看起来不错.其他人似乎根本没有出现在屏幕上.我似乎只是得到一个空框架.其次是正确的密钥似乎每次都有效,但只是为了我的理智,我已经放置了系统打印.系统输出似乎没有出现在控制台上,但它会在框架中切换图像(尽管有时我会得到一个空框架).任何建议/解决方案将不胜感激.

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.

关于代码的注意事项:我拥有的绘制字符串用于测试坐标.我正在使用 Eyetribe,所以只是为了显示我在看哪里.束带似乎工作得很好.另外,我调用 switchImage 的速度非常快,几乎每秒 22 次.这可能是个问题吗?虽然这让我想知道为什么它适用于某些图像而不适用于其他图像.更新:问题似乎出在 g.drawImage 中.它似乎没有为某些图像绘制,但我似乎无法弄清楚为什么会这样.目前这是我的全屏图像代码.

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();  
}

为了在图像之间切换,我使用了按键监听器.keyListner 调用切换图像代码.两者都在下面给出.

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);


        }

    }
}

推荐答案

可能无法解决您的问题,但您的 Swing 编码实践不正确:

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(); 

一个 Swing 组件只能有一个父级.首先,将标签添加到框架和面板.所以标签只会出现在面板上.去掉第一个语句.

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.

所有的 Swing 组件都应该在框架生效之前添加到框架中.所以去掉 validate() 语句,把 setVisible(...) 移到底部.

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();  

当您更改 Swing 组件的属性时,该组件足够智能以重新绘制自身.您需要做的就是更改标签的图标.无需将标签添加回面板或 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

可能找不到图片?

拉绳似乎工作得很好.

您无需创建所有 BufferedImages 并进行自定义绘制,只需在标签顶部显示一个标签即可.

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

所以基本代码是:

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

注意:

  1. imageLabel 可以直接添加到框架中.我认为您的面板没有任何理由.
  2. 要更改图像,您只需在 imageLabel 上调用 setIcon() 方法
  3. 要更改您刚刚在 textLabel 上调用 setText() 方法的文本

这篇关于JFrame没有全屏显示所有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆