Hey guys, I’m new to this stuff and i would really appreciate some help on this. Working on a game similar to Age of Empires. the only scrolling in this game is on the X axis. The screen that appears when you play it is 640pixels long while my background image is 1280 pixels long. i cannot figure out how to scroll to the right / left and keeping the image intact. As i move right what ever was on the end of the picture just repeats itself. Here is the code, any help would be greatly appreciated. Thanks!
package EvolutionOfWar.FrameWorkPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
import EvolutionOfWar.SpriteSheetStuff.SpriteSheet;
public class Panel extends JPanel implements Runnable{
private static final long serialVersionUID = 1L;
//double buffering
private Image dbImage;
private Graphics dbg;//doubleBufferGraphics
//JPanel variables
static final int gWidth = 640, gHeight = 480;
static final Dimension gameDim = new Dimension(gWidth, gHeight);
//Game variables
private Thread thread;
private volatile boolean running = false;
private int renderX = 0,
lx = 0, rx = 1280, lrx = 30, rwx = 530;
private boolean moveRight = false, moveLeft = false;
//keys
//GameObjects
public Panel(){
setPreferredSize(gameDim);
setBackground(Color.WHITE);
setFocusable(true);
requestFocus();
HandlerClass handler = new HandlerClass();
addMouseListener(handler);
addMouseMotionListener(handler);
}
private class HandlerClass implements MouseListener, MouseMotionListener{
public void mouseClicked(MouseEvent event) {
}
public void mouseEntered(MouseEvent event) {
}
public void mouseExited(MouseEvent event) {
}
public void mousePressed(MouseEvent event) {
}
public void mouseReleased(MouseEvent event) {
}
public void mouseDragged(MouseEvent event) {
}
public void mouseMoved(MouseEvent event) {
int x = event.getX();
System.out.println(x + " " + rwx);
if (x > rwx && renderX < 1280){
moveRight = true;
System.out.println("Moving Right");
}
if ( x < rwx ) moveRight = false;
}
}
public void run(){
long lastTime = System.nanoTime();
final double amountOfTicks = 60.0;
double ns = 1000000000.0 / amountOfTicks;
double delta = 0;
int updates = 0;
int frames = 0;
long timer = System.currentTimeMillis();
while (running){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
if (delta >= 1) {
gameUpdate();
updates++;
delta--;
}
gameRender();
paintScreen();
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println(updates + " Ticks, " + frames + " Frames");
updates = 0;
frames = 0;
}
}
stopGame();
}
private void gameUpdate(){
if (running && thread != null){
//update game state.
updateScrolling();
}
}
private void updateScrolling(){
if (moveRight) renderX-=5;
if (moveLeft) renderX+=5;
}
private void gameRender(){
if (dbImage == null){ //create buffer
dbImage = createImage(gWidth, gHeight);
if (dbImage == null){
System.err.println("dbImage is stilll nulL!");
return;
}else{
dbg = dbImage.getGraphics();
}
}
//clear the screen.
dbg.setColor(Color.white);
dbg.fillRect(0, 0, gWidth, gHeight);
//draw Game elements
draw(dbg);
}
/* Draw all game content in this method */
public void draw(Graphics g){
super.paintComponents(g);
Graphics2D g2d = (Graphics2D)g;
g.drawImage(SpriteSheet.background, renderX, 0, null);
}
private void paintScreen(){
Graphics g;
try{
g = this.getGraphics();
if (dbImage != null && g != null){
g.drawImage(dbImage, renderX, 0, getWidth(), getHeight(),null);
}
g.dispose();
}catch (Exception e){
System.err.println(e);
}
}
public void addNotify(){
super.addNotify();
startGame();
}
private void startGame(){
if(thread == null || !running){
thread = new Thread(this);
thread.start();
running = true;
}
}
public void stopGame(){
if (!running) return;
if (running){
running = false;
}
}
}