I have a code which extends from the Canvas class. I use two instances of this class. Each displays an image, what image is that refreshes about once a second. Now, when I use only one of them it functions correctly but while using both of them, at random they display the other Canvas's image.
Any help is appreciated
package com.imagedisplay;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.Color;
import java.io.File;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.ButtonGroup;
import javax.swing.JMenuBar;
import javax.swing.KeyStroke;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JFileChooser;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.AbstractAction;
import javax.swing.event.MenuListener;
import javax.swing.event.MenuEvent;
import java.awt.image.BufferedImage;
import com.gamedeconstructor.image.ImageEngine;
import com.util.Timer;
public class View3d extends Canvas
{
//the current rendering.
BufferedImage bImage;
//the parent panel, its width and height used.
JPanel parent;
public View3d(JPanel inParent)
{
setupBufferStrategy();
parent = inParent;
revalidate();
repaint();
}
public void setImage(BufferedImage inImage)
{
bImage = inImage;
render();
}
@Override
public void paint(Graphics g)
{
super.paint(g);
render();
}
public void render()
{
BufferStrategy bs = this.getBufferStrategy();
if(bImage!=null)
{
Graphics g = bs.getDrawGraphics();
BufferedImage scaled = ImageEngine.scale(bImage,parent.getWidth(),parent.getHeight());
setSize(parent.getWidth(),parent.getHeight());
g.drawImage(scaled,0,0,null);
g.dispose();
bs.show();
}
}
public void setupBufferStrategy()
{
BufferStrategy bs = this.getBufferStrategy();
if(bs == null)
{
this.createBufferStrategy(1);
return;
}
}
}
CodePudding user response:
The problem turned out to be in a different area of the code.
Command line was used by java and the output of the commands was stored on the hard drive which then had to be read by java again. While process.waitfor was called, this still did not stop a FileIO.read call from reading the previous image. Once I added a FileIO.delete command the problem disapeared. Now I would expect an exception instead of this problem being solved, because if the program could reach the previous image, having only a p.waitfor call to prevent that, than now that it is deleted, and it is too early in its attempt to read the image file. It should fail to find the file. but anyway, it works.
Thank you for your times.