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); } } }