Adding an image to frame, not in center, but should be.. + game programming in general

StackOverflow https://stackoverflow.com/questions/14241178

  •  14-01-2022
  •  | 
  •  

Domanda

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();
    }
}
È stato utile?

Soluzione

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);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top