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 25 of 25

Thread: TF2 Demake

  1. #1
    Member
    Join Date
    Mar 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default TF2 Demake

    I am working on making a TF2 demake for the Notch followers since he hasn't worked on it since the new game and I want to improve from where he left off.
    I am basically watching the videos and looking at how he is styling the code so everyone likes it.
    This is the videos of him doing it.
    realnotch - Not working on anything, just herping the derp.
    I am going to quickly give away my src cause I am in major trouble if I can't get this fixed.

    I am having an error on line 15 in the Art file and am trying to figure out what I did wrong. Any help is appreciated and I know I shouldn't be doing it because it is his, but most of the people asked me on the forums since I made a couple of nice mods to do it since I stopped modding. If you could help, or at least look through it, thank you.


    Here is the error message:
    Exception in thread "Game Thread" java.lang.Error: Unresolved compilation problem:
    Default constructor cannot handle exception type IOException thrown by implicit super constructor. Must define an explicit constructor

    at com.Impostor.start.Art.<init>(Art.java:15)
    at com.Impostor.start.Art.init(Art.java:12)
    at com.Impostor.start.ImpostorFortress.init(ImpostorF ortress.java:39)
    at com.Impostor.start.ImpostorFortress.run(ImpostorFo rtress.java:45)
    at java.lang.Thread.run(Unknown Source)
    Since I don't know how to do the code type on this forum I am going to put it in quotes till I find out.
    ImpostorFortress.java
    package com.Impostor.start;
     
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.image.*;
     
    import javax.swing.JFrame;
     
    public class ImpostorFortress extends Canvas implements Runnable {
    	private static final long serialVersionUID = 1L;
     
    	private static final int WIDTH = 320;
    	private static final int HEIGHT = 240;
    	private static final int SCALE = 2;
     
    	private boolean keepRunning = true;
    	private BufferedImage screenImage;
    	private BitMap screenBitMap;
     
    	public ImpostorFortress() {
    		Dimension size = new Dimension(WIDTH*SCALE, HEIGHT*SCALE);
     
    		setPreferredSize(size);
    		setMaximumSize(size);
    		setMinimumSize(size);
    	}
     
    	public void start() {
    		new Thread(this, "Game Thread").start();
    	}
     
    	public void stop() {
    		keepRunning = false;
    	}
     
    	public void init() {
    		Art.init();
    		screenImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
    		screenBitMap = new BitMap(screenImage);
    	}
     
    	public void run() {
    		init();
     
    		while (keepRunning) {
    			tick();
    			render(screenBitMap);
    			try {
    			Thread.sleep(10);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    			swap();
    		}
    	}
     
    	private void swap() {
    		BufferStrategy bs = getBufferStrategy();
    		if (bs==null) {
    			this.createBufferStrategy(2);
    			return;
    		}
    		Graphics g = bs.getDrawGraphics();
    		int screenW = getWidth();
    		int screenH = getHeight();
    		int w = WIDTH*SCALE;
    		int h = HEIGHT*SCALE;
     
    		g.setColor(Color.BLACK);
    		g.fillRect(0, 0, screenW, screenH);
    		g.drawImage(screenImage, (screenW*w)/2, (screenH*h)/2, w, h, null);
    		g.dispose();
     
    		bs.show();
    	}
     
    	private void render(BitMap screen) {
    		screen.draw(Art.i.sprites[0][0], 0, 0);
    	}
     
    	private void tick() {
    	}
     
    	public static void main(String[] args) {
    		ImpostorFortress gameComponent = new ImpostorFortress();
     
    		JFrame frame = new JFrame("Impostor Fortress");
    		frame.add(gameComponent);
    		frame.pack();
    		frame.setResizable(false);
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
     
    		gameComponent.start();
    	}
     
     
    }


    Art.java

    package com.Impostor.start;
     
    import java.awt.image.BufferedImage;
    import java.io.IOException;
     
    import javax.imageio.ImageIO;
     
    public class Art {
    	public static Art i;
     
    	public static void init() {
    		i = new Art();
    	}
     
    //This BitMap[][] sprites line is the one with the error and the only one
    	public BitMap[][] sprites = loadAndCut("/chars.png", 16, 16);
     
    	private BitMap[][] loadAndCut(String name, int sw, int sh) throws IOException {
    		BufferedImage img;
    		try {
    			img = ImageIO.read(Art.class.getResource(name));
    		} catch (IOException e) {
    		throw new RuntimeException("Failed to load " + name);
    	}
    		int xSlices = img.getWidth()/sw;
    		int ySlices = img.getHeight()/sh;
     
    		BitMap[][] result = new BitMap[xSlices][ySlices];
    		for (int x=0; x < xSlices; x++) {
    			for (int y=0; y < ySlices; y++) {
    			result[x][y] = new BitMap(sw, sh);
    			img.getRGB(x * sw, y * sh, sw, sh, result[x][y].pixels, 0, sw);
    			}
    		}
    		return result;
    	}

    BitMap.java
    package com.Impostor.start;
     
    import java.awt.image.BufferedImage;
    import java.awt.image.DataBufferInt;
     
    public class BitMap {
    	public final int[] pixels;
    	public final int w, h;
     
    	public BitMap(int w, int h) {
    		this.w = w;
    		this.h = h;
    		pixels = new int[w*h];
    	}
     
    	public BitMap(int w, int h, int[] pixels) {
    		this.w = w;
    		this.h = h;
    		this.pixels = pixels;
    	}
     
    	public BitMap(BufferedImage img) {
    		this.w = img.getWidth();
    		this.h = img.getHeight();
    		pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
    	}
     
    	public void draw(BitMap b, int xp, int yp) {
    		int x0 = xp;
    		int x1 = xp + b.w;
    		int y0 = yp;
    		int y1 = yp + b.h;
    		if (x0 < 0) x0 = 0;
    		if (y0 < 0) y0 = 0;
    		if (x1 > w) x1 = w;
    		if (y1 > h) y1 = h;
     
    		for (int y=y0; y<y1; y++) {
    			int sp = (y-y0) * b.w - xp;
    			int dp = (y) * b.w;
     
    			for (int x = x0; x < x1; x++) {
    				pixels[dp + x] = b.pixels[sp + x];
    			}
    		}
    	}
    }

    Thank you if you can help.
    Last edited by Chillers; March 23rd, 2012 at 03:28 PM. Reason: To add in code.


  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: TF2 Demake

    error on line 15
    Please post the full text of the error message and the lines of code that cause it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: TF2 Demake

    Ok no problem, but what's wrong with the source code?
    I am editing the first post with the error and will add the source

  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: TF2 Demake

    what's wrong with the source code?
    Can't guess until you post the error message and the code.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    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: TF2 Demake

    Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get
    highlighting

    The error message says:
    Default constructor cannot handle exception type IOException thrown by implicit super constructor.
    Must define an explicit constructor
    Can you compile the code with a compiler that generates compile time messages? Try using the javac commmand.
    Your IDE's messages are different from the standard javac compiler's messages.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Mar 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: TF2 Demake

    Ok I will try compiling it, but I have some weird thing with compiling like that so give me some time to try and fix it again (reason why I use a IDE)
    Fixing code look up now.

  7. #7
    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: TF2 Demake

    Did you try adding a constructor to the Art class that "throws" an exception as the error message suggested?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Mar 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: TF2 Demake

    I don't mean to double post, but I can't compile it correctly and I already have too much stuff in my c drive and I put all my things on my desktop so I can edit the pictures easier.

  9. #9
    Member
    Join Date
    Mar 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: TF2 Demake

    	public BitMap[][] sprites = loadAndCut("/chars.png", 16, 16);
     
    	private BitMap[][] loadAndCut(String name, int sw, int sh) throws IOException {
    		BufferedImage img;
    		try {
    			img = ImageIO.read(Art.class.getResource(name));
    		} catch (IOException e) {
    		throw new RuntimeException("Failed to load " + name);
    	}
    Yes, but I don't see what else to add, maybe an else statement?

  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: TF2 Demake

    Did you try adding a constructor to the Art class that "throws" an exception as the error message suggested?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Mar 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: TF2 Demake

    Yes it will catch the exception and throw an exception telling what is missing, but I think it has to do with line 15 cause it is out of syntax

  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: TF2 Demake

    Post the code om line 15 and the error message for it.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Mar 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: TF2 Demake

    I have posted the code and the error message, but I can't compile it cause my computers java is wack and doesn't compile, but I use IDE's for compiling.

  14. #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: TF2 Demake

    I dont see that You have not posted the new code with the new Art class constructor.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Mar 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: TF2 Demake

    It's been in there. . .
    If it's anything else you are free to edit it and I will try it, but I don't what you mean cause I haven't really grasped what a constructor is yet.

  16. #16
    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: TF2 Demake

    It's been in there. . .
    I can not see it. Can you copy and paste here the code you are talking about?

    Read this about how to create a class and use constructors:
    Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Mar 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: TF2 Demake

    Here is the error code
    	public BitMap[][] sprites = loadAndCut("/chars.png", 16, 16);
    Here is the full class in art
    package com.Impostor.start;
     
    import java.awt.image.BufferedImage;
    import java.io.IOException;
     
    import javax.imageio.ImageIO;
     
    public class Art {
    	public static Art i;
     
    	public static void init() {
    		i = new Art();
    	}
     
    	public BitMap[][] sprites = loadAndCut("/chars.png", 16, 16);
     
    	private BitMap[][] loadAndCut(String name, int sw, int sh) throws IOException {
    		BufferedImage img;
    		try {
    			img = ImageIO.read(Art.class.getResource(name));
    		} catch (IOException e) {
    		throw new RuntimeException("Failed to load " + name);
    	}
    		int xSlices = img.getWidth()/sw;
    		int ySlices = img.getHeight()/sh;
     
    		BitMap[][] result = new BitMap[xSlices][ySlices];
    		for (int x=0; x < xSlices; x++) {
    			for (int y=0; y < ySlices; y++) {
    			result[x][y] = new BitMap(sw, sh);
    			img.getRGB(x * sw, y * sh, sw, sh, result[x][y].pixels, 0, sw);
    			}
    		}
    		return result;
    	}
    }
    Also if I get constructors it is to get things from other classes?
    I learned from thenewboston for most things and I went to learning it from java (I only been learning for a month)
    so like
     Water ice = new Water (#,#,#);

  18. #18
    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: TF2 Demake

    The first code you posted is not a constructor.
    Try adding the constructor to the Art class.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Mar 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: TF2 Demake

    Like how would I do that though?
    Cause I need it to load and cut that image and break it into 16 by 16 pixels so I can use my characters I have made.

  20. #20
    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: TF2 Demake

    Are you asking how to write the code to define a constructor to the Art class?

    See the tutorial:
    Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Here are some constructors that you have defined:
    public BitMap(int w, int h) {
    public ImpostorFortress() {
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Mar 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: TF2 Demake

    I know, but what would I use it though?
    I can make things if I know what their use should be

  22. #22
    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: TF2 Demake

    You write it because the compiler wants one defined. See the error message.
    Must define an explicit constructor
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Member
    Join Date
    Mar 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: TF2 Demake

    Yes ok let me get working on one

    public BitMap(BufferedImage img) {
    		this.w = img.getWidth();
    		this.h = img.getHeight();
    		pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
    	}
    Should I move the pixels = part to the top cause it would change my error so that it would first recieve image file then the width and height.
    Last edited by Chillers; March 23rd, 2012 at 04:13 PM.

  24. #24
    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: TF2 Demake

    The postion of that line of code should not cause any problems.
    All three lines reference the method's argument and are not related.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Member
    Join Date
    Mar 2012
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: TF2 Demake

    I am reading a book on 2D graphics now so I think I will do all its tests and everything then come back to this project.
    I want to eventually make this Twitter