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

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

问题描述

所以,我正在研究一个需要在图像之间切换的项目。图像需要处于全屏模式。似乎有2个我面临的问题。
首先是图像切换。当我在图像之间切换时,切换时某些图像显示正常。其他人似乎根本不显示在屏幕上。我似乎正在得到一个空框架。
其次,右键似乎每次都能正常工作,但为了我的理智,我已经放置了系统打印。该系统似乎不在控制台上显示,但它切换帧中的图像(即使我有时会得到一个空帧)。
任何建议/解决方案将受到高度赞赏。



关于代码的注意事项:我的绘制字符串用于测试坐标。我正在使用Eyetribe,只是为了显示我在看什么。抽绳似乎完美。
另外,我正在很快地调用switchImage,几乎每秒调用22次。这可能是一个问题吗?虽然这让我想知道为什么它适用于某些图像而不适用于其他图像。
更新:问题似乎在g.drawImage中。它似乎没有绘制一些图像,但我似乎无法弄清楚为什么会发生这种情况。
目前这是我的全屏图片代码。

  public void showFrame(){

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

工具包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); (testImagesList!= null){
img = new ImageIcon(testImagesList.get(0));



if
}
Image imag = img.getImage();
bi = new BufferedImage(1280,800,BufferedImage.TYPE_INT_ARGB);
图形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();

$ / code>

要在图像间切换,我正在使用一个按键侦听器。 keyListner调用切换图像代码。两者都在下面给出。

  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);
图形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(固定坐标在(400,500),400,500);
ImageIcon newIcon = new ImageIcon(bi);
jL.setIcon(newIcon);
jP.add(jL);
jF.validate();

$ b @Override
public void keyPressed(KeyEvent e){
// TODO自动生成的方法存根
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);

$ b}

}
}


<解决方案可能不会解决您的问题,但您的Swing编码实践不正确:

 

code> jL.setIcon(newIcon);
//jF.add(jL); //删除
jP.add(jL);
jF.add(jP);
jF.validate();

Swing组件只能有一个父对象。首先将标签添加到框架和面板。所以标签只会出现在面板上。摆脱第一条语句。



所有Swing组件都应该在框架生效之前添加到框架中。所以摆脱validate()语句并将setVisible(...)移动到底部。

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

当您更改Swing组件的属性时,该组件足够聪明以重新绘制自己。所有你需要做的就是改变标签的图标。没有必要将标签添加回面板或验证()框架。


其他人似乎不显示在屏幕上


也许图像没有找到?



而不是创建所有BufferedImages并进行自定义绘画,您可以只显示一个

所以基本的代码是:

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

注意:


  1. imageLabel可以直接添加到框架中。
  2. 要更改图像,您只需调用imageLabel上的setIcon()方法

  3. 要更改您刚调用textLabel上的setText()方法的文本


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

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

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