Results 1 to 13 of 13
Thread: How to display Images?
- 10-08-2010, 03:03 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 16
- Rep Power
- 0
How to display Images?
Hello,
I am building a car game currently and I am having troubles placing images.
Game initilises
Then the intro startsJava Code:mainScreen = new JFrame("Kario Mart"); mainScreen.getContentPane().add(new javax.swing.JLabel(new javax.swing.ImageIcon("alien.png"))); mainScreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainScreen.setSize(Game.xDim,Game.yDim + 60); intro = new Intro(); screen = new GameComponent(); mainScreen.getContentPane().add(screen); mainScreen.setVisible(true);
And no image is drawn, I would like to know which methods I should use to place/draw an image onto a frame, using g.drawImage or jframe.add(comp) or something.Java Code:clear(); Graphics2D g2 = (Graphics2D) g; // Flashing text, based on arcade machines, could possibly be annoying if (counter >= 20) { g2.setColor(Color.black); g2.scale(2, 2); g2.drawString("Spaces goes fowards, Backspace goes backwards", 79, 340); g2.scale(0.5, 0.5); } id = new ImageDisplay(); id.draw(KarioMart.game, KarioMart.screen); if (stage <= 1) { ; g2.drawImage(new ImageIcon("alien.png").getImage(), 0, 0, null); paint(g); }
I have looked up 10 different tutorials and I just feel so stupid that I can't get this one thing right.
Are there any limitations on images being displayed? Anything I have forgotten? Thanks,
Rectal Exambot
- 10-08-2010, 03:23 AM #2
Most drawing of images is down in a JPanel. Extend the JPanel class and override its paintComponent method and add your drawImage method call there.
Add the class to the container where you want the image to show.
You should get the image one time outside of the paintComponent method, not every time you draw it.
For sample code, Search on the forum for paintComponent.
- 10-08-2010, 03:44 AM #3
Member
- Join Date
- Aug 2010
- Posts
- 16
- Rep Power
- 0
id = new ImageDisplay();Java Code:import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import javax.swing.ImageIcon; import javax.swing.JPanel; public class ImageDisplay extends JPanel { int x = 0, y = 0; ImageIcon logo; Image img; public ImageDisplay() { logo = new ImageIcon("alien.png"); img = logo.getImage(); } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.drawImage(img, 10, 10, null); } }
Screen.getContentPane().add(id);
id.paintComponent(screen.getGraphics());
and then id.repaint(); is recalled, is this in the correct order? What do I need to change? Yes I am looking up tutorialsLast edited by Rectal Exambot; 10-08-2010 at 03:57 AM.
- 10-08-2010, 01:19 PM #4
That looks about right. Have you written a small test program to see if it works.
- 10-08-2010, 03:18 PM #5
Never never never use getGraphics of a Component. That way lies grief.
Never never never attempt to invoke paintComponent from your code. Call repaint() and let the RepaintManager look after the rest.
Recommended reading: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
db
-
With some exceptions -- as Darryl knows but likely wants to keep his answer succinct and avoid confusing the OP. But for the benefit of lurkers who may be into advanced graphics techniques, the exceptions I refer to are best summarized in the highly recommended book "Filthy Rich Clients" by Haase and Guy
- 10-10-2010, 11:50 AM #7
Member
- Join Date
- Aug 2010
- Posts
- 16
- Rep Power
- 0
Sort of, I added it to my old program and no image shows and I am unsure as what order and ill just go through everything just for clarification. It currently doesn't work.
This was my first ever game, and I had been working on it for a while for an assignment, and just implementing new features but this is how it works.
Java Code:public static void main(String[] args) { mainScreen = new JFrame("Kario Mart"); raceGame = new KarioMart(); raceGame.introScreen(); }Java Code:public KarioMart() { mainScreen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainScreen.setSize(Game.xDim,Game.yDim + 60); intro = new Intro(); id = new ImageDisplay(); screen = new GameComponent(); mainScreen.getContentPane().add(screen); mainScreen.setVisible(true); }public class Intro extends JComponent implements KeyListenerJava Code:public class GameComponent extends JComponent { BufferedImage buffI; public GameComponent() { this.setFocusable(true); setPreferredSize(new Dimension((int) Game.xDim,(int) Game.yDim + 60)); buffI = new BufferedImage((int) Game.xDim,(int) Game.yDim + 60, BufferedImage.TYPE_INT_ARGB); } public void paint(Graphics g) { g.drawImage(buffI, 0, 0, null); } public void clear() { // TODO Auto-generated method stub Graphics g = buffI.getGraphics(); g.setColor(new Color(1.0f,1.0f,1.0f)); g.fillRect(0, 0, (int) Game.xDim + 100, (int) Game.yDim + 100); } public Graphics2D getGraphics2D() { // TODO Auto-generated method stub return buffI.createGraphics(); } public void drawImage(Image image, int i, int j) { // TODO Auto-generated method stub Graphics g = buffI.getGraphics(); g.drawImage(image, i, j, null); } }
The intro handles keypresses during the intro, allows drawing of text and the selection of tracks/vehicles.
The main problems I have been having:
What adds what? Do I add the ImageDisplay to the frame? Or to the GameComponent? Where should I recall the repaint().
If I want to get graphics how do I do so without using the getGraphics? Should it be the Frame.getGraphics()?
(Sorry if the tutorial answered any questions asked, but I am off to bed, I will look at it tommorrow)
- 10-10-2010, 01:27 PM #8
For what context do you want a Graphics object . The paintComponent method is passed a Graphics object for the GUI that you are to use in that method.If I want to get graphics
For the buffered image that you are building, you currently get a graphics object for it so you can draw on it.
Call repaint() when you have made a change to the variables that control what is displayed on the GUI and want the JVM to call the paintComponent method so that those changes can be drawn.
- 10-10-2010, 02:59 PM #9
Actually, I don't.
So much for that :rolleyes: ;)but likely wants to keep his answer succinct and avoid confusing the OP.
Ah. I don't have nor have read any books, my learning has been from the tutorials and on the forums.But for the benefit of lurkers who may be into advanced graphics techniques, the exceptions I refer to are best summarized in the highly recommended book "Filthy Rich Clients" by Haase and Guy
db
-
I had assumed that you had read it since you know so dang much about graphics. What the text states is pretty much what you do but specifically that you do not want to call getGraphics on a component for rendering purposes, but that you may sometimes call the method to query rendering attributes. You also may sometimes want to override a JComponent's paint method rather than paintComonent when you "want to alter the graphics state for all of the taht component's rendering". They then give a great example with a translucent button.
Great, great book and highly recommended!
- 10-11-2010, 03:39 AM #11
Member
- Join Date
- Aug 2010
- Posts
- 16
- Rep Power
- 0
Okay, messing around a little, but how do I add an image? I currently just have an alien.png file inside the source, I can get text to display but not the alien, any ideas?
I have commented out all the other stuff but I keep getting this:
javax.imageio.IIOException: Can't read input file!
javax.imageio.IIOException: Can't read input file!
I have tried PNGs and JPEGS and can't seem to find where to store the images
Edit: I am using Eclipse, apparently I need to use the Jar File if I want to see the image... now I am madLast edited by Rectal Exambot; 10-11-2010 at 04:15 AM.
-
If your images are in a Jar file then you can't retrieve the images as files but rather as resources. Note that ImageIO.read() has an overload that accepts an InputStream as a parameter, and you can get your BufferedImage that way.
- 10-11-2010, 04:36 AM #13
Member
- Join Date
- Aug 2010
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
JavaHelp how to display images
By fossildoc in forum New To JavaReplies: 0Last Post: 04-04-2010, 06:42 AM -
How to display Images from MS access using JSP
By redmaverick in forum JDBCReplies: 3Last Post: 11-30-2009, 09:28 AM -
display images
By prof.deedee in forum AWT / SwingReplies: 10Last Post: 11-12-2009, 09:08 PM -
Dynamic display of images
By gixerino in forum NetBeansReplies: 7Last Post: 01-15-2009, 02:55 PM -
display images in a Web Application based on java/jsp
By mnsse in forum Advanced JavaReplies: 0Last Post: 03-25-2008, 12:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks