Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (553)
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  
  Using JScrollPane as the camera for a top-down shooter  (Read 2560 times)
0 Members and 1 Guest are viewing this topic.
Offline xXShadowCowXx

Senior Newbie





« Posted 2012-02-29 04:20:17 »

Hey all,

New here, but the community seems great. As the title says, I'm trying to use a JScrollPane as a camera in my game. My main problem is that I can't center the camera on the player. I can make the camera scroll when the player hits the edge with scrollRectToVisible(), but even that breaks after a bit, allowing the character to walk off screen. A good example of what I'm trying to do is Notch's Left 4k Dead. I looked at the source for that, but everything seems to be somewhat hacked together. I have looked at the Java API for methods that would help me follow the player but none of them seem to work. I've toyed around with changing the underlying JViewPort but that didn't work either.

Any help would be appreciated with this problem.
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-02-29 04:27:42 »

I don't see any point of using JScrollPane as a scrolling map. In fact, you shouldn't be using Swing to make games at all. It is best to just stick to pure AWT and Graphics2D.

To scroll, it is best to make a Camera class that only stores an X and Y offset. Then in your paint method before you draw anything, translate the transform:
1  
2  
3  
4  
5  
6  
7  
8  
9  
public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
   
    g2.translate(myCamera.getXOffset(),myCamera.getYOffset());
   
    //draw the world

    //to un-translate:
   g2.translate(-myCamera.getXOffset(),-myCamera.getYOffset());

Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Reply #2 - Posted 2012-02-29 04:37:30 »

I would do something to what ra4king is saying, but I would make a Followable interface with .getX() and .getY().  Have any class that you want to be able to follow implement the interface.  You then could do something like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
//Your variable
Followable following;

//Somewhere else:
following = player1;

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
   
    g2d.translate(following.getX(), following.getY());
   
    //draw stuff


You could then have fancy stuff, like a Camera class that can be followed.  It could move over to where something is going on, and then move back to Player1.  etc... 

It's java, so it's all up to your imagination.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-02-29 04:40:00 »

To center a player (or any "Followable") in the middle of the screen:

1  
g2.translate(-following.getX() + screenWidth/2, -following.getY() + screenHeight/2);

Offline xXShadowCowXx

Senior Newbie





« Reply #4 - Posted 2012-02-29 04:42:18 »

Thanks for the replies. I'll start restructuring everything to run on a canvas. I've just been drawing to a class that extended JPanel.
Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Reply #5 - Posted 2012-02-29 04:42:35 »

To center a player (or any "Followable") in the middle of the screen:

1  
g2.translate(-following.getX() + screenWidth/2, -following.getY() + screenHeight/2);


Yea, I was just being cheap.
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2012-02-29 04:44:11 »

Thanks for the replies. I'll start restructuring everything to run on a canvas. I've just been drawing to a class that extended JPanel.
Just curious, why did you decide to use JPanel? I'm trying to track down this widespread recommendation of JPanel usage. The most serious issue of why JPanel is bad is that it doesn't accept input events by default!

Canvas is the best choice to draw on and, combined with BufferStrategy, will give you optimal performance.

Offline xXShadowCowXx

Senior Newbie





« Reply #7 - Posted 2012-02-29 04:48:57 »

It's actually stemming from a couple colleagues at school. I asked about canvas and they said that I shouldn't bother and it wouldn't help performance very much. So I just went with JPanel and it seemed simple enough so I stuck with it.
Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Reply #8 - Posted 2012-02-29 04:53:47 »

Well, IMHO, I think they're wrong.
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #9 - Posted 2012-02-29 05:01:59 »

LOL! Bunch of clueless colleagues Grin

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline xXShadowCowXx

Senior Newbie





« Reply #10 - Posted 2012-02-29 05:34:33 »

Canvas is giving me some problems drawing. Anything explicitly different than JPanel that would be causing me problems? I have the BufferStrategy and such. When it did draw all it did was flicker.
Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Reply #11 - Posted 2012-02-29 05:58:45 »

My Paint method looks, like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public void paint(Graphics g) {
     
      Graphics2D b = (Graphics2D) bufferStrategy.getDrawGraphics();
       //b.clearRect(0, 0, WIDTH, HEIGHT);
      b.drawImage(imgDbl, 0, 0, screenWidth, screenHeight, null);
       //b.drawImage(imgAll, 0, 0, null);
      b.dispose();
       bufferStrategy.show();
     
   }


Note:  My main class extends Frame, and a canvas is added to it.
Offline xXShadowCowXx

Senior Newbie





« Reply #12 - Posted 2012-02-29 07:04:29 »

This would be basically what I have without all the extra methods and stuff for my actual game. No matter what I try this will just not draw.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
public class Game extends Canvas
{
   BufferStrategy strategy;

   Game()
   {
     /*
      *   Frame crap!
      *
      */

       
      //Make the frame and a panel to put stuff in
     JFrame frame = new JFrame("GAME");
      frame.setPreferredSize(new Dimension(1024,768));
     
      JPanel panel = (JPanel) frame.getContentPane();
      panel.setPreferredSize(new Dimension(frame.getWidth(), frame.getHeight()));
      panel.setLayout(null);
     
      setBounds(0,0,frame.getWidth(),frame.getHeight());
      frame.add(this);
     
      setIgnoreRepaint(true);
     
      frame.pack();
     
      //Some stuff to make the frame do what we want
     frame.setResizable(false);
      frame.setVisible(true);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setLocationRelativeTo(null);
     
      //Make the buffering strategy
     createBufferStrategy(2);
      strategy = getBufferStrategy();
    }
   public void paint(Graphics g)
   {
      Graphics2D d = (Graphics2D) strategy.getDrawGraphics();
     
      d.setColor(Color.BLUE);
      d.fillRect(50,50,200,200);
     
      d.dispose();
      strategy.show();
   }
   public static void main(String[] args)
   {
      Game g = new Game();
   }


I can't make that rectangle draw. Let alone my actual paint method with enemies and such. Any tips?
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #13 - Posted 2012-02-29 07:15:12 »

Nooooooooooo do not use the repaint mechanism at all when using BufferStrategy:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
public class MyGame implement Runnable {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(WIDTH,HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        frame.setIgnoreRepaint(true); //important!

        Canvas canvas = (Canvas)frame.add(new Canvas());
        canvas.setIgnoreRepaint(true); //important!
       
        frame.setVisible();
       
        canvas.createBufferStrategy(2);
        strategy = canvas.getBufferStrategy();

        new Thread(this).start();
    }

    private BufferStrategy strategy;
   
    public void run() {
        //game loop
       
        while(true) {
            //update

            //render: recommended to setup like this according to BufferStrategy JavaDocs.
           do {
                do {
                    Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
                   
                    //draw everything
                   
                    g.dispose();
                } while(strategy.contentsRestored());
               
                strategy.show();
        } while(strategy.contentsLost());
    }
}

Offline xXShadowCowXx

Senior Newbie





« Reply #14 - Posted 2012-02-29 07:49:17 »

Thank you ra4king. That got the display working.
Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Reply #15 - Posted 2012-03-01 03:35:21 »

I had setIgnoreRepaint(true); then called repaint(); in the main loop each frame.  Kinda' the same thing, I guess.
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #16 - Posted 2012-03-01 03:52:03 »

Calling repaint() when you have setIgnoreRepaint(true) will do absolutely nothing Cheesy

Offline gbeebe

Senior Member


Medals: 5
Projects: 1



« Reply #17 - Posted 2012-03-01 03:55:19 »

but it's working fine... maybe it's an OpenJVM thing.  I have to try it on a windows machine.
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #18 - Posted 2012-03-01 04:05:34 »

Are you calling those two methods on the same object? 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.

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56

Norakomi (42 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!