Reloaded file not change in java - java

Im try to load image with file chooser,
but when I updated image after loaded, and reload it, image is not change. It just show first time loaded.
(I tryed not use filechooser)
Load-> change image -> Load -> image not change
Load-> change image ->Exit Program ->open program -> Load -> image change
How can I clear it?
Load Image
int ret = chooser.showOpenDialog(null);
if(ret == JFileChooser.APPROVE_OPTION){
String filePath = chooser.getSelectedFile().getPath();
ImageIcon icon = new ImageIcon(filePath);
main.ChangeImage(icon);
}
Change Image
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(image.getImage(), 0, 0, this);
}
public void ChangeImage(ImageIcon img) {
image = img;
origin_image = image;
origin_height = image.getIconHeight();
origin_width = image.getIconWidth();
this.setBounds(0, 0, image.getImage().getWidth(null), image.getImage()
.getHeight(null));
repaint();
}

Java tends to cache images.
If the images change during run-time, one way to fool the methods into reloading it is to read the bytes of the file yourself, form an input stream from them (using ByteArrayInputStream) and use the input stream in ImageIO.read(InputStream).
The reason this works is that if an URL or File is used to load the image, the toolkit uses the location as a key to cache it.
Other notes
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
// ..
Should be:
public void paintComponent(Graphics g) {
super.paintComponent(g); // paint the border and BG to avoid arttifacts!
Graphics2D g2 = (Graphics2D) g;
// ..

Related

How to draw transparent image in java with BufferedImage?

I'm willing to show an transparent image on a canvas in java with the help of graphics2D and BufferedImage.
Here is the code which loads image.
private static BufferedImage sprites,board;
public static void load(){
try {
board = new BufferedImage(100,100,BufferedImage.TYPE_INT_ARGB);
board = ImageIO.read(new File("res/chesssprite.png"));
} catch (IOException ex) {
Logger.getLogger(SpriteManager.class.getName()).log(Level.SEVERE, null, ex);
}
}
and here is the code which renders the image
public void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
Graphics2D g2d = (Graphics2D) g;
{
g2d.setColor(new Color(150,150,150));
g2d.fillRect(0,0,getWidth(), getHeight());
g2d.setComposite(AlphaComposite.Src);
g2d.drawImage(board,0,0,null);
}
g = g2d;
g.dispose();
bs.show();
}
I have search on net a lot but didn't come with an solution. If anyone knowns how to fix this.
Here is the image..
And here is how output looks like
Okay whoever facing these kind of problem:
Make sure the image is transparent. Test it in image viewer.
Remove the line g2d.setComposite(AlphaComposite.Src); This line adds alpha composites which make every transparent pixel black.

Load Image to JPanel

I want to load an image to JPanel. The images that are going to be drawn are images saved from this JPanel.
For example i have this picture that has been capture from the JPanel and later i want to load that image to the same JPanel.
I have tried this but it does not work. This piece of code is inside a class that extends JPanel. Any suggestions?
public void load(String path) throws IOException {
BufferedImage img = ImageIO.read(new File(path));
Graphics2D g2d = img.createGraphics();
g2d.drawImage(img, 0, 0, null);
this.repaint();
}
You draw the image back to itself (?) using a Graphics object derived from the Image itself. Instead store the image to a field, not a local variable, and draw that image within the JPanel's paintComponent method. Most important, have a look at the Swing graphics tutorials
private BufferedImage img;
public void load(String path) throws IOException {
img = ImageIO.read(new File(path));
this.repaint();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (img != null) {
g.drawImage(img, 0, 0, null);
}
}
you can do that by an override
public void paintComponent(Graphics g) {...} for javax.swing components
and public void paint(Graphics g) for java.awt components

The dicom bufferedImage from pixelmed is having low contrast

I would like to display a DICOM image in my java program. I am using pixelmed. However, I found that i cant correctly display the correct contrast. The contrast is too low.
Here is my code:
(SourceImage is a class provided by PixelMed, chosenImageFile.getPath() is just the path of the DICOM File.)
SourceImage dimg = new SourceImage(chosenImageFile.getPath());
BufferedImage image = dimg.getBufferedImage();
BufferedImage source = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = source.createGraphics();
g2d.drawImage(image, 0, 0, null);
dicomImgDisplayer1.setImage(source);
dicomImgDisplayer1 is an class extend JPanel. setImage() of this JPanel class will call the setImage() of an JFrame class.
The JFrame class's setImage() code:
public void setImage(BufferedImage image) {
this.image = image;
setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
repaint();
revalidate();
}
public void paint(Graphics graphics) {
Graphics2D g2d = (Graphics2D) graphics;
g2d.drawImage(image, null, 0, 0);
}
Is that something wrong with the color model? Please help. Thanks.
Does your image have a prescribed window width / window center? Be sure you set that (or allow the user to adjust it). See SingleImagePanel - there are some static methods to apply windowing to your buffered image.

Can't get a JPEG Image to display in JFrame

I am trying to get a background Image to display on a JFrame for a small game I'm programming. The image is a .jpg and I keep getting these errors:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
at web.game.Base.<init>(Base.java:45)
at web.game.Base.main(Base.java:61)
And my code is:
Image myimage;
public Base() {
ImageIcon ii = new ImageIcon(this.getClass().getResource("myimage.jpg"));
myimage = ii.getImage();
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(myimage, 10, 10, null);
There are many reasons you might have problems. The most obvious begin, the image doesn't exist (or at least doesn't exist where you think it does)
private BufferedImage myImage;
public Board() {
try {
myimage = ImageIO.read(this.getClass().getResource("/myimage"));
} catch (IOException exp) {
exp.printStackTrace();
}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (myImage != null) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(myimage, 10, 10, this);
System.out.println("Background Image");
}
}
You may find Performing Custom Painting and Reading/Loading an Image of some use.
When using getResource you have two (basic) choices. Relative or absolute.
With a relative path, the class loader will start searching based on the context of the class. So if you image is in the root folder of your project, but your class is in sub folder (of packages), then a relative path won't work, because the class loader will start search from where the class resides.
An absolute path will allow you to search from the root of the classpath, which is going to be more helpful, especially in your case.
Try something like this and see what you get..
System.out.println(TestReference.class.getResource("myimage.jpg"));
System.out.println(TestReference.class.getResource("/myimage.jpg"));

Java: Load image from file, edit and add to JPanel

I want to load an image from my computer into 2D Graphics so that I can edit it afterwards and then I want to add it to JPanel. If you need to see my project I can send it to you.
void loadImage()
{
FileDialog fd = new FileDialog(new Frame(), "Please choose a file:", FileDialog.LOAD);
fd.show();
if (fd.getFile() != null)
{
File fil = new File(fd.getDirectory(), fd.getFile());
strDirectory = fd.getDirectory();
strFileType = fd.getFile();
mainImage.setIcon(new ImageIcon(fil.toString()));
getFileList(strDirectory);
checkFileType(strFileType);
}
}
Thanks in advance
To load your image into the memory, you can use ImageIO.read(File). To edit it afterwards, obtain a Graphics2D instance from it by calling createGraphics():
BufferedImage img = ImageIO.read(yourFile);
Graphics2D g = img.createGraphics();
// Draw here on the graphics
g.dispose();
You can even turn on anti-alias by setting a RenderingHint before drawing:
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIASING_ON);
Then, to add it to a JPanel, create your custom JComponent and add an instance of that component to your JPanel:
public class JImageComponent extends JComponent
{
private BufferedImage img;
public JImageComponent(BufferedImage bi)
{
img = bi;
}
#Override
public void paintComponent(Graphics g)
{
g.drawImg(img, 0, 0, this);
}
}
please read this tutorials about Icon in Swing and your Image/ImageIcon would by placed to the JLabel, this way eliminated all troubles came from paint/paintComponents ...
For image loading you should use ImageIO object with method read(File file) see docs. Then you will get BufferedImage instance of which you can make your changes through Graphics2D instance which you'll obtain by calling createGraphics() on the image instance see docs. Last thing, override method paintComponent() from JPanel or better JComponent see docs and there you can draw your image on Graphics instance which you'll get as parameter in paintComponent(Graphics g) method by calling drawImage(Image img, int x, int y, ImageObserver observer) see docs where ImageObserver set to null.

Categories

Resources