I tried to do this:
Path pathForSpriteSheet = Paths.get("/PicFolder/TheSpriteSheet.png");
Then I put it in this method like this:
loadImage(pathForSpriteSheet);
public void loadImage(BufferedImage image){
theImage = image;
}
It says "The method loadImage(BufferedImage) in the type BufferedImageLoader is not applicable for the arguments"
To further what Tom said, you cannot pass a Path variable to the method loadImage when it requires a BufferedImage. What you need to do is create a BufferedImage based off of the path you already have and send that to the method. Something like:
BufferedImage img = ImageIO.read(pathForSpriteSheet.toFile());
loadImage(img);
Related
I think I kind of reinvent caching in Java but have a point I don't get further.
In case the answer is anywhere on Stackoverflow for this issue I might had not understood it when searching or didn't understand the required complexity and searched for a more easy way.
Short what I want to do: call a method on an Object. The object should load a picture and store it as Image. Then it should decorate itself with an Decorator so that called method will next time only return the image with no more IO operations.
My Interace Picture Interafce is simple like this:
import java.awt.*;
public interface PictureInterface {
public Image getImage();
}
My Decorator looks like this:
import java.awt.*;
public class PictureDecorator implements PictureInterface {
private final Picture p;
public PictureDecorator(Picture p){
this.p = p;
}
public Image getImage(){
return this.p.pipeImage();
}
}
It saves a Picture and on getImage() calls pictures pipeImage - the picture "real" getImage().
And last but not least the Picture Class:
import java.awt.Image;
public class Picture implements PictureInterface{
private final String path;
private final Image image;
public Picture(String path){
this.path = path;
}
private void loadImage(){
this.image = /*IO Magic Loading the Image from path*/
}
public Image getImage() {
loadImage();
/*Decorate Yourself with Picture Decorator*/
return /*Decorator.getImage*/;
}
Image pipeImage(){
return this.image;
}
}
If getImage is called I want Picture to Decorate itself and call the Decorators getImage and most importent overwrite its old refference (Java is call by value, this is where i'm stuck atm) so on further getImage Calls the Decorators getImage Method is called.
As a little extra-question I think my access to the mage from Decorator is not best practice, hints welcome ^^
EDIT:
To add a thing: I allready thought if this it not possible: what would be "smarter": go for if(image==NUll) or make a decorateYourself() function where image is loaded and decorator returned in Picture and in Decorator it only returns itself, apply this to the Image var and then call getImage, like:
ImageInterface x = new Image("path);
x = x.decorateYourself()
Image i = x.getImage()
this ways i would only do a method-call to return the decorator itself, but i have to call both methods ...
If getImage is called i want Picture to Decorate itself and call the
Decorators getImage and most importent overwrite its old refference
(Java is call by value, this is where i'm stuck atm) so on further
getImage Calls the Decorators getImage Method is called.
A decorator doesn't work in this way.
With decorator you want to augment or diminish a behavior of an existing class without being invasive for this class : no needed modification.
So the decorator instance decorates an object that has to share with the decorator class a common type and a common method.
Besides I don't think that you need to use a decorator.
Here you don't decorate a picture but you bypass its loading if it was already previously performed.
I think that it would be more suitable to use a proxy that decides whether it must load the resources of get it from the cache.
Don't worry, it doesn't change many things in the classes you have introduced: interface, common method and object wrapping are still required.
In your case PictureInterface is the common type between the proxy class and the proxy subjects classes that provides the common method : getImage().
import java.awt.*;
public interface PictureInterface {
public Image getImage();
}
PictureProxy, a proxy class could implement PictureInterface to act as any PictureInterface instances.
PictureProxy should be responsible to check if it has cached the result of a previous loading of the image. It is the case it returns it. Otherwise it calls getImage() on the Picture instance that holds and it caches the result.
import java.awt.*;
public class PictureProxy implements PictureInterface {
private final Picture p;
private final Image image;
public PictureProxy(Picture p){
this.p = p;
}
public Image getImage(){
if (image != null){
return image;
}
image = p.getImage();
return image;
}
}
And Picture class should not be aware of the proxy when it performs getImage().
It is the proxy class that handles the state of the cache.
import java.awt.Image;
public class Picture implements PictureInterface{
private final String path;
private final Image image;
public Picture(String path){
this.path = path;
}
private void loadImage(){
this.image = /*IO Magic Loading the Image from path*/
}
public Image getImage() {
loadImage();
return image;
}
}
From the client of the classes you could do something like that :
Picture picture = new PictureProxy(new Picture("picturePath"));
Image img = picture.getImage(); // load the image from Picture the first time and get it
Image img = picture.getImage(); // get it from the PictureProxy cache
I have one default image and I want to replace the image when the user or the program already selected an image. I have only the basic image reader for displaying the default image.
private static void loadImage()throws Exception{
File image2 = new File("...Example\\blackimage.jpg");
bi = ImageIO.read(image2);
}
You could override methods so
private static void loadImage(String imagePath) throws Exception {
File image2 = new File(imagePath);
bi = ImageIO.read(image2);
}
private static void loadImage() throws Exception {
loadImage("...Example\\blackimage.jpg");
}
This would give you two methods, one to call if you have a image in mind and one for the default image.
If your program already has one selected for a particular user, for example stored in some sort of local storage / database, it can call the first method, however if an image is not found it can call the default no parameter method.
I am stuck with a downcasting issue with java.
Here is the plot:
The class MyPicture extends the abstract class BufferedImage. The point is to add a few methods to BufferedImage. Then, I have the class MyWindow which sets a user-friendly window. In this class, I want to load a picture in MyPic, copy it in MyPic_filtered, use a method used in MyPicture on MyPic_filtered and finally show MyPic and MyPic_filtered in separated windows (but this last part is OK ^^).
I don't know which types I should use for MyPic and MyPic_filtered. I tried the cast them in the right type, it builds but doesn't run.
Here is the code:
//Loading the picture
BufferedImage MyPic = ImageIO.read(new File(URL)); //URL is a string
//Copy the picture
MyPicture myPic_filtered = myPic;
//Use the method from MyPicture
myPic_filtered.method_from_MyPicture();`
Could someone help me please?
You can add a caster when you are trying to pass a base class instance to extended one, like:
MyPicture myPic_filtered = (MyPicture)myPic;
Then you can access "myPic" by using this keyword.
Or maybe you do not need to extend the BufferedImage. Just treat bufferedImage as an instance variable, like:
class MyPicture {
BufferedImage bi;
//other variables
......;
public MyPicture(BufferedImage input) {
this.bi = input;
}
public BufferedImage method_from_MyPicture() {
//Do something with bi and output
........
}
}
Not sure which structure is better. But it solves the problem in either way.
I'm beginner to java GUI. And want to access images from the folder but i'm getting the following error.
My Code
import java.awt.Image;
import javax.swing.ImageIcon;
public class Images {
private static String IMG_FOLDER = "C:/Users/RASHID/workspace/images/";
public static Image ICON = getImage(IMG_FOLDER + "icon.png");
private static Images instance;
private Images() {}
public static Images getInstance() {
if(instance==null)
instance = new Images();
return instance;
}
public static Image getImage(String image){
return getImageIcon(image).getImage();
}
public static ImageIcon getImageIcon(String image){
return new ImageIcon(getInstance().getClass().getClassLoader().getResource(image));
}
}
When i try to run this one in main i get the following Errors. I don't know whats happening here.
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at Images.getImageIcon(Images.java:38)
at Images.getImage(Images.java:34)
at Images.<clinit>(Images.java:9)
You don't use classloaders to fetch files from hard drive. Instead, you need to fetch them as Files and transform to Images first:
File sourceimage = new File("c:\\mypic.jpg");
Image image = ImageIO.read(sourceimage);
return new ImageIcon(image);
(taken directly from this site - take a look)
You are trying to construct ImageIcon object using a constroctur that takes URL paramter (because getResource() method returns URL object, and by the way in this case it returns null, hence NullPointerException)
You should use ImageIcon(String filename) constructor instead, which will create ImageIcon from the file specified.
Read from Local Folder
File sourceimage = new File("c:\\picture_name.jpeg");
Image image = ImageIO.read(sourceimage);
So for my 2D game I want to use an image to represent a player but when I try to use an image, it tells me that it couldn't be "instantiated". I don't know what that means:
public class PlayerOne extends Entity{
private Image img = new Image();
[...]
#Override
public void render(Graphics g){
g.drawImage( img , x, y, Color.BLUE, new ImageObserver());
}
}
I tried it in another class with BufferedImages but that somehow doesn't work.
So it can't create Objects of neither Image nor the ImageObserver. Does anyone know a fix for this error ?
You cannot instantiate an abstract class. Please see link.
Following syntax would be operational:
private Image image = new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);
Also see link for more information on BufferedImage. Additionally, here is a tutorial which ilustrates an example implementation;
You should have an image file (*.png for example ) to start with. Then use
Image img = new ImageIcon("img.png").getImage();