Results 1 to 2 of 2
  1. #1
    ccub is offline Member
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Default loading images from seperate class and drawing them

    for whatever reason this isn't drawing correctly (at all) and yes I do have "blood.png" in the same directory as my src

    also I've done g.drawImage with createBufferStrategy(2) before and it worked fine but I can't access it from another class unless it's static (which defeats the purpose)

    graphicsTest.java: [Java] package com.c0decub.gtest; import java.awt.Canvas; import java.awt.Color; i - Pastebin.com

    images.java: [Java] package com.c0decub.gtest; import java.awt.Canvas; import java.awt.Graphics; - Pastebin.com

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: loading images from seperate class and drawing them

    You'll want to post your actual code here in the forum to make it as easy as possible for others to see it. Here, I'll do that for you this time:


    graphicsTest.java
    Java Code:
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    import java.awt.image.DataBufferInt;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class graphicsTest extends Canvas implements Runnable {
    	
        public static final int GAME_WIDTH = 320;
        public static final int GAME_HEIGHT = 240;
        public static final int SCALE = 2;
        public static final int SCALE_IMG = 3;
        
        private Thread thread;
        private boolean running;
    	
    	public Graphics g;
    	
    	private static BufferedImage blood = Images.load("blood.png");
    	
    	public static void main(String[] args) {
    		graphicsTest gObj = new graphicsTest();
    		
    		JFrame frame = new JFrame("graphics");
    		frame.add(gObj);
            frame.pack();
            frame.setResizable(false);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            
            gObj.start();
    	}
    	
    	public synchronized void start() {
            thread = new Thread(this);
            
    		running = true;
    		
            thread.start();
    	}
    	
    	public synchronized void stop(){ 
    		running = false;
    	}
    
    	public graphicsTest(){ 
            int w = GAME_WIDTH * SCALE;
            int h = GAME_HEIGHT * SCALE;
            setPreferredSize(new Dimension(w, h));
            setMaximumSize(new Dimension(w, h));
            setMinimumSize(new Dimension(w, h));
           // setBackground(Color.BLACK);
    	}
    
    	public void run() {
    		while (running){
    			render();
    		}
    	}
    	
    	public void render(){
    		Images.load("blood.png");
    		g.drawImage(blood, 0, 0, this);
    	}
    	
    	
    }
    Images.java
    Java Code:
    import java.awt.Canvas;
    import java.awt.Graphics;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    
    public class Images{
    	
        public static BufferedImage load(String imageName)
        {
            BufferedImage image;
            try
            {
                image = ImageIO.read(Images.class.getResourceAsStream(imageName));
                BufferedImage img = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
                Graphics g = img.getGraphics();
                g.drawImage(image, 0, 0, null);
                System.out.println(image.getWidth());
                g.dispose();
                image = img;
            }
            catch (IOException e)
            {
                e.printStackTrace();
                return null;
            }
            return image;
        }
        
        
    	
    }

Similar Threads

  1. Help displaying an array from a seperate class
    By aeon1613 in forum New To Java
    Replies: 2
    Last Post: 01-16-2011, 08:15 AM
  2. Keyboard Input In A Seperate Class?
    By FatalSylence in forum New To Java
    Replies: 6
    Last Post: 10-12-2010, 05:29 AM
  3. Manipulating components of a seperate GUI Class
    By ribbs2521 in forum New To Java
    Replies: 1
    Last Post: 10-20-2009, 10:53 PM
  4. Callback when window is done drawing/loading?
    By rodrigo in forum SWT / JFace
    Replies: 0
    Last Post: 08-21-2009, 02:01 AM
  5. Loading images in seperate packages?
    By Bibendum in forum New To Java
    Replies: 0
    Last Post: 03-02-2008, 10:43 PM

Posting Permissions

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