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