Results 1 to 3 of 3

Thread: Camera

  1. #1
    dstars5 is offline Member
    Join Date
    Mar 2013
    Posts
    19
    Rep Power
    0

    Question Camera

    Before I get into my actual problem, is there a way to close a thread on this forum? I have another one at the top of this section, and I feel rather obnoxious having two up there!

    Anyways, I need a camera from my game. My solution to this was to make a class named "Camera" and make a function inside of it called "returnXCam()" and "changeXCam()". The way I implemented it into my existing code was like this:

    Java Code:
    Rectangle2D grass = new Rectangle2D.Float(SIDE_OFFSET + cam.returnXCam(), SIDE_OFFSET, TOTAL_WIDTH + cam.returnXCam(), getHeight() - 2 * SIDE_OFFSET);
    So I add cam.returnXCam() to the X value of the rectangle I am about to draw. I have two problems with this.

    It works, but is there a better way than adding cam.returnXCam() to every X value I need?

    And while it works, the items on the screen don't stay properly sized. Here is an image of how they should look (This is before I move them).

    Camera-minigolfbefore.png

    and here it is after I move it:

    Camera-minigolfafter.png

    Obviously, this is not what I want to happen. Also, here is the part of the code that actually draws everything.

    Java Code:
    	public void paintComponent(Graphics g) {
    		Graphics2D g2 = (Graphics2D)g;
    		
    		g.clearRect(0, 0, width, height);
    		
    		//Draw the Course
    
    		if (stage == 0) {
    			Rectangle2D grass = new Rectangle2D.Float(SIDE_OFFSET + cam.returnXCam(), SIDE_OFFSET, TOTAL_WIDTH + cam.returnXCam(), getHeight() - 2 * SIDE_OFFSET);
    	
    			Rectangle2D r1 = new Rectangle2D.Float(SIDE_OFFSET + cam.returnXCam(), SIDE_OFFSET, BUMPER_WIDTH + cam.returnXCam(), getHeight() - 2 * SIDE_OFFSET);
    			Rectangle2D r2 = new Rectangle2D.Float(SIDE_OFFSET + cam.returnXCam(), SIDE_OFFSET, TOTAL_WIDTH + cam.returnXCam(), BUMPER_WIDTH); 
    			Rectangle2D r3 = new Rectangle2D.Float(SIDE_OFFSET + cam.returnXCam(), getHeight() - (SIDE_OFFSET + BUMPER_WIDTH), TOTAL_WIDTH + cam.returnXCam(), BUMPER_WIDTH);
    			Rectangle2D r4 = new Rectangle2D.Float(SIDE_OFFSET + TOTAL_WIDTH - BUMPER_WIDTH + cam.returnXCam(), SIDE_OFFSET, BUMPER_WIDTH + cam.returnXCam(), getHeight() - 2 * SIDE_OFFSET); 
    			
    			Ellipse2D hole = new Ellipse2D.Float(cupX + cam.returnXCam(), SIDE_OFFSET + BUMPER_WIDTH + BALL_OFFSET, (int)(BALL_WIDTH * 2.5) + cam.returnXCam(), (int)(BALL_WIDTH * 2.5));
    			
    			Ellipse2D ball = new Ellipse2D.Float(ballX + cam.returnXCam(), ballY, BALL_WIDTH + cam.returnXCam(), BALL_WIDTH);
    			
    			cupX = ballX;
    			
    			g.setColor(Color.yellow);
    			
    			DELAY = 100;
    			if (!playerClicked) {
    				if (!atTop) {
    					powStage++;
    					if (powStage == MAX_POWER) {
    						atTop = true;
    					}
    				} else {
    					if (powStage == 1) {
    						atTop = false;
    					}
    					powStage--;
    				}
    			}
    			
    			g.fillRect(TOTAL_WIDTH + BUMPER_WIDTH + cam.returnXCam(), getHeight() - SIDE_OFFSET - BUMPER_WIDTH, BUMPER_WIDTH + cam.returnXCam(), -(powStage * BUMPER_WIDTH / 2));
    			
    			
    			g.setColor(Color.green);
    			g2.fill(grass);
    			
    			g.setColor(Color.white);
    			g2.fill(ball);
    			g2.fill(hole);
    			
    			g.setColor(Color.black);
    			g2.fill(r1);
    			g2.fill(r2);
    			g2.fill(r3);
    			g2.fill(r4);
    			
    			
    			if (playerClicked) {
    				y_vel = -powStage;
    				stage = 1;
    			}
    			
    		} else if (stage == 1) {
    			stage++;
    			
    		} else if (stage == 2) {
    			DELAY = 17;
    			
    			Rectangle2D grass = new Rectangle2D.Float(SIDE_OFFSET + cam.returnXCam(), SIDE_OFFSET, TOTAL_WIDTH + cam.returnXCam(), getHeight() - 2 * SIDE_OFFSET);
    			
    			Rectangle2D r1 = new Rectangle2D.Float(SIDE_OFFSET + cam.returnXCam(), SIDE_OFFSET, BUMPER_WIDTH, getHeight() - 2 * SIDE_OFFSET);
    			Rectangle2D r2 = new Rectangle2D.Float(SIDE_OFFSET + cam.returnXCam(), SIDE_OFFSET, TOTAL_WIDTH + cam.returnXCam(), BUMPER_WIDTH); 
    			Rectangle2D r3 = new Rectangle2D.Float(SIDE_OFFSET + cam.returnXCam(), getHeight() - (SIDE_OFFSET + BUMPER_WIDTH), TOTAL_WIDTH + cam.returnXCam(), BUMPER_WIDTH);
    			Rectangle2D r4 = new Rectangle2D.Float(SIDE_OFFSET + TOTAL_WIDTH - BUMPER_WIDTH + cam.returnXCam(), SIDE_OFFSET, BUMPER_WIDTH + cam.returnXCam(), getHeight() - 2 * SIDE_OFFSET); 
    			
    			Ellipse2D hole = new Ellipse2D.Float(cupX + cam.returnXCam(), SIDE_OFFSET + BUMPER_WIDTH + BALL_OFFSET, (int)(BALL_WIDTH * 2.5) + cam.returnXCam(), (int)(BALL_WIDTH * 2.5));
    			
    			ballX += x_vel;
    			ballY += y_vel;
    			
    			if (x_vel < 0 && repaints % SPEED_LOSS == 0) {
    				x_vel += 1;
    			} else if (x_vel > 0 && repaints % SPEED_LOSS == 0) {
    				x_vel -= 1;
    			}
    			
    			if (y_vel < 0 && repaints % SPEED_LOSS == 0) {
    				y_vel += 1;
    			} else if (y_vel > 0 && repaints % SPEED_LOSS == 0) {
    				y_vel -= 1;
    			}
    			
    			Ellipse2D ball = new Ellipse2D.Float(ballX, ballY, BALL_WIDTH, BALL_WIDTH);
    			
    			if (ball.intersects(r1) || ball.intersects(r4)) {
    				x_vel = -x_vel;
    			} else if (ball.intersects(r2) || ball.intersects(r3)) {
    				y_vel = -y_vel;
    			} 
    			
    			int ballCenterX = ballX + BALL_WIDTH / 2;
    			int ballCenterY = ballY + BALL_WIDTH / 2;
    			
    			int cupCenterX = cupX + ((int)(BALL_WIDTH * 2.5) / 2);
    			int cupCenterY = (SIDE_OFFSET + BUMPER_WIDTH + BALL_OFFSET) + ((int)(BALL_WIDTH * 2.5) / 2);
    			
    			int xDist = ballCenterX - cupCenterX;
    			int yDist = ballCenterY - cupCenterY;
    			
    			int dist = (int) Math.sqrt(Math.pow(xDist, 2) + Math.pow(yDist, 2));
    			
    			System.out.println(xDist + " " + yDist + " " + dist);
    			
    			if (dist <= (BALL_WIDTH * 2.5) / 2) {
    				playing = false;
    				g.setColor(Color.green);
    				Ellipse2D coverBall = new Ellipse2D.Float(ballX, ballY, BALL_WIDTH, BALL_WIDTH);
    				g2.fill(coverBall);
    				JOptionPane.showMessageDialog(null, "You made it in the hole!", "Congratulations!", JOptionPane.ERROR_MESSAGE);
    			}
    		
    			g.setColor(Color.green);
    			g2.fill(grass);
    			
    			g.setColor(Color.black);
    			g2.fill(r1);
    			g2.fill(r2);
    			g2.fill(r3);
    			g2.fill(r4);
    			
    			
    			g.setColor(Color.white);
    			g2.fill(ball);
    			g2.fill(hole);
    			
    			if (x_vel == 0 && y_vel == 0) {
    				stage = 0;
    				playerClicked = false;
    			}
    		}
    		repaints++;
    		
    	}
    and the full code:

    Java Code:
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionAdapter;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Rectangle2D;
    import java.util.Random;
    
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    
    public class MiniGolf extends JPanel implements Runnable, MouseListener, KeyListener {
    	public Thread thread = new Thread(this);
    	
    	Camera cam = new Camera();
    
    	Random generator = new Random();
    	
    	private int width = 0;
    	private int height = 0;
    	
    	int ballX = (TOTAL_WIDTH + SIDE_OFFSET - BALL_WIDTH / 2) / 2;
    	int ballY = 0;
    	int repaints = 0;
    	
    	boolean playerClicked = false;
    	
    	int stage = 0;
    
    	int powStage = 0;
    	
    	boolean atTop = false;
    	
    	int cupX = ballX;
    	
    	int currentPow = 0;
    	
    	private boolean playing = true;
    	
    	private static final int BUMPER_WIDTH = 50;
    	private static final int TOTAL_WIDTH = 2 * BUMPER_WIDTH + 6 * BUMPER_WIDTH;
    	private static final int SIDE_OFFSET = 10;
    	private static final int BALL_WIDTH = 10;
    	private static final int BALL_OFFSET = 50;
    	private static final int SPEED_LOSS = 50;
    	private static final int MAX_POWER = 6;
    	
    	private int DELAY = 17;
    	
    	private int WIDTH = 0;
    	private int HEIGHT  = 0;
    	
    	private double x_vel = 0;
    	private double y_vel = 0;
    	
    	public void run() {
    		while(playing) {
    			repaint();
    			try {
    				Thread.sleep(DELAY);
    			} catch (Exception e) {
    				
    			}
    		}
    		System.out.println("Not Playing");
    	}
    	
    	public MiniGolf(int w, int h) {
    		width = w;
    		height = h;
    		ballY = height - (BUMPER_WIDTH + SIDE_OFFSET + BALL_OFFSET);
    		
    		this.setFocusable(true);
    		
    		addMouseListener(this);
    		addKeyListener(this);
    		thread.start();
    	}
    	
    	public static void main(String[] args) {
    	}
     
    	public void paintComponent(Graphics g) {
    		Graphics2D g2 = (Graphics2D)g;
    		
    		g.clearRect(0, 0, width, height);
    		
    		//Draw the Course
    
    		if (stage == 0) {
    			Rectangle2D grass = new Rectangle2D.Float(SIDE_OFFSET + cam.returnXCam(), SIDE_OFFSET, TOTAL_WIDTH + cam.returnXCam(), getHeight() - 2 * SIDE_OFFSET);
    	
    			Rectangle2D r1 = new Rectangle2D.Float(SIDE_OFFSET + cam.returnXCam(), SIDE_OFFSET, BUMPER_WIDTH + cam.returnXCam(), getHeight() - 2 * SIDE_OFFSET);
    			Rectangle2D r2 = new Rectangle2D.Float(SIDE_OFFSET + cam.returnXCam(), SIDE_OFFSET, TOTAL_WIDTH + cam.returnXCam(), BUMPER_WIDTH); 
    			Rectangle2D r3 = new Rectangle2D.Float(SIDE_OFFSET + cam.returnXCam(), getHeight() - (SIDE_OFFSET + BUMPER_WIDTH), TOTAL_WIDTH + cam.returnXCam(), BUMPER_WIDTH);
    			Rectangle2D r4 = new Rectangle2D.Float(SIDE_OFFSET + TOTAL_WIDTH - BUMPER_WIDTH + cam.returnXCam(), SIDE_OFFSET, BUMPER_WIDTH + cam.returnXCam(), getHeight() - 2 * SIDE_OFFSET); 
    			
    			Ellipse2D hole = new Ellipse2D.Float(cupX + cam.returnXCam(), SIDE_OFFSET + BUMPER_WIDTH + BALL_OFFSET, (int)(BALL_WIDTH * 2.5) + cam.returnXCam(), (int)(BALL_WIDTH * 2.5));
    			
    			Ellipse2D ball = new Ellipse2D.Float(ballX + cam.returnXCam(), ballY, BALL_WIDTH + cam.returnXCam(), BALL_WIDTH);
    			
    			cupX = ballX;
    			
    			g.setColor(Color.yellow);
    			
    			DELAY = 100;
    			if (!playerClicked) {
    				if (!atTop) {
    					powStage++;
    					if (powStage == MAX_POWER) {
    						atTop = true;
    					}
    				} else {
    					if (powStage == 1) {
    						atTop = false;
    					}
    					powStage--;
    				}
    			}
    			
    			g.fillRect(TOTAL_WIDTH + BUMPER_WIDTH + cam.returnXCam(), getHeight() - SIDE_OFFSET - BUMPER_WIDTH, BUMPER_WIDTH + cam.returnXCam(), -(powStage * BUMPER_WIDTH / 2));
    			
    			
    			g.setColor(Color.green);
    			g2.fill(grass);
    			
    			g.setColor(Color.white);
    			g2.fill(ball);
    			g2.fill(hole);
    			
    			g.setColor(Color.black);
    			g2.fill(r1);
    			g2.fill(r2);
    			g2.fill(r3);
    			g2.fill(r4);
    			
    			
    			if (playerClicked) {
    				y_vel = -powStage;
    				stage = 1;
    			}
    			
    		} else if (stage == 1) {
    			stage++;
    			
    		} else if (stage == 2) {
    			DELAY = 17;
    			
    			Rectangle2D grass = new Rectangle2D.Float(SIDE_OFFSET + cam.returnXCam(), SIDE_OFFSET, TOTAL_WIDTH + cam.returnXCam(), getHeight() - 2 * SIDE_OFFSET);
    			
    			Rectangle2D r1 = new Rectangle2D.Float(SIDE_OFFSET + cam.returnXCam(), SIDE_OFFSET, BUMPER_WIDTH, getHeight() - 2 * SIDE_OFFSET);
    			Rectangle2D r2 = new Rectangle2D.Float(SIDE_OFFSET + cam.returnXCam(), SIDE_OFFSET, TOTAL_WIDTH + cam.returnXCam(), BUMPER_WIDTH); 
    			Rectangle2D r3 = new Rectangle2D.Float(SIDE_OFFSET + cam.returnXCam(), getHeight() - (SIDE_OFFSET + BUMPER_WIDTH), TOTAL_WIDTH + cam.returnXCam(), BUMPER_WIDTH);
    			Rectangle2D r4 = new Rectangle2D.Float(SIDE_OFFSET + TOTAL_WIDTH - BUMPER_WIDTH + cam.returnXCam(), SIDE_OFFSET, BUMPER_WIDTH + cam.returnXCam(), getHeight() - 2 * SIDE_OFFSET); 
    			
    			Ellipse2D hole = new Ellipse2D.Float(cupX + cam.returnXCam(), SIDE_OFFSET + BUMPER_WIDTH + BALL_OFFSET, (int)(BALL_WIDTH * 2.5) + cam.returnXCam(), (int)(BALL_WIDTH * 2.5));
    			
    			ballX += x_vel;
    			ballY += y_vel;
    			
    			if (x_vel < 0 && repaints % SPEED_LOSS == 0) {
    				x_vel += 1;
    			} else if (x_vel > 0 && repaints % SPEED_LOSS == 0) {
    				x_vel -= 1;
    			}
    			
    			if (y_vel < 0 && repaints % SPEED_LOSS == 0) {
    				y_vel += 1;
    			} else if (y_vel > 0 && repaints % SPEED_LOSS == 0) {
    				y_vel -= 1;
    			}
    			
    			Ellipse2D ball = new Ellipse2D.Float(ballX, ballY, BALL_WIDTH, BALL_WIDTH);
    			
    			if (ball.intersects(r1) || ball.intersects(r4)) {
    				x_vel = -x_vel;
    			} else if (ball.intersects(r2) || ball.intersects(r3)) {
    				y_vel = -y_vel;
    			} 
    			
    			int ballCenterX = ballX + BALL_WIDTH / 2;
    			int ballCenterY = ballY + BALL_WIDTH / 2;
    			
    			int cupCenterX = cupX + ((int)(BALL_WIDTH * 2.5) / 2);
    			int cupCenterY = (SIDE_OFFSET + BUMPER_WIDTH + BALL_OFFSET) + ((int)(BALL_WIDTH * 2.5) / 2);
    			
    			int xDist = ballCenterX - cupCenterX;
    			int yDist = ballCenterY - cupCenterY;
    			
    			int dist = (int) Math.sqrt(Math.pow(xDist, 2) + Math.pow(yDist, 2));
    			
    			System.out.println(xDist + " " + yDist + " " + dist);
    			
    			if (dist <= (BALL_WIDTH * 2.5) / 2) {
    				playing = false;
    				g.setColor(Color.green);
    				Ellipse2D coverBall = new Ellipse2D.Float(ballX, ballY, BALL_WIDTH, BALL_WIDTH);
    				g2.fill(coverBall);
    				JOptionPane.showMessageDialog(null, "You made it in the hole!", "Congratulations!", JOptionPane.ERROR_MESSAGE);
    			}
    		
    			g.setColor(Color.green);
    			g2.fill(grass);
    			
    			g.setColor(Color.black);
    			g2.fill(r1);
    			g2.fill(r2);
    			g2.fill(r3);
    			g2.fill(r4);
    			
    			
    			g.setColor(Color.white);
    			g2.fill(ball);
    			g2.fill(hole);
    			
    			if (x_vel == 0 && y_vel == 0) {
    				stage = 0;
    				playerClicked = false;
    			}
    		}
    		repaints++;
    		
    	}
    
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		playerClicked = true;
    		System.out.println("Player Clicked!");
    		
    	}
    
    	@Override
    	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mousePressed(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mouseReleased(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void keyPressed(KeyEvent e) {
    		int key = e.getKeyCode();
    		System.out.println("pressed!");
    		
    		if (key == KeyEvent.VK_UP) {
    			cam.changeYCam(-1);
    		} else if (key == KeyEvent.VK_DOWN) {
    			cam.changeYCam(1);
    		} else if (key == KeyEvent.VK_LEFT) {
    			cam.changeXCam(-1);
    		} else if (key == KeyEvent.VK_RIGHT) {
    			cam.changeXCam(1);
    		} else if (key == KeyEvent.VK_PLUS) {
    			//zoom in
    		} else if (key == KeyEvent.VK_MINUS) {
    			//zoom out
    		}
    		
    	}
    
    	@Override
    	public void keyReleased(KeyEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    }
    Anyone know how to fix my problems?

    Thanks!

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,322
    Rep Power
    5

    Default Re: Camera

    It appears you are changing the aspect ratio of the ball and cup, thus turning it into an Ellipse. As for changing all your X values, check out Graphics.translate(). Note that the translate as well as other Graphics and Graphics2D functions like scale, rotate, etc. are additive. To help combat this, I recommend creating a copy of your graphics context before using it, then disposing of the copy before exiting the paint routine. This preserves the original graphics context.

    Java Code:
    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.translate(cam.returnXCam(), 0);
    // rest of code that relies on new coordinate space
        g2d.dispose();
    }
    Regards,
    Jim
    Last edited by jim829; 04-16-2013 at 01:21 PM. Reason: made correction
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    dstars5 is offline Member
    Join Date
    Mar 2013
    Posts
    19
    Rep Power
    0

    Default Re: Camera

    Thanks! And when you said that, I realized the 3rd and 4th parameters are for width.... Nice little fail on my part! But thank you for the .translate()! That will help so much!

Similar Threads

  1. Camera Screen
    By tnrh1 in forum New To Java
    Replies: 3
    Last Post: 05-22-2012, 07:35 PM
  2. IP Camera
    By BMWTouring in forum Advanced Java
    Replies: 0
    Last Post: 08-28-2011, 05:03 PM
  3. camera pic
    By Aveekdtt in forum CLDC and MIDP
    Replies: 0
    Last Post: 01-10-2010, 12:52 AM
  4. Loading a camera
    By mrvigneshmca in forum Java Applets
    Replies: 0
    Last Post: 03-18-2009, 10:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •