Ok so I got a static ImageIcon and the Image just doesn't show up. In the same program I use other ImagesIcon but they are not static so when I declare them I do it like this:
public ImageIcon blabla = new ImageIcon(getClass().getResource(blabla.png));
But if I declare a ImageIcon Static I cannot use that line since one cannot get acces to getClass() from a static value. Right now those images are not showing up using this:
public static ImageIcon blabla = new ImageIcon(blabla.png);
Thanks for your help!
public static ImageIcon networkOfflineIcon = new ImageIcon("Images/networkOfflineIcon.png");
public static ImageIcon networkIcon = new ImageIcon("Images/networkIcon.png");
protected static JMenuItem jmiRemote = new JMenuItem(" Remote", networkOfflineIcon);
//************************************************************************
public static void changeNetWorkStatus(boolean network_status)
//************************************************************************
{
if(network_status){
Application.jmiRemote.setIcon(networkIcon);
Application.jmiRemote.setText("NetWork Online/Remote is On");
Application.lockScreenRemote();
}else if(!network_status){
Application.jmiRemote.setIcon(networkOfflineIcon);
Application.jmiRemote.setText("NetWork Offline/Remote is Off");
Application.unlockScreenRemote();
}
}//DOESNT CHANGE THE IMAGE
//************************************************************************
In a static context, you can write:
public ImageIcon imageIcon = new ImageIcon(MyClass.class.getResource("icon.png"));
Or, alternatively try ImageIO.read(new File("icon.png"))
Related
I'm trying to call GoogleCloudVision to get the image labels into an array. I don't know what to do for the ObjectLabel method which seems to be the problem. The program is meant to take an image file and run it through googles cloud vision and get an array of labels for the image, then based on the array determine what is the image.
this is the error I'm getting.
java.nio.file.NoSuchFileException: C:\Users\DELL Laptop\Downloads\Exam3Starter\src
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:85)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
at java.base/sun.nio.fs.WindowsDirectoryStream.<init>(WindowsDirectoryStream.java:86)
at java.base/sun.nio.fs.WindowsFileSystemProvider.newDirectoryStream(WindowsFileSystemProvider.java:533)
at java.base/java.nio.file.Files.newDirectoryStream(Files.java:544)
at GoogleCloudVision.findJsonCredentialsFile(GoogleCloudVision.java:51)
at GoogleCloudVision.detectImageLabels(GoogleCloudVision.java:80)
at SeeFood.labelImage(SeeFood.java:57)
at SeeFood.main(SeeFood.java:83)
Code:
public static void labelImage(String filename) throws IOException, InterruptedException {
// TODO: replace with your implementation
String userFile = filename;
JFrame img = new JFrame();
ImageIcon icon = new ImageIcon(userFile);
JLabel label = new JLabel(icon);
img.add(label);
img.pack();
img.setVisible(true);
ArrayList<ObjectLabel> labels = GoogleCloudVision.detectImageLabels(userFile);
JOptionPane.showMessageDialog(null, labels);
} // end labelImage
public class ObjectLabel {
String Label = "";
float confidence = 2.0F;
public ObjectLabel(String description, float score) {
}
}
import java.awt.*;
import javax.swing.*;
public class O2BIntro extends Open2Beat {
private JLabel screenGraphic;
private ImageIcon introBackground;
private ImageIcon titleImage;
private ImageIcon startButtonImg;
private ImageIcon settingButtonImg;
private ImageIcon buttonSelectedImg;
public O2BIntro() {
introBackground = new ImageIcon(this.getClass().getResource("../img/wp2537453-dj-background-hd.jpg"));
titleImage = new ImageIcon(this.getClass().getResource("src/img/open2BeaTTitle.png"));
startButtonImg = new ImageIcon(this.getClass().getResource("src/img/MenuImg_Gamestart_KR.png"));
settingButtonImg = new ImageIcon(this.getClass().getResource("src/img/MenuImg_Preference_KR.png"));
}
public void paintIntro() {
screenGraphic = new JLabel(introBackground,JLabel.CENTER);
screenGraphic.setBounds(0, 0, DisplayBasic.myScreenWidth, DisplayBasic.myScreenHeight);
add(screenGraphic);
}
}
So, this is my current part of code. I tried to load my images from /src/img/, but it still doesn't display anything. How should I edit my code? Which kind of framework or component should I use to display properly?
I am doing an application which user by clicking on an image(ImageView1) can see that in another ImageView2. so I try to get the image of ImageView1 in a variable
BufferedImage img= SwingFXUtils.fromFXImage(ImageView1.getImage(), null);
And then assign that variable to ImageView2
ImageView2.setImage(SwingFXUtils.toFXImage(img, null));
But it seems however, setImage is done successfully, but ImageView2 is not showing anything. anyone can help me for a better solution?
Here is the code example:
Controller. ImageView1
#FXML
private void HandleMousePressedOnImageOne()
{
BufferedImage img= SwingFXUtils.fromFXImage(ImageOne.getImage(), null);
try
{
ImageSelection imgSelection= ImageSelection.getImageSelectionInstance();
imgSelection.SetBufferedImageOne(img);
FXMLLoader loader= new FXMLLoader();
SplitPane p= loader.load(getClass().getResource("ImageSelection.fxml").openStream());
ImageSelectionController imgSelectionController= (ImageSelectionController)loader.getController();
imgSelectionController.HandleImageOne();
System.out.println("Image One has been Pressed!");
}
catch(Exception e)
{
e.printStackTrace();
}
}
Model.ImageSelection
public class ImageSelection {
private BufferedImage bufferedImageOne;
private static ImageSelection imageSelectionInstace= new ImageSelection();
private ImageSelection(){}
public static ImageSelection getImageSelectionInstance()
{
return imageSelectionInstace;
}
public void SetBufferedImageOne(BufferedImage img)
{
this.bufferedImageOne= img;
}
public BufferedImage getBufferedImageOne()
{
return this.bufferedImageOne;
}
}
Controller2.ImageView2
public void HandleImageOne(){
ImageSelection imgSelection= ImageSelection.getImageSelectionInstance();
BufferedImage img= imgSelection.getBufferedImageOne();
ImageOne.setImage(SwingFXUtils.toFXImage(img, null));
}
Your code doesn't work because you are loading a new copy of the UI defined in the FXML file ImageSelection.fxml. You don't display this new copy. When you retrieve the controller from the loader that loads that copy of the UI and call
imgSelectionController.HandleImageOne();
you change the image in the ImageView that is part of that new instance of the UI. Since that instance is not displayed, you don't see any effect from that call.
A better approach, which also avoids your first controller having a dependency on the second controller as in your code, is to create an ObjectProperty<Image> in your model, and observe it from the second controller:
public class ImageSelection {
private final ObjectProperty<Image> image = new SimpleObjectProperty<>();
private static ImageSelection imageSelectionInstance= new ImageSelection();
private ImageSelection(){}
public static ImageSelection getImageSelectionInstance() {
return imageSelectionInstance;
}
public ObjectProperty<Image> imageProperty() {
return image ;
}
public final void setImage(Image image) {
imageProperty().set(image);
}
public final Image getImage()
{
return imageProperty().get();
}
}
and in the second controller, do
public class ImageSelectionController {
#FXML
private ImageView imageOne ;
public void initialize() {
ImageSelection.getImageSelectionInstance().imageProperty()
.addListener((obs, oldImage, newImage) -> imageOne.setImage(newImage));
}
// ...
}
Now all the first controller needs to do is set the image in the model:
#FXML
private void handleMousePressedOnImageOne() {
ImageSelection.getImageSelectionInstance().setImage(imageOne.getImage());
}
Note that there's absolutely no need to convert from a JavaFX image to a BufferedImage and back.
I also recommend not using a singleton pattern, for a number of (well-documented; just google "what is wrong with using singletons") reasons. Create a single instance and pass it to each of the controllers, or use a dependency injection framework to manage that for you.
I am making Flappy Bird and I am struggling to get the bird to animate. I have tried the following:
This is the class where I create the ImageView of the bird:
public class Bird {
public static Image[] birdFrames = new Image[3];
public static ImageView birdView = new ImageView();
private static int frameCounter = 0;
public static ImageView bird() {
birdFrames[0] = new Image("/Resources/BirdFrame0.png");
birdFrames[1] = new Image("/Resources/BirdFrame1.png");
birdFrames[2] = new Image("/Resources/BirdFrame2.png");
birdView.setImage(birdFrames[0]);
return birdView;
}
public static void birdAnimate() {
birdView.setImage(birdFrames[frameCounter++]);
if (frameCounter == 3) {
frameCounter = 0;
}
}
}
And here is the code I used to add it to the stage:
Bird.bird().relocate(100,200);
Bird.birdAnimate();
root.getChildren().add(Bird.bird());
All I get is a non updating still image of the bird on my screen
If anyone knows why this isn't working and how to fix it, I would really appreciate the help
I'm trying to get mutiple images in 'GetImage' class, and disply them in the main class.
Can anybody show me an example how to do it?? I tried bunch of other samples but they didn't work since I have two classes.
Here is one that I tried.
main clss:
import java.awt.*;
import hsa.*;
public class Test
{
static Console c;
public void Display()
{
GetImage c = new GetImage();
c.paint(g);
}
public Test()
{
c = new Console ();
}
public static void main (String[] args) throws Exception
{
Test = new Test();
a.Display();
}
}
seperate class:
import java.awt.*;
import hsa.Console;
import java.awt.event.*;
public class GetImage extends Frame
{
Image image;
String imageName = "ImageFileName.jpg";
public void paint (Graphics g)
{
Toolkit tool = Toolkit.getDefaultToolkit ();
image = tool.getImage (imageName);
g.drawImage (image, 30, 30, this); // location of the image
g.drawString (imageName, 100, 50); // location of the name
}
}
I'm not very familiar with the hsa package, but some quick googling says it's an educational package from some company that has since gone out of business, correct me if I'm wrong. So personally I'd try to avoid using any of their stuff if you can.
If you have to use this for school or something then you probably want to stick entirely with their package instead of mix and matching hsa with awt. Something like this might accomplish what you want, but again I'm not familiar with the hsa package.
import java.awt.*;
import hsa.*;
public class Test
{
static Console c;
public void Display()
{
GetImage gI = new GetImage(c,25,80,12);
}
public Test()
{
c = new Console ();
}
public static void main (String[] args) throws Exception
{
Test = new Test();
a.Display();
}
}
import java.awt.*;
import hsa.ConsoleCanvasGraphics;
import java.awt.event.*;
public class GetImage extends ConsoleCanvasGraphics
{
Image image, image2;
String imageName = "ImageFileName.jpg", image2Name = "Image2FileName.jpg";
public GetImage(ConsoleParent parent, int rows, int columns, int fontSize)
{
Toolkit tool = Toolkit.getDefaultToolkit ();
image = tool.getImage (imageName);
image2 = tool.getImage (image2Name);
super(parent,rows,columns,fontSize);
drawImage(image,30,30,this);
drawImage(image2,30,60,this);
drawString(imageName,100,50,new Font("TimesRoman", Font.PLAIN, 20),Color.BLACK);
drawString(image2Name,100,80,new Font("TimesRoman", Font.PLAIN, 20),Color.BLACK);
}
}
Again, I'd try to avoid hsa myself, but if you're set on using it and need to have two separate classes in your program then the above should be a rough outline of something that might work.