Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Drawing an Image  RSS feed

 
David Herron
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this assignment,

"Write a class that extends the JComponent class that keeps track of information for movie. Maintain instance variables for the title, director, year released, and an image. Write your own paintComponent method in this class that draws the image and then displays the title, director, and release year. In your applet class, instantiate several Movie objects for different movies and add each to your applet."

I don't know how to draw these images.

The example in the book uses an ImgPanel. But I want to simplify this example to just draw a jpeg image in my applet. If I can just get this solved, I would be fine with the rest of the assignment. I know it is simple, but I have run out of resources. I think I need to initialize something still in Movies, but I don't know what. Here is what I am trying -



 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16006
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that your complete class Movies, or is there more code that you didn't post?

In line 5 you're declaring a member variable img, but you are not setting it anywhere. So it remains null, the expression in if-statement in line 10 is always going to be false, and no image is ever drawn.
 
David Herron
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this is my complete code for movies. I thought I was setting img in the JApplet with img = getImage( getCodeBase( ), "DeadMan.jpg" ); Do I set img inside a method? I know how to draw directly in a single class JApplet, but drawing in JPanel and adding it in the JApplet is confusing to me.

Sorry about this. I am completely new to programming.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having fields with the same name in two different classes doesn't automagically give them the same value.

I am completely new to programming.

Start here and don't progress to Applets or GUIs until you know the basics.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!