• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why am I getting an exception?  RSS feed

 
cameron fraser
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ive been following my book on ImageIcons but decided to try out a URL:



Can anyone explain why this is not working or give me a tip on how I can?

Thank you.
 
Tim Moores
Saloon Keeper
Posts: 3900
91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does "is not working" mean? What happens if you run the code? Post the stack trace of any exception.

Also, make sure the missing 2nd slash in the URL isn't causing problems.
 
cameron fraser
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, sorry forgot to mention.

Got it to get past the exception error with a new image link with two slashes "//" but the JFrame its supposed to display the image on is just blank.

Any ideas?

Full code:

public class SimpleGuiB extends JPanel implements ActionListener {
JButton button;

public static void main (String[] args) {
SimpleGuiB gui = new SimpleGuiB();
gui.go();
}

public void go(){
SimpleGuiB gui = new SimpleGuiB();
JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(970, 600);
frame.setVisible(true);
frame.add(gui);
}

public void paintComponent(Graphics g){

URL test;
try {
test = new URL("https://timedotcom.files.wordpress.com/2015/01/trump.jpg");
Image image = new ImageIcon(test).getImage();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
 
Carey Brown
Bartender
Posts: 3018
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are attempting to load the image inside paintComponent(). This is not the place to do it. The paintComponent() method needs to be as fast as possible and loading an image is slow. Also you never actually paint the image.

Edit: See javadocs for Graphics.drawImage(...)
 
cameron fraser
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh, I removed it from there. Would I create a custom method for it? I tried it in the go() method but it wont load there either. Any suggestions?

I did have some paintcode just didnt want to complicate and divert attention to it.
 
Carey Brown
Bartender
Posts: 3018
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd want to reverse the order of these two lines

And load the image prior to both of them.
Then, let's see your code again (this time post it using Code tags).
 
cameron fraser
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Not sure if I am doing the last parameter correctly, I am following other examples, but I am getting an error:

Multiple markers at this line
- Line breakpoint:SimpleGuiB [line: 23] - test()
- The method drawImage(Image, int, int, ImageObserver) in the type Graphics is not applicable for the arguments (String, int, int,
SimpleGuiB)

 
Carey Brown
Bartender
Posts: 3018
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cameron fraser wrote:
Not sure if I am doing the last parameter correctly, I am following other examples, but I am getting an error:

Multiple markers at this line
- Line breakpoint:SimpleGuiB [line: 23] - test()
- The method drawImage(Image, int, int, ImageObserver) in the type Graphics is not applicable for the arguments (String, int, int,
SimpleGuiB)

You need to read the javadocs for drawImage(), it takes an Image, not a String.

Why did you create a test() method? This should go in the paintComponents(Graphics g) method.
 
cameron fraser
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You said "You are attempting to load the image inside paintComponent(). This is not the place to do it."

Could you type out some code I could paste in to display the image? A bit over my head right now.
 
Carey Brown
Bartender
Posts: 3018
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cameron fraser wrote:You said "You are attempting to load the image inside paintComponent(). This is not the place to do it."

Could you type out some code I could paste in to display the image? A bit over my head right now.

Correct. Don't LOAD it, but you should DRAW it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!