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

  • SEARCH
  • Home
  • 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 9118625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:11:55+00:00 2026-06-17T05:11:55+00:00

I just started learning game programming, after taking a 1 semester course in the

  • 0

I just started learning game programming, after taking a 1 semester course in the university, so i think im ready and i really want to do this, so first of all:
1) What could be good resources for learning? Ive googlef a lot and have two books: Killer Game Programming in Java and Begining java SE6 game programing. One is overly specific and not specific at the same time, the other explains very little, so its difficult understanding what is what. Esspecialy with rendering, the buffers, how to render to applets, frames and panels. Any help would be greatly appriciated 😉 Thank you!

Ive writen a very basic code, to move a box around, everything is well, but the box isnt in the middle as it should be:

The object:

       package milk;


    public class Thing 
    {
        double x,y;
        Shape shape;
        int[] thingx={-5,5,5,-5};
        int[] thingy={5,5,-5,-5};

    Thing(double x, double y)
    {
        setX(x);
        setY(y);
        setShape();
    }

    public void setX(double x)
    {
        this.x=x;
    }

    public void setY(double y)
    {
        this.y=y;
    }

    public void setShape()
    {
        this.shape=new Polygon(thingx,thingy,thingx.length);
    }

    public double getX()
    {
        return x;
    }

    public double getY()
    {
        return y;
    }

    public Shape getShape()
    {
        return shape;
    }

    public void incX(int i)
    {
        this.x+=i;
    }

    public void incY(int i)
    {
        this.y+=i;
    }

}

The panel:

    package milk;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.AffineTransform;

import javax.swing.JPanel;

@SuppressWarnings("serial")
public class MilkPanel extends JPanel implements Runnable, KeyListener{

    Thread animator;
    Graphics2D g2d;
    Thing thing=new Thing(320,240);
    AffineTransform identity = new AffineTransform();

    MilkPanel()
    {
        setSize(640,480);
        setBackground(Color.black);
        setFocusable(true);
        requestFocus();
        addKeyListener(this);
    }

    public void addNotify() 
    {
        super.addNotify();
        animator = new Thread(this);
        animator.start();
    }

    public void paint(Graphics g)
    {
        g2d=(Graphics2D)g;

        g2d.setColor(Color.black);
        g2d.fillRect(0, 0, getSize().width,getSize().height);
        drawThing();
        g.dispose();
    }

    public void drawThing()
    {
        g2d.setTransform(identity);
        g2d.translate(thing.getX(), thing.getY());
        g2d.setColor(Color.orange);
        g2d.draw(thing.getShape());
    }
    public void run()
    {
        while(true)
        {
            try
            {
                Thread.sleep(20);
            }
            catch(Exception e)
            {

            }
            repaint();
        }
    }

    public void keyPressed(KeyEvent e) 
    {
        int key=e.getKeyCode();
        switch(key)
        {
        case KeyEvent.VK_UP:
            thing.incY(-5);
        break;

        case KeyEvent.VK_DOWN:
            thing.incY(5);
        break;

        case KeyEvent.VK_RIGHT:
            thing.incX(5);
        break;

        case KeyEvent.VK_LEFT:
            thing.incX(-5);
        break;
        }
    }

    public void keyReleased(KeyEvent e) {   }

    public void keyTyped(KeyEvent e) {  }




}

The main:

  package milk;

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class MilkIt extends JFrame {

    public MilkIt() {

        add(new MilkPanel());

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(640,480);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        new MilkIt();
    }
}
  • 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-06-17T05:11:57+00:00Added an answer on June 17, 2026 at 5:11 am

    It looks like you have set the position of the box to the point (320,240), which is indeed half of (640,480). The X,Y position of an object will actually be its topleft corner, however. Additionally, you will likely want to make a method for setting this information generically, as opposed to hard-coding it.

    If you want to find an object’s center position on a given axis (this works for X, Y, or Z, whether you’re working in 2D or 3D (or more!?)), you want to take half of its size on that axis and subtract if from its position (which is actually a corner); the result will be the center.

    The algorithm you’re looking for is essentially this:

    xPos = (screenXSize / 2) - (xSize / 2);
    yPos = (screenYSize / 2) - (ySize / 2);
    

    To standardize it even further, consider putting your variables in arrays based on how many dimensions you’re using – then what I said about using either 2D or 3D automatically applies no matter what you’re doing (you can even mix and match 2D and 3D elements in the same game, as is common for certain reasons).

    for (int i = 0; i < DIMENSIONS; i++) {
        pos[i] = (screenSize[i] / 2) - (size[i] / 2);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started learning some ruby, and I want to do something like this:
just started learning Assembler in school, and want to ask how to compile this
I just started learning ASP.NET MVC4 today. After reading tutorials, downloading VS 2012, and
I just started learning C but I don't understand this part of the code.
Just started learning Haskell. I have an empty source file with this inside: pe
Just started learning Groovy, got the PragProg book Programming Groovy and had a problem
I just started learning .NET so chances are this is a big n00b mistake:
I just started learning C++ and am currently using codeblocks. I want to write
Am new to android programming, just started learning it the past 6 weeks and
I just started learning JS the other day and am (of course) having some

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.