Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Problems With Image Loading and Extended Objects

  1. #1
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Problems With Image Loading and Extended Objects

    I've been having a bit of trouble with drawing graphics for an object that extends another object. For whatever reason, the graphics are being shown for the original object, instead of being overwritten in the new object.

    Original Object:
    package gravitygamesinteractive.scorpioengine.main;
     
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.Rectangle;
     
    import javax.swing.ImageIcon;
     
    public class Immobileobject extends Rectangle{
    	public int id,layer,objwidth,objheight,tilesizex,tilesizey;
    	public boolean smarttile;
    	public static ImageIcon tiletopleft,tiletop,tiletopright,tileleft,tile,tileright,tilebottomleft,
    	tilebottom,tilebottomright,tiletopthin,tiletbthin,tilebottomthin,tileleftthin,tilelrthin,tilerightthin;
     
    	public Immobileobject(int x,int y,int layer,int objwidth,int objheight, int tilesizex, int tilesizey,
    			boolean smarttile){
    		this.x=x;
    		this.y=y;
    		this.layer=layer;
    		this.tilesizex=tilesizex;
    		this.tilesizey=tilesizey;
    		this.objwidth=objwidth;
    		this.objheight=objheight;
    		this.width=objwidth*tilesizex;
    		this.height=objheight*tilesizey;
    		this.smarttile=smarttile;
     
    		setImages();
     
    	}
     
    	private void setImages() {
    		tiletopleft = new ImageIcon("KTCTestTile.png");
    		tiletop = new ImageIcon("KTCTestTile.png");
    		tiletopright = new ImageIcon("KTCTestTile.png");
    		tileleft = new ImageIcon("KTCTestTile.png");
    		tile = new ImageIcon("KTCTestTile.png");
    		tileright = new ImageIcon("KTCTestTile.png");
    		tilebottomleft = new ImageIcon("KTCTestTile.png");
    		tilebottom = new ImageIcon("KTCTestTile.png");
    		tilebottomright = new ImageIcon("KTCTestTile.png");
    		tiletopthin = new ImageIcon("KTCTestTile.png");
    		tiletbthin = new ImageIcon("KTCTestTile.png");
    		tilebottomthin = new ImageIcon("KTCTestTile.png");
    		tileleftthin = new ImageIcon("KTCTestTile.png");
    		tilelrthin = new ImageIcon("KTCTestTile.png");
    		tileright = new ImageIcon("KTCTestTile.png");
    	}
     
    	public void tick(){
    		//later on, things such as tile animation will go here
    	}
     
    	public void render(Graphics g, Component c){
    		g.setColor(Color.red);
    		if(smarttile){
    		if(objwidth==1 && objheight==1){
    			g.fillRect(0, 0, tilesizex, tilesizey);
    			tile.paintIcon(c, g, x-Frame.sx, y-Frame.sy);
    		}
    		if(objwidth==1 && objheight>1){
    			for(int h=0; h<objheight; h++){
    				if(h==0){
    					tiletopthin.paintIcon(c, g, x-Frame.sx, y-Frame.sy);
    				}
    				if(h==objheight-1){
    					tilebottomthin.paintIcon(c, g, x-Frame.sx, y-Frame.sy+(objheight*tilesizey-1));
    				}
    				else if(h>0){
    					tiletbthin.paintIcon(c, g, x-Frame.sx, y-Frame.sy+(h*tilesizey-1));
    				}
    			}
    		}
    		if(objwidth>1 && objheight==1){
    			for(int w=0; w<objwidth; w++){
    				if(w==0){
    					tileleftthin.paintIcon(c, g, x-Frame.sx, y-Frame.sy);
    				}
    				if(w==objwidth-1){
    					tilerightthin.paintIcon(c, g, x-Frame.sx+(objwidth*tilesizex-1), y-Frame.sy);
    				}
    				else if(w>0){
    					tilelrthin.paintIcon(c, g, x-Frame.sx+(w*tilesizex-1), y-Frame.sy);
    				}
    			}
    		}
    		if(objwidth>1 && objheight>1){
    			for(int h=0; h<objheight; h++){
    				for(int w=0; w<objwidth; w++){
    					if(w==0 && h==0){
    						tiletopleft.paintIcon(c, g, x-Frame.sx, y-Frame.sy);
    					}
    					if(w==objwidth-1 && h==0){
    						tiletopright.paintIcon(c, g, x-Frame.sx+(w*tilesizex-1), y-Frame.sy);
    					}
    					if(w==0 && h==objheight-1){
    						tilebottomleft.paintIcon(c, g, x-Frame.sx, y-Frame.sy+(h*tilesizey-1));
    					}
    					if(w==objwidth-1 && h==objheight-1){
    						tilebottomright.paintIcon(c, g, x-Frame.sx+(w*tilesizex-1), y-Frame.sy+(h*tilesizey-1));
    					}
    					if(w<objwidth-1 && w>0 && h==0){
    						tiletop.paintIcon(c, g, x-Frame.sx+(w*tilesizex-1), y-Frame.sy);
    					}
    					if(w<objwidth-1 && w>0 && h==objheight-1){
    						tilebottom.paintIcon(c, g, x-Frame.sx+(w*tilesizex-1), y-Frame.sy+(h*tilesizey-1));
    					}
    					if(w==0 && h<objheight-1 && h>0){
    						tileleft.paintIcon(c, g, x-Frame.sx, y-Frame.sy+(h*tilesizey-1));
    					}
    					if(w==objwidth-1 && h<objheight-1 && h>0){
    						tileright.paintIcon(c, g, x-Frame.sx+(w*tilesizex-1), y-Frame.sy+(h*tilesizey-1));
    					}
    					if(w>0 && h>0 && w<objwidth-1 && h<objheight-1){
    						tile.paintIcon(c, g, x-Frame.sx+(w*tilesizex-1), y-Frame.sy+(h*tilesizey-1));
    					}
    				}
    			}
    		}
    	}else{
    		for(int h=0; h<objheight; h++){
    			for(int w=0; w<objwidth; w++){
    					tile.paintIcon(c, g, x-Frame.sx+(w*tilesizex-1), y-Frame.sy+(h*tilesizey-1));
    			}
    		}
    	}
    	}
     
    }

    Extending Object:
    package gravitygamesinteractive.scorpioengine.main;
     
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.Rectangle;
     
    import javax.swing.ImageIcon;
     
    public class Ground extends Immobileobject{
    	public int id,layer,objwidth,objheight,tilesizex,tilesizey;
    	public boolean smarttile;
    	public static ImageIcon tiletopleft,tiletop,tiletopright,tileleft,tile,tileright,tilebottomleft,
    	tilebottom,tilebottomright,tiletopthin,tiletbthin,tilebottomthin,tileleftthin,tilelrthin,tilerightthin;
     
    	public Ground(int x, int y, int layer, int objwidth, int objheight,
    			int tilesizex, int tilesizey, boolean smarttile) {
    		super(x, y, layer, objwidth, objheight, tilesizex, tilesizey, smarttile);
     
    		/*tiletopleft = new ImageIcon("res/resources/images/leveltiles/grassland/KTCTestTile.png");
    		tiletop = new ImageIcon("res/resources/images/leveltiles/grassland/KTCTestTile.png");
    		tiletopright = new ImageIcon("res/resources/images/leveltiles/grassland/KTCTestTile.png");
    		tileleft = new ImageIcon("res/resources/images/leveltiles/grassland/KTCTestTile.png");
    		tile = new ImageIcon("res/resources/images/leveltiles/grassland/KTCTestTile.png");
    		tileright = new ImageIcon("res/resources/images/leveltiles/grassland/KTCTestTile.png");
    		tilebottomleft = new ImageIcon("res/resources/images/leveltiles/grassland/KTCTestTile.png");
    		tilebottom = new ImageIcon("res/resources/images/leveltiles/grassland/KTCTestTile.png");
    		tilebottomright = new ImageIcon("res/resources/images/leveltiles/grassland/KTCTestTile.png");
    		tiletopthin = new ImageIcon("res/resources/images/leveltiles/grassland/KTCTestTile.png");
    		tiletbthin = new ImageIcon("res/resources/images/leveltiles/grassland/KTCTestTile.png");
    		tilebottomthin = new ImageIcon("res/resources/images/leveltiles/grassland/KTCTestTile.png");
    		tileleftthin = new ImageIcon("res/resources/images/leveltiles/grassland/KTCTestTile.png");
    		tilelrthin = new ImageIcon("res/resources/images/leveltiles/grassland/KTCTestTile.png");
    		tileright = new ImageIcon("res/resources/images/leveltiles/grassland/KTCTestTile.png");*/
     
    		setImages();
     
    	}
     
    	public void tick(){
    		super.tick();
    	}
     
    	public void render(Graphics g, Component c){
    		super.render(g, c);
    	}
     
    	public void setImages(){
    		System.out.println("What am I fighting for?");
    		tiletopleft = new ImageIcon("KTCTestTile2.png");
    		tiletop = new ImageIcon("KTCTestTile2.png");
    		tiletopright = new ImageIcon("KTCTestTile2.png");
    		tileleft = new ImageIcon("KTCTestTile2.png");
    		tile = new ImageIcon("KTCTestTile2.png");
    		tileright = new ImageIcon("KTCTestTile2.png");
    		tilebottomleft = new ImageIcon("KTCTestTile2.png");
    		tilebottom = new ImageIcon("KTCTestTile2.png");
    		tilebottomright = new ImageIcon("KTCTestTile2.png");
    		tiletopthin = new ImageIcon("KTCTestTile2.png");
    		tiletbthin = new ImageIcon("KTCTestTile2.png");
    		tilebottomthin = new ImageIcon("KTCTestTile2.png");
    		tileleftthin = new ImageIcon("KTCTestTile2.png");
    		tilelrthin = new ImageIcon("KTCTestTile2.png");
    		tileright = new ImageIcon("KTCTestTile2.png");
    	}
     
    }

    Level Class (calls the above two objects):
    package gravitygamesinteractive.scorpioengine.main;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    import javax.swing.JComponent;
     
    public class Level extends JComponent{
    	public static int minx,maxx,miny,maxy;
    	public static ArrayList<Immobileobject> objects=new ArrayList<Immobileobject>();
    	public static ArrayList<Player> players=new ArrayList<Player>();
    	public static int currentSong;
     
    	private Scanner s;
    	private int objectId,spx,spy,spId,ex2;
    	private String oidstr;
    	private String xstr;
    	private String ystr;
    	private String Idstr;
    	private String ex2str;
    	public static ArrayList<String> objarray= new ArrayList<String>();
    	public static ArrayList<String> xarray= new ArrayList<String>();
    	public static ArrayList<String> yarray= new ArrayList<String>();
    	public static ArrayList<String> Idarray=new ArrayList<String>();
    	public static ArrayList<String> exbit2array=new ArrayList<String>();
     
    	public Level(){
    		objects.add(new Ground(0,0,0,5,5,16,16,true));
    		System.out.println(objects.size());
    	}
    	public void tick(){
    		for(int e=0;e<objects.size();e++){
    			objects.get(e).tick();
    		}
    	}
     
    	public void paintComponent(Graphics g){
    		g.setColor(Color.black);
    		g.fillRect(0, 0, Frame.realsize.width, Frame.realsize.height);
     
    		for(int e=0;e<objects.size();e++){
    			objects.get(e).render(g,this);
    		}
    	}
    }
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problems With Image Loading and Extended Objects

    Where is the driver? A class with a main() method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Problems With Image Loading and Extended Objects

    Here:
    package gravitygamesinteractive.scorpioengine.main;
     
     
    import javax.swing.*;
    import java.awt.*;
     
    public class Frame{
    	private static final long serialVersionUID=1L;
    	private Image screen;
    	private static int pixelSize=2;
     
    	public static Dimension size = new Dimension(1280,720);
    	public static Dimension realsize=new Dimension(size.width/pixelSize,size.height/pixelSize);
     
    	public static String name="Scorpio Engine";
    	public static int sx=0,sy=0;
     
    	public static int dir;
    	public static boolean isMoving=false;
    	public static boolean isJumping=false;
     
    	public Frame(){
     
    }
    public static JFrame frame;
    public static Main main;
    public static void main(String args[]){
    	JFrame frame=new JFrame();
        frame.setSize(size);
        main=new Main();
        frame.add(main);
     
    	frame.setTitle(name);
    	frame.setResizable(false);
    	frame.setLocationRelativeTo(null);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setVisible(true);
    	//frame.addKeyListener(new KeyListen());
     
     
    }
     
    }

    ...and here's the Main class in case you need it:
    package gravitygamesinteractive.scorpioengine.main;
     
    import java.awt.*;
    import java.awt.image.VolatileImage;
     
    import javax.swing.JPanel;
     
    public class Main extends JPanel implements Runnable{
    	public static boolean isRunning=false;
    	public static boolean rightPressed, leftPressed, upPressed, downPressed, jumpPressed, actionPressed;
    	public Level level;
     
    	public Main(){
    		start();
    	}
     
    	public void start(){
    		level=new Level();
     
    		isRunning=true;
    		new Thread(this).start();
    	}
     
    	public void tick(){
    		level.tick();
    	}
     
    	public void paintComponent(Graphics g){
    		level.paintComponent(g);
    	}
     
    	public void run() {
     
    		while(isRunning){
    			tick();
    			repaint();
     
     
    			try{
    				Thread.sleep(16);
    			}catch(Exception e){
     
    			}
    		}
    	}
     
    }
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problems With Image Loading and Extended Objects

    When I execute the code, I get a frame with a black section in the upper left????
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Problems With Image Loading and Extended Objects

    Yeah, that was testing to make sure that the paintComponent method in the Level class was being called properly. That's not the problem though, the problem is with the Immobileobject and Ground classes. Try replacing KTCTestTile and KTCTestTile2 with other images and you should see the problem I'm talking about. The Ground object draws KTCTestTile, the image set by Immobileobject, as opposed to KTCTestTile2, which is supposed to be the one that shows up.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problems With Image Loading and Extended Objects

    Can you make a small, simple program that compiles, executes and shows the problem.
    Try replacing KTCTestTile and KTCTestTile2 with other images
    If what has been posted does not show the problem, why was it posted?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Problems With Image Loading and Extended Objects

    It will show the problem, its just that the images weren't included. If you put images in the main folder of the .jar with those names, it should show you what I'm talking about...
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problems With Image Loading and Extended Objects

    Describe what you see when the code is executed and what you want to change about the way it executes.


    I changed the image names to images I have.
    Now I get a bunch of displays of one of images in the upper left corner of the black rectangle in the upper left of the frame????
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Problems With Image Loading and Extended Objects

    Well, what I see is the first image, as well as the black rectangle in the top left. What I should be seeing is the SECOND image in the top left, as well as the black rectangle. I'm trying to override the first image with the second.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problems With Image Loading and Extended Objects

    One problem I see is the use of static variables.

    Try debugging the code by adding some println statements to show the values of the variables that are used.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Problems With Image Loading and Extended Objects

    When I put a println in the Ground object, it says the value is the second image, however, in the Level.paintComponent method, it says the object in the array has it set to the first image.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problems With Image Loading and Extended Objects

    That says there are two variables with the same name that have different contents in different parts of the objects.
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    Gravity Games (April 22nd, 2013)

  14. #13
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Problems With Image Loading and Extended Objects

    *trys commenting out the declaration of the ImageIcons in the Ground object* ...wow I feel stupid...

    Well it works now, thanks a lot for being so patient!
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problems With Image Loading and Extended Objects

    Glad you found it.

    Try not to use so many static variables also.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. loading my image
    By riddick_charel in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 26th, 2013, 06:12 AM
  2. Two problems (Dealing with Classes and Objects)
    By AustinStanley in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 4th, 2012, 08:17 PM
  3. Replies: 2
    Last Post: October 31st, 2012, 01:07 AM
  4. ImageIcon Erratically Loading URL Image
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 3rd, 2011, 04:05 PM
  5. Image problems
    By Bekuraryou1228 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 19th, 2010, 05:53 PM