Results 1 to 13 of 13

Thread: Drawing Images

  1. #1
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Drawing Images

    I am trying to draw an image unto the screen, however, nothing appears...

    Java Code:
    private ImageIcon i = new ImageIcon("image.png");
    private Image image;
    ...
    image = i.getImage();
    ...
    g.drawImage(image, (int)(x - r), (int)(y - r), null);
    Am I doing it right? The program gives me no error, it's just that when I try to draw it, I see nothing.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Drawing Images

    Can you make a Short, Self Contained, Correct Example that shows the problem?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Drawing Images

    I can try. The image is not shown, even though the source is correct because I didn't get an error. When printing out the x and y coordinates however, I can see that it is moving. I am also wondering where I define the height and width of the image. Are there constructors or functions for that?

    Java Code:
    ArrayList<Boss> bosses = new ArrayList<Boss>();
    ...
    // UPDATING BOSSES
    for(int x = 0; x < bosses.size(); x++){
        bosses.get(x).update();
    }
    ...
    // DRAWING BOSSES
    for(int x = 0; x < bosses.size(); x++){
        bosses.get(x).draw(g);
    }
    ...
    public class Boss{
    	// FIELDS
    	...
    	private ImageIcon i;
    	private Image image;
    	
    	// CONSTRUCTOR
    	public Boss(){
    		angle = Math.random() * 140 + 20;
    		rad = Math.toRadians(angle);
    		
    		r = 30;
    		
    		x = Math.random() * GamePanel.WIDTH / 2 + GamePanel.WIDTH / 4;
    		y = -r;
    		
    		speed = 5;
    		
    		dx = Math.cos(rad) * speed;
    		dy = Math.sin(rad) * speed;
    		
    		health = 300;
    		
    		ready = false;
    		dead = false;
    		
    		i = new ImageIcon("image.PNG");
    		image = i.getImage();
    	}
    	
    	// FUNCTIONS
    	public void update(){
    		x += dx;
    		y += dy;
    		
    		if(ready){
    			if(x < r || x > GamePanel.WIDTH - r) dx = -dx;
    			if(y < r || y > GamePanel.HEIGHT - r) dy = -dy;
    		}else{
    			if(x > r && x < GamePanel.WIDTH - r && y > r && y < GamePanel.HEIGHT - r)
    				ready = true;
    		}
    	}
    	
            public void draw(Graphics2D g){
                    g.drawImage(image, (int)(x - r), (int)(y - r), null);
            }
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Drawing Images

    The posted code won't compile or execute. Can you make a SSCCE?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Drawing Images

    Then I need to post a lot of code, I mean, a lot. The image is a boss for a game.

  6. #6
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Drawing Images

    As you wish, I made the code executable. As you can see, no picture can be seen, but the object is moving around.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class Test{
    	public static void main(String[] args){
    		JFrame window = new JFrame("Space Invaders");
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.setContentPane(new GamePanel());
    		
    		window.setResizable(false);
    		window.setVisible(true);
    		window.setLocationRelativeTo(null);
    		window.pack();
    	}
    }
    
    class GamePanel extends JPanel implements Runnable{
    	// FIELDS
    	public static int WIDTH = 400;
    	public static int HEIGHT = 400;
    	
    	private Thread thread;
    	private boolean running;
    	
    	private BufferedImage image;
    	private Graphics2D g;
    	
    	private int FPS = 30;
    	private double averageFPS;
    	
    	public static ArrayList<Boss> bosses;
    	
    	// CONSTRUCTOR
    	public GamePanel(){
    		super();
    		setPreferredSize(new Dimension(WIDTH, HEIGHT));
    		setFocusable(true);
    		requestFocus();
    	}
    	
    	// FUNCTIONS
    	public void addNotify(){
    		super.addNotify();
    		if(thread == null){
    			thread = new Thread(this);
    			thread.start();
    		}
    	}
    	
    	public void run(){
    		running = true;
    		
    		image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    		g = (Graphics2D) image.getGraphics();
    		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    		g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    		
    		bosses = new ArrayList<Boss>();
    		
    		bosses.add(new Boss());
    		
    		long startTime;
    		long URDTimeMillis;
    		long waitTime;
    		long totalTime = 0;
    		
    		int frameCount = 0;
    		int maxFrameCount = 30;
    		
    		long targetTime = 1000 / FPS;
    		
    		while(running){
    			startTime = System.nanoTime();
    			
    			gameUpdate();
    			gameRender();
    			gameDraw();
    			
    			URDTimeMillis = (System.nanoTime() - startTime) / 1000000;
    			waitTime = targetTime - URDTimeMillis;
    			try{
    				Thread.sleep(waitTime);
    			}catch(Exception e){
    			}
    			
    			totalTime += System.nanoTime() - startTime;
    			frameCount++;
    			if(frameCount == maxFrameCount){
    				averageFPS = 1000.0 / ((totalTime / frameCount) / 1000000);
    				frameCount = 0;
    				totalTime = 0;
    			}
    		}
    	}
    	
    	private void gameUpdate(){
    		// UPDATING BOSSES
    		for(int x = 0; x < bosses.size(); x++){
    			bosses.get(x).update();
    			System.out.println("X: " + bosses.get(x).getX() + " Y: " + bosses.get(x).getY());
    		}
    	}
    	
    	private void gameRender(){
    		// DRAWING BACKGROUND
    		g.setColor(new Color(0, 119, 255));
    		g.fillRect(0, 0, WIDTH, HEIGHT);
    		
    		// DRAWING BOSSES
    		for(int x = 0; x < bosses.size(); x++){
    			bosses.get(x).draw(g);
    		}
    	}
    	
    	private void gameDraw(){
    		Graphics g2 = this.getGraphics();
    		g2.drawImage(image, 0, 0, null);
    		g2.dispose();
    	}
    }
    
    class Boss{
    	// FIELDS
    	private double x;
    	private double y;
    	private double r;
    	
    	private double dx;
    	private double dy;
    	private double speed;
    	
    	private double angle;
    	private double rad;
    	
    	private int health;
    	
    	private boolean ready;
    	private boolean dead;
    	
    	private ImageIcon i;
    	private Image image;
    	
    	// CONSTRUCTOR
    	public Boss(){
    		angle = Math.random() * 140 + 20;
    		rad = Math.toRadians(angle);
    		
    		r = 30;
    		
    		x = Math.random() * GamePanel.WIDTH / 2 + GamePanel.WIDTH / 4;
    		y = -r;
    		
    		speed = 5;
    		
    		dx = Math.cos(rad) * speed;
    		dy = Math.sin(rad) * speed;
    		
    		health = 300;
    		
    		ready = false;
    		dead = false;
    		
    		i = new ImageIcon("Gandhi.PNG");
    		image = i.getImage();
    	}
    	
    	// FUNCTIONS
    	public double getX(){return x;}
    	public double getY(){return y;}
    	
    	public void update(){
    		x += dx;
    		y += dy;
    		
    		if(ready){
    			if(x < r || x > GamePanel.WIDTH - r) dx = -dx;
    			if(y < r || y > GamePanel.HEIGHT - r) dy = -dy;
    		}else{
    			if(x > r && x < GamePanel.WIDTH - r && y > r && y < GamePanel.HEIGHT - r)
    				ready = true;
    		}
    	}
    	
    	public void draw(Graphics2D g){
    		g.drawImage(image, (int)(x - r), (int)(y - r), null);
    	}
    }

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Drawing Images

    When I compile and execute the code, the image from line 166 is shown on a blue background and it moves around bouncing off the sides.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Drawing Images

    So you entered an own image source and you saw the image?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Drawing Images

    Yes, I used my own image.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Drawing Images

    Then I might know what's the problem. My image partly consists of "invisible areas", because I don't want white space surrounding what I have drawn, because then it will look like a box. Do you understand what I mean?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default Re: Drawing Images

    Try using another image to see if the contents of the image is the problem.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Zelaine is offline Senior Member
    Join Date
    Aug 2013
    Location
    Sweden
    Posts
    161
    Rep Power
    2

    Default Re: Drawing Images

    I found the problem, I of course had to include the entire source. Do you know how to change the size of the image? Because right now it is pretty big...

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Drawing Images

    Image.getScaledInstance().

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 1
    Last Post: 11-07-2011, 12:18 AM
  2. Help with drawing images
    By jammas615 in forum Java 2D
    Replies: 11
    Last Post: 07-05-2011, 09:57 PM
  3. drawing images in swing
    By owboateng in forum New To Java
    Replies: 3
    Last Post: 05-09-2011, 03:27 PM
  4. Help drawing images for pong game
    By aalsaqri in forum New To Java
    Replies: 4
    Last Post: 05-28-2010, 03:10 PM
  5. drawing images
    By diggitydoggz in forum New To Java
    Replies: 4
    Last Post: 01-02-2009, 04:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •