Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6006083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:29:21+00:00 2026-05-23T01:29:21+00:00

I’ve been told by my friend that my code isn’t very OO. How would

  • 0

I’ve been told by my friend that my code isn’t very OO. How would I make this more OO and reusable?
I tried making a class called level, that printed the images in level, but noting printed, so that didn’t work.

(code dump coming, I’m sorry)

public class Main extends Applet implements Runnable, KeyListener,
java.awt.event.MouseListener {
double x_pos = 300;
double y_pos = 200;
int radius = 20;
int appletsize_x = 640;
int appletsize_y = 440;
double speed = 3;
float speedModifier = 1f;
Image grass;
Image hero;
Image hero45;
Image hero90;
Image hero135;
Image hero180;
Image hero225;
Image hero270;
Image hero315;
Image sprite;
Image tree;
Image path;

private boolean leftPressed;
private boolean rightPressed;
private boolean downPressed;
private boolean upPressed;

private Image dbImage;
private Graphics dbg;

public void init() {
    Dimension dim = getMaximumSize();
    this.setSize(appletsize_x, appletsize_y);
    MediaTracker mt = new MediaTracker(this);
    tree = getImage(getCodeBase(), "tree_64.png");
    grass = getImage(getCodeBase(), "grassTile.png");
    path = getImage(getCodeBase(), "path.png");
    hero = getImage(getCodeBase(), "hero.png");
    hero45 = getImage(getCodeBase(), "hero45.png");
    hero90 = getImage(getCodeBase(), "hero90.png");
    hero135 = getImage(getCodeBase(), "hero135.png");
    hero180 = getImage(getCodeBase(), "hero180.png");
    hero225 = getImage(getCodeBase(), "hero225.png");
    hero270 = getImage(getCodeBase(), "hero270.png");
    hero315 = getImage(getCodeBase(), "hero315.png");
    sprite = getImage(getCodeBase(), "hero.png");
    mt.addImage(hero, 0);
    mt.addImage(path, 0);
    mt.addImage(tree, 0);
    mt.addImage(grass, 0);
    mt.addImage(hero45, 0);
    mt.addImage(hero90, 0);
    mt.addImage(hero135, 0);
    mt.addImage(hero180, 0);
    mt.addImage(hero225, 0);
    mt.addImage(hero270, 0);

    try {
        mt.waitForID(0);
    } catch (InterruptedException ie) {
    }
}

public void start() {

    this.addKeyListener(this);
    this.addMouseListener(this);
    Thread th = new Thread(this);
    th.start();
}

public void stop() {

}

public void destroy() {

}

public void run() {

    // lower ThreadPriority
    this.requestFocusInWindow();
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

    while (true) {

        if (downPressed && leftPressed) {
            double y_decr, x_incr;
            y_decr = Math.sqrt(Math.pow(speed, 2)) / 1.5;
            x_incr = Math.sqrt(Math.pow(speed, 2)) / 1.5;
            y_pos += y_decr;
            x_pos -= x_incr;
        } else if (downPressed && rightPressed) {
            y_pos += Math.sqrt(Math.pow(speed, 2)) / 1.5;
            x_pos += Math.sqrt(Math.pow(speed, 2)) / 1.5;
        } else if (upPressed && rightPressed) {
            y_pos -= Math.sqrt(Math.pow(speed, 2)) / 1.5;
            x_pos += Math.sqrt(Math.pow(speed, 2)) / 1.5;
        } else if (upPressed && leftPressed) {
            y_pos -= Math.sqrt(Math.pow(speed, 2)) / 1.5;
            x_pos -= Math.sqrt(Math.pow(speed, 2)) / 1.5;
        } else {
            // Hitting (right)
            if (x_pos > this.getSize().width - radius) {

                // x_speed = -x_speed;
            }
            if (leftPressed == true) {
                x_pos -= speed * speedModifier;
            }

            if (rightPressed == true) {
                x_pos += speed * speedModifier;
            }

            if (upPressed == true) {
                y_pos -= speed * speedModifier;
            }

            if (downPressed == true) {
                y_pos += speed * speedModifier;

            }
        }

        // Hitting right

        if (x_pos > this.getSize().width - 32) {
            x_pos = this.getSize().width - 32;

        } // Hitting left
        if (x_pos < 0) {
            x_pos = 0;
        }
        // } // Hitting top
        if (y_pos < 0) {
            y_pos = 0;

        }

        // Hitting bottom
        if (y_pos > this.getSize().height - 32) {
            y_pos = this.getSize().height - 32;

        }


        repaint();

        try {
            // Stop thread for 1 milliseconds
            Thread.sleep(20);
        } catch (InterruptedException ex) {
            // do nothing
        }

        // set ThreadPriority to maximum value
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    }
}

public void paint(Graphics g) {
    int layout = 0;
    int coll = 0;
    while (coll <= 440) {
        drawRows(layout, grass, g, coll);
        coll = coll + 32;
    }
    drawCollums(0, path, g, 180);
    g.drawImage(tree, 50, 40, this);
    g.drawImage(tree, 300, 20, this);
    g.drawImage(tree, 500, 300, this);
    int x_posI = (int) x_pos;
    int y_posI = (int) y_pos;

    if (downPressed && leftPressed) {
        this.sprite = hero225;
    } else if (downPressed && rightPressed) {
        this.sprite = hero135;
    } else if (upPressed && rightPressed) {
        this.sprite = hero45;
    } else if (upPressed && leftPressed) {
        this.sprite = hero315;
    } else if (leftPressed == true) {
        this.sprite = hero270;
    } else if (rightPressed == true) {
        this.sprite = hero90;
    } else if (upPressed == true) {
        this.sprite = hero;
    } else if (downPressed == true) {
        this.sprite = hero180;
    }

    // this.sprite will contain value set on last "movement"
    g.drawImage(this.sprite, x_posI, y_posI, this);


}



public void update(Graphics g) {

    if (dbImage == null) {
        dbImage = createImage(this.getSize().width, this.getSize().height);
        dbg = dbImage.getGraphics();
    }

    dbg.setColor(getBackground());
    dbg.fillRect(0, 0, this.getSize().width, this.getSize().height);

    dbg.setColor(getForeground());
    paint(dbg);

    g.drawImage(dbImage, 0, 0, this);
}

@Override
public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_LEFT) {
        leftPressed = true;

    }
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
        rightPressed = true;

    }
    if (e.getKeyCode() == KeyEvent.VK_UP) {
        upPressed = true;
    }
    if (e.getKeyCode() == KeyEvent.VK_DOWN) {
        downPressed = true;
    }

}

@Override
public void keyReleased(KeyEvent e) {

    if (e.getKeyCode() == KeyEvent.VK_LEFT) {
        leftPressed = false;
    }
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {

        rightPressed = false;
    }
    if (e.getKeyCode() == KeyEvent.VK_UP) {
        upPressed = false;

    }
    if (e.getKeyCode() == KeyEvent.VK_DOWN) {
        downPressed = false;
    }
}

@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void mouseClicked(MouseEvent e) {
    System.out.println("HIT!");
    // TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}

public void drawRows(int x, Image image, Graphics g, int coll) {
    while (x <= appletsize_x) {
        g.drawImage(image, x, coll, this);
        x += 32;
    }
}

public void drawCollums(int y, Image image, Graphics g, int coll) {
    while (y <= appletsize_x) {
        g.drawImage(image, coll, y, this);
        y += 32;
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:29:21+00:00Added an answer on May 23, 2026 at 1:29 am

    Well for one thing, all those images should be in an array. And they should contain a filename so you can load them in a nice loop. You have over 3 pages of copy&paste code, that’s absolutely horrible… What if you want to change the way they get loaded slightly?

    Then, Math.sqrt(Math.pow(speed, 2)) jumps out at me. That’s the equivalent of Math.abs(speed), and about 1/200x as fast.

    This next part is particularly funny too. I won’t comment on whether or not it’s needed (and it most likely isn’t, it’s a design flaw on your part), but.. ya..

            // Stop thread for 1 milliseconds
            Thread.sleep(20);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a jquery bug and I've been looking for hours now, I can't
I'm looking for suggestions for debugging... If you view this site in Firefox or
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.