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

Thread: Need help finding a problem with a few game classes one of them being an applet.

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help finding a problem with a few game classes one of them being an applet.

    I'm having an annoying issue where one of my classes is causing my applet not to run.

    I've narrowed down the issue to the ImageEntity class and I'm stumped as to where the problem is in it and how to make it go away.

    The class was originally setup to be used by a JFrame but I've been trying to make it usable to the applet but it doesn't seem to be working.

    Here's the code:

    [Java] ThreeJavaClassesIssue - Pastebin.com

    I've only included the classes that are having the issue but there are a few others I could post the code for if needed.

    All three of these classes are from various class projects and the book we are working from never made any mention of an issue like this at the current time and most likely assumed we would know what we were doing by this point. Obviously not.
    Last edited by StormWingDelta; February 23rd, 2012 at 11:03 PM. Reason: Errors


  2. #2
    Junior Member
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Need help finding a problem with a few game classes one of them being an applet.

    Is there an error or anything? You just say it causes your applet not to run. Is there any way to can elaborate? It will make it easier for us to help you.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help finding a problem with a few game classes one of them being an applet.

    The applet won't initialize when run with the ImageEntity class. It doesn't say any errors at all and would be nice if it did. I'm trying to switch it over to ImageIO at the moment but I'm more confused than ever.

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help finding a problem with a few game classes one of them being an applet.

    Solved the issue. I can't believe how simple it was to solve. Me and the teacher went and downloaded the source code for the project that the book was showing and all they did was drop all the things to do with JFrame and replace them with Applet.
    The book never mentioned to do this but the source code shows it was done.

    I still want to figure out a way of using ImageIO the same way this class is used though. Any ideas?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,416
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Need help finding a problem with a few game classes one of them being an applet.

    a way of using ImageIO
    Please explain.
    Post the code that shows the problem and explain how it is being used.

  6. #6
    Junior Member
    Join Date
    Jan 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help finding a problem with a few game classes one of them being an applet.

    Here's the fixed code for the Image Class.
    It gets an image from file and displays it in an applet that it is being used with.
    It works fine but could use some tweaks to be a bit more useful.
     
    import java.awt.*;
    import java.awt.Graphics2D;
    import java.awt.geom.*;
    import java.applet.*;
    import java.net.*;
     
    public class ImageEntity extends BaseGameEntity {
        //variables
        private Image image;
        private Applet applet;
        private AffineTransform at;
        private Graphics2D g2d;
     
        //default constructor
        ImageEntity(Applet a) {
            applet = a;
            setImage(null);
            setAlive(true);
        }
     
        //sets and returns the entity's image object
        public Image getImage() { return image; }
        public void setImage(Image image) { this.image = image; }
     
        //returns the width and height of the entity
        public int width() {
            return getImage().getWidth(applet);
        }
        public int height() {
            return getImage().getHeight(applet);
        }
     
        //returns the center of the entity in pixels
        public double getCenterX() {
            return getX() + width() / 2;
        }
        public double getCenterY() {
            return getY() + height() / 2;
        }
     
        //set reference to the drawing object
        public void setGraphics(Graphics2D g) {
            g2d = g;
        }
     
         private URL getURL(String filename) {
             URL url = null;
             try {
                 url = this.getClass().getResource(filename);
             }
             catch (Exception e) { }
             return url;
         }
     
        //load an image file
        public void load(String filename) {
            setImage(applet.getImage(getURL(filename)));
            while(getImage().getWidth(applet) <= 0);
            double x = applet.getSize().width/2  - width()/2;
            double y = applet.getSize().height/2 - height()/2;
            at = AffineTransform.getTranslateInstance(x, y);
        }
     
        //move and rotate the entity
        public void transform() {
            at.setToIdentity();
            at.translate((int)getX() + width()/2, (int)getY() + height()/2);
            at.rotate(Math.toRadians(getFaceAngle()));
            at.translate(-width()/2, -height()/2);
        }
     
        //draw the entity
        public void draw() {
            g2d.drawImage(getImage(), at, applet);
        }
     
        //bounding rectangle
        public Rectangle getBounds() {
            return new Rectangle((int)getX(),(int)getY(),width(),height());
        }
     
    }

    What I'm confused about is how to use ImageIO to do the same thing and make use of things like RescaleOp at the same time for things like a cloaking system for sprites.

    I've seen some codes for this I'm just not sure how to use them.
    Last edited by StormWingDelta; February 24th, 2012 at 08:36 PM. Reason: Fixing

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,416
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Need help finding a problem with a few game classes one of them being an applet.

    how to use ImageIO to do the same thing
    Did you read the API doc for ImageIO and do you have any questions about how to use it.

Similar Threads

  1. Mobile Game Development Classes
    By skj.kamal in forum Java ME (Mobile Edition)
    Replies: 3
    Last Post: August 16th, 2012, 04:12 AM
  2. Help with game applet
    By skerridge in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 28th, 2012, 01:24 PM
  3. Problem with Java Classes, in an applet
    By SashaThompson in forum Object Oriented Programming
    Replies: 12
    Last Post: October 2nd, 2011, 07:15 AM
  4. Dice Wagering Game new to Multiple Classes
    By Laxman2809 in forum What's Wrong With My Code?
    Replies: 24
    Last Post: September 16th, 2011, 08:22 PM
  5. Need help with java applet game.
    By vlan in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 10th, 2010, 04:18 AM