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.
|
|
|
|
ra4king
|
 |
«
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());
g2.translate(-myCamera.getXOffset(),-myCamera.getYOffset()); |
|
|
|
|
gbeebe
|
 |
«
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
| Followable following;
following = player1;
public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.translate(following.getX(), following.getY());
|
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!
|
|
ra4king
|
 |
«
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); |
|
|
|
|
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.
|
|
|
|
gbeebe
|
 |
«
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.
|
|
|
|
ra4king
|
 |
«
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.
|
|
|
|
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.
|
|
|
|
gbeebe
|
 |
«
Reply #8 - Posted
2012-02-29 04:53:47 » |
|
Well, IMHO, I think they're wrong.
|
|
|
|
ra4king
|
 |
«
Reply #9 - Posted
2012-02-29 05:01:59 » |
|
LOL! Bunch of clueless colleagues 
|
|
|
|
Games published by our own members! Check 'em out!
|
|
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.
|
|
|
|
gbeebe
|
 |
«
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.drawImage(imgDbl, 0, 0, screenWidth, screenHeight, null); b.dispose(); bufferStrategy.show(); } |
Note: My main class extends Frame, and a canvas is added to it.
|
|
|
|
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() { 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(); frame.setResizable(false); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); 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?
|
|
|
|
ra4king
|
 |
«
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);
Canvas canvas = (Canvas)frame.add(new Canvas()); canvas.setIgnoreRepaint(true); frame.setVisible(); canvas.createBufferStrategy(2); strategy = canvas.getBufferStrategy();
new Thread(this).start(); }
private BufferStrategy strategy; public void run() { while(true) {
do { do { Graphics2D g = (Graphics2D)strategy.getDrawGraphics(); g.dispose(); } while(strategy.contentsRestored()); strategy.show(); } while(strategy.contentsLost()); } } |
|
|
|
|
xXShadowCowXx
Senior Newbie 
|
 |
«
Reply #14 - Posted
2012-02-29 07:49:17 » |
|
Thank you ra4king. That got the display working.
|
|
|
|
gbeebe
|
 |
«
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.
|
|
|
|
ra4king
|
 |
«
Reply #16 - Posted
2012-03-01 03:52:03 » |
|
Calling repaint() when you have setIgnoreRepaint(true) will do absolutely nothing 
|
|
|
|
gbeebe
|
 |
«
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.
|
|
|
|
ra4king
|
 |
«
Reply #18 - Posted
2012-03-01 04:05:34 » |
|
Are you calling those two methods on the same object? 
|
|
|
|
|