Results 1 to 2 of 2
Thread: Image not showing up!!
- 11-08-2010, 05:39 AM #1
Member
- Join Date
- Apr 2010
- Location
- Phoenix, AZ
- Posts
- 25
- Rep Power
- 0
Image not showing up!!
Hello, been awhile since I asked a question on here but I am still trying to learn this fun and sometimes frustrating language.
But the code that I have was a part of a tutorial I found that involved making a screen, then drawing two rectangles on there and moving one of them around, I decided to make it so that I can input my own image and move it around instead (since I am interested in game programming) But the image isn't showing up, I have gotten the image I am trying to use to show up in other programs through following tutorials and stuff so I get the general idea of how they work but I can't figure this out. Please help, thanks ahead of time.
*******Class with main method*******
********Class controlling my screen and what is on it********Java Code:package captain; import javax.swing.*; public class MainGame { JFrame window; Screen panel; public MainGame(){ window = new JFrame("Collistion detection, movement, double buffering, and a game loop!"); panel = new Screen(); window.setSize(800,600); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.getContentPane().add(panel); window.setVisible(true); } public void go(){ panel.startGame(); } public static void main(String[] args){ MainGame game = new MainGame(); game.go(); } }
********Class to control the player / entity*****Java Code:package captain; import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Toolkit; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; public class Screen extends JPanel implements KeyListener{ BufferedImage buffer; Entity player; Entity enemy; Image myImage; Image playerImage; Image enemyImage; public Screen(){ setIgnoreRepaint(true); addKeyListener(this); setFocusable(true); } public void loadImages(){ myImage = new ImageIcon("C:\\Users\\Thrash\\Desktop\\practice\\CaptainStandRight.jpg").getImage(); } public void keyTyped(KeyEvent e){ } public void keyPressed(KeyEvent e){ int key = e.getKeyCode(); if(key ==KeyEvent.VK_LEFT) player.left = true; if(key == KeyEvent.VK_RIGHT) player.right = true; if(key == KeyEvent.VK_UP) player.up = true; if(key == KeyEvent.VK_DOWN) player.down = true; } public void keyReleased(KeyEvent e){ int key = e.getKeyCode(); if(key ==KeyEvent.VK_LEFT) player.left = false; if(key == KeyEvent.VK_RIGHT) player.right = false; if(key == KeyEvent.VK_UP) player.up = false; if(key == KeyEvent.VK_DOWN) player.down = false; } public void Initialize(){ buffer = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB); player = new Entity(myImage, 100,100); } public void update(){ player.move(); } public void drawBuffer(){ Graphics2D b = buffer.createGraphics(); b.setColor(Color.BLACK); b.fillRect(0, 0, 800, 600); b.drawImage(player.getImage(),100,100,null); b.dispose(); } public void drawScreen(){ Graphics2D g = (Graphics2D)this.getGraphics(); g.drawImage(buffer,0,0,this); Toolkit.getDefaultToolkit().sync(); g.dispose(); } public void startGame(){ Initialize(); while(true){ try{ update(); drawBuffer(); drawScreen(); Thread.sleep(15); } catch(Exception e){ e.printStackTrace(); } } } }
Java Code:package captain; import java.awt.Image; public class Entity { int x,y,speed; boolean up,down,left,right,collision; Image image; public Entity(Image image, int x, int y){ this.image = image; this.x = x; this.y = y; speed = 3; up = down = left = right = collision = true; } public int getX(){ return x; } public int getY(){ return y; } public Image getImage(){ return image; } public int getWidth(){ return image.getWidth(null); } public int getHeight(){ return image.getHeight(null); } public void move(){ if (up) y -= speed; if(down) y += speed; if(left) x -= speed; if(right) x += speed; } }
-
I see you have a method called loadImages, but I don't see that you've called this method anywhere. Also, is this supposed to be an "active" graphics program? I'm not very familiar with this type of coding since most of the graphics programs I use use passive graphics, and in this situation, a while (true) with a Thread.sleep(...) will freeze my GUI.
Similar Threads
-
Table not showing up
By dilpreet28 in forum New To JavaReplies: 3Last Post: 08-04-2010, 07:05 AM -
showing image in web application
By Juuno in forum Advanced JavaReplies: 3Last Post: 05-11-2009, 04:36 AM -
Showing
By bostonstate in forum New To JavaReplies: 3Last Post: 08-25-2008, 07:49 PM -
Converting multiple banded image into single banded image... Image enhancement
By archanajathan in forum Advanced JavaReplies: 0Last Post: 01-08-2008, 05:29 PM -
Why isn't this showing?
By JToolTip in forum Java AppletsReplies: 2Last Post: 07-07-2007, 11:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks