Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Scrolling on X axis only  (Read 301 times)
0 Members and 1 Guest are viewing this topic.
Offline Oxy

Junior Newbie





« Posted 2013-04-15 03:20:40 »

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;
         }
      }
   
}
Offline Oxy

Junior Newbie





« Reply #1 - Posted 2013-04-15 03:28:22 »

Solved it. If anyone is able to close this thread, because it wont let me, that would be great. Sorry to bother anyone.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #2 - Posted 2013-04-15 06:00:50 »

Not really closing of threads around here, but next time put your code in the code tags Wink
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (12 views)
2014-07-29 18:09:19

Riven (8 views)
2014-07-29 18:08:52

Dwinin (9 views)
2014-07-29 10:59:34

E.R. Fleming (26 views)
2014-07-29 03:07:13

E.R. Fleming (10 views)
2014-07-29 03:06:25

pw (40 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (27 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!