Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding an image to frame, not in center, but should be.. + game programming in general
    primarykey
    data
    text
    <p>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!</p> <p>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:</p> <p>The object:</p> <pre><code> 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; } } </code></pre> <p>The panel:</p> <pre><code> 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) { } } </code></pre> <p>The main:</p> <pre><code> 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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload