Java 2D Graphics get image from URL instead of resource - java

Alright so I've followed THIS tutorial of youtube for drawing with Java 2D Graphics, however how would I do it so it fetches the image from an URL instead of Resources ?
If you could please update and link me to the new code, that would be a +
Thank you VERY in advance
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class Screen extends JPanel {
private BufferedImage image;
public Screen() {
try {
image = Image.IO.read(getClass().getResourceAsStream("/imagee.png"));
} catch(IOException e) {
e.printStackTrace();
}
repaint();
}
public void paint(Graphics g) {
g.drawImage(image, 10, 10, null);
}
}
I don't exactly know Java, I just need to do this for something

Something like...
image = Image.IO.read(new URL("http://..."));
FYI Class#getResource returns a URL
If the code is an example from the tutorial, then the tutorial is wrong and you should find a new one. Don't override paint, instead override paintComponent. You MUST call super.paint (or super.paintComponent if you've overridden paintComponent) in order to maintain the paint chain and prevent possible graphical glitches from occurring. It would also be easier to to use a JLabel...

You should try to get an URL object:
URL url = new URL("the-URL");
BufferedImage image = ImageIO.read(url);

Related

Images no displaying in my java program

Recently i decided to start learning how to make 2D games With JAVA ( eclipse ) so i found a tutorial online that shows how to make superMari game with java, i wrote the same code he wrote and i followed step by step what he did, which wasn't a big thing to talk about, unfortunately he's code shows, after excuting, a window with two images in it while mine shows just the window with no images, i ensure you that i imported the two images and put them in one package to avoid all kind of problems but it still shows nothing.
my code has two classes, "main" and "Scene", here it is, hopefully someone will find a solution for me, thank you guys!
Main.java :
package AiMEUR.AMiN.jeu;
import javax.swing.JFrame;
public class Main {
public static Scene scene;
public static void main(String[] args) {
JFrame fenetre = new JFrame("Naruto in mario World!!");
fenetre.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fenetre.setSize(700, 360);
fenetre.setLocationRelativeTo(null);
fenetre.setResizable(false);
fenetre.setAlwaysOnTop(true);
scene = new Scene();
fenetre.setContentPane(scene);
fenetre.setVisible(true);
}
}
Scene.java :
package AiMEUR.AMiN.jeu;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
#SuppressWarnings("serial")
public class Scene extends JPanel{
private ImageIcon icoFond;
private Image imgFond1;
private ImageIcon icoMario;
private Image imgMario;
private int xFond1;
public Scene(){
super();
this.xFond1 = -50;
icoFond = new ImageIcon(getClass().getResource("/Images/fond.gif"));
this.imgFond1 = this.icoFond.getImage();
icoMario = new ImageIcon(getClass().getResource("/Images/1.png"));
this.imgMario = this.icoMario.getImage();
// paintComponent(this.getGraphics());
}
public void paintCompenent(Graphics g){
super.paintComponent(g);
Graphics g2 = (Graphics2D)g;
g2.drawImage(this.imgFond1, this.xFond1, 0, null);
g2.drawImage(imgMario, 300, 245, null);
}
}
You have not named the paintComponent method correctly, and therefore it is not being overridden.
The correct name is paintComponent not paintCompenent:
public class Example extends JPanel {
#Override
public void paintComponent(final Graphics g) {
super.paintComponent(g);
}
}
You can determine the loading status of an ImageIcon by doing something like this:
public Scene(){
super();
this.xFond1 = -50;
icoFond = new ImageIcon(getClass().getResource("/Images/fond.gif"));
int status = icoFond.getImageLoadStatus();
switch (status) {
case (MediaTracker.COMPLETE): {
System.out.println("icoFond image has successfully loaded");
}
case (MediaTracker.ERRORED): {
System.out.println("The icoFond image didn't load successfully");
// probably because the image isn't actually at "/Images/fond.gif"
}
}
this.imgFond1 = this.icoFond.getImage();
icoMario = new ImageIcon(getClass().getResource("/Images/1.png"));
this.imgMario = this.icoMario.getImage();
// paintComponent(this.getGraphics());
}

Easily print image on the screen for debug purposes in a blocking manner

I am working on a computer vision project and somewhere in a process an endless loop happens. It seems that my image data is being corrupted.
In past, I used to save debug results on the disk using this method:
public static boolean saveToPath(String path, BufferedImage image) {
File img = new File(path);
try {
ImageIO.write(image, "png", new File(path));
} catch (IOException ex) {
System.err.println("Failed to save image as '"+path+"'. Error:"+ex);
return false;
}
return true;
}
The problem is that once loops are used and the error is somewhere inbetween, I need to see many images. So basically, I'd like a method that would be defined like this:
/** Displays image on the screen and stops the execution until the window with image is closed.
*
* #param image image to be displayed
*/
public static void printImage(BufferedImage image) {
???
}
And could be called in a loop or any function to show be the actual image, effectively behaving like a break point. Because while multithreading is very good in production code, blocking functions are much better for debugging.
You can code something like this. In this example, the image file has to be in the same directory as the source code.
Here's the image displayed in a dialog. You left click the OK button to continue processing.
If the image is bigger than your screen, scroll bars will appear to let you see the whole image.
In your code, since you already have the Image, you can just copy and paste the displayImage method.
package com.ggl.testing;
import java.awt.Image;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class DisplayImage {
public DisplayImage() {
displayImage(getImage());
}
private Image getImage() {
try {
return ImageIO.read(getClass().getResourceAsStream(
"StockMarket.png"));
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public void displayImage(Image image) {
JLabel label = new JLabel(new ImageIcon(image));
JPanel panel = new JPanel();
panel.add(label);
JScrollPane scrollPane = new JScrollPane(panel);
JOptionPane.showMessageDialog(null, scrollPane);
}
public static void main(String[] args) {
new DisplayImage();
}
}

Weird bug involving background images in java

My problem is that when I run my program I get a white screen and text from an earlier build instead of the background image that's suppose to be displayed. I've deleted all the code that was associated with that build.
I've looked around for help and all the threads I've seen say to write the code how I've set it up. I don't understand where the displayed background is even coming from.
Here is the relivent code:
package tactics;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.swing.JFrame;
public class Tactics2 extends JFrame{
private Screen s;
private BufferedImage bg;
private BufferedImage template;
private boolean loaded = false;
public static void main(String[] args) throws IOException{
DisplayMode dm = new DisplayMode(1024, 768, 16, DisplayMode.REFRESH_RATE_UNKNOWN);
Tactics2 t = new Tactics2();
t.run(dm);
}
//run method
public void run(DisplayMode dm) throws IOException{
loadpics();
s = new Screen();
try{
s.setFullScreen(dm, this);
try{
Thread.sleep(5000);
}catch(InterruptedException ex){}
}finally{
s.restoreScreen();
}
}
public void loadpics() throws IOException{
bg = new BufferedImage(1024, 768, BufferedImage.TYPE_INT_RGB);
template = new BufferedImage(1024, 768, BufferedImage.TYPE_INT_RGB);
ChaosBack cb = new ChaosBack();
bg = cb.ChaosBack(bg, template);
loaded = true;
repaint();
}
#Override
public void paint(Graphics g){
if(loaded){
g.drawImage(bg, 0, 0, null);
}
}
}
You've broken the paint chain
#Override
public void paint(Graphics g){
if(loaded){
g.drawImage(bg, 0, 0, null);
}
}
Basically, you've failed to call super.paint. Graphics is a shared resource, that is, everything painted for a given paint cycle uses the same Graphics context.
Part of the job of the paint chain is to prepare it for painting by clearing the Graphics context.
You should avoid overriding paint of a top level container for a number reasons. It's not double buffered, so it may flicker as it's updated and it doesn't take into consideration the frame decorations, meaning you can end up painting underneath the borders of the frame, instead within the viewable area.
You'd better of creating a custom component, extending from something like JPanel and overriding it's paintComponent method (making sure you call super.paintComponent)
Thread.sleep(5000); is a REALLY bad idea within a Swing application. It's possible to actually stop your application cold and stop it from been updated/painted or respond to any user interaction.
Swing is not thread safe. This means that all changes to the UI must be made from within the context of the Event Dispatching Thread.
Take a look at:
Performing Custom Painting
Painting in AWT and Swing
Concurrency in Swing
Initial Threads
How to Use Swing Timers
For details and ideas

Drawing this Image in my Java Applet wont work

import java.awt.*;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
public class MonoplyDriver extends JApplet {
boolean isFirst=true;
Player John = new Player(1500,"Circle","John");
Board board = new Board();
Image imgBoard;
public void init()
{
//imgBoard = new ImageIcon("res/board.png").getImage();
imgBoard = getImage(getDocumentBase(),"res/board.png");
setSize(750,750);
System.out.println(getDocumentBase());
}
public void paint(Graphics g)
{
//super.paint(g);
if(isFirst)
{
isFirst=false;
}
g.drawImage(imgBoard, 0, 0, this);
}
}
From the sounds of it, the image is not being found because it is a internal resource.
You could try something like...
imgBoard = ImageIO.read(getClass().getResource("res/board.png"));
This will throw an IOException if the image cannot be loaded for some reason, which is more useful than what you're getting right now
As an aside. You should avoid painting directly to top level containers, but instead using something that extends from JComponent and override it's paintComponent method
Take a look at Performing Custom Painting and Reading/Loading an Image for more details

How can I display an image in the Applet?

I have an image and I want to display it in the applet, The problem is the image wont display. Is there something wrong with my code?
Thanks...
Here's my code :
import java.applet.Applet;
import java.awt.*;
public class LastAirBender extends Applet
{
Image aang;
public void init()
{
aang = getImage(getDocumentBase(), getParameter("images.jpg"));
}
public void paint(Graphics g)
{
g.drawImage(aang, 100, 100, this);
}
}
aang = getImage(getDocumentBase(), getParameter("images.jpg"));
I suspect you are doing something wrong, and that should be just plain:
aang = getImage(getDocumentBase(), "images.jpg");
What is the content of HTML/applet element? What is the name of the image? Is the image in the same directory as the HTML?
Update 1
The 2nd (changed) line of code will try to load the images.jpg file in the same directory as the HTML.
Of course, you might need to add a MediaTracker to track the loading of the image, since the Applet.getImage() method returns immediately (now), but loads asynchronously (later).
Update 2
Try this exact experiment:
Save this source as ${path.to.current.code.and.image}/FirstAirBender.java .
/*
<applet class='FirstAirBender' width=400 height=400>
</applet>
*/
import javax.swing.*;
import java.awt.*;
import java.net.URL;
import javax.imageio.ImageIO;
public class FirstAirBender extends JApplet {
Image aang;
public void init() {
try {
URL pic = new URL(getDocumentBase(), "images.jpg");
aang = ImageIO.read(pic);
} catch(Exception e) {
// tell us if anything goes wrong!
e.printStackTrace();
}
}
public void paint(Graphics g) {
super.paint(g);
if (aang!=null) {
g.drawImage(aang, 100, 100, this);
}
}
}
Then go to the prompt and compile the code then call applet viewer using the source name as argument.
C:\Path>javac FirstAirBender.java
C:\Path>appletviewer FirstAirBender.java
C:\Path>
You should see your image in the applet, painted at 100x100 from the top-left.
1) we living .. in 21century, then please JApplet instead of Applet
import java.awt.*;
import javax.swing.JApplet;
public class LastAirBender extends JApplet {
private static final long serialVersionUID = 1L;
private Image aang;
#Override
public void init() {
aang = getImage(getDocumentBase(), getParameter("images.jpg"));
}
#Override
public void paint(Graphics g) {
g.drawImage(aang, 100, 100, this);
}
}
2) for Icon/ImageIcon would be better to look for JLabel
3) please what's getImage(getDocumentBase(), getParameter("images.jpg"));
there I'll be awaiting something like as
URL imageURL = this.getClass().getResource("images.jpg");
Image image = Toolkit.getDefaultToolkit().createImage(imageURL);
Image scaled = image.getScaledInstance(100, 150, Image.SCALE_SMOOTH);
JLabel label = new JLabel(new ImageIcon(scaled));
Well , above answers are correct. This is the code I used to display image. Hope it helps:
/*
<applet code = "DisplayImage.class" width = 500 height = 300>
</applet>
*/
import java.applet.Applet;
import java.awt.*;
public class DisplayImage extends Applet
{
Image img1;
public void init(){
img1 = getImage(getCodeBase(),"Nature.jpg" );
}
public void paint(Graphics g){
g.drawImage(img1, 0,0,500,300,this);
}
}
In above code, we create an image class object and get image from location specified by codebase. Then plot the image using drawImage method. Those who are interested in knowing value of getCodeBase() and getDocumentBase() methods can add following code in paint method. They are actually location of src folder in your project folder:-
String msg;
URL url=getDocumentBase();
msg="Document Base "+url.toString();
g.drawString(msg,10,20);
url=getCodeBase();
msg="Code Base "+url.toString();
g.drawString(msg,10,40);
One more point to note:- Make sure images and classes don't have same name in src folder. This was preventing my image to be displayed.

Categories

Resources