3 Replies - 286 Views - Last Post: 31 July 2013 - 03:08 PM Rate Topic: -----

#1 Flippinroo2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 31-July 13

Need help with first game

Posted 31 July 2013 - 01:28 PM

Well I am just starting java and have been watching Youtube tutorials. I am trying to make a simple application where i control a square that collects a smaller square and then re spawns the small square in a new random location. Currently the square spawns out of frame and i can't figure out how to fill in the main larger square with color, because as of right now the larger square only has a red outline and is transparent in the middle. Any input is appreciated.
package javaGame;
import java.awt.*;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import java.util.*;

public class JavaGame extends JFrame{
	
	int a, b, xaxis, yaxis, rectX, rectY, tokenX, tokenY;
    //Scanner scanner = new Scanner(System.in);
	//String name = scanner.nextLine();
    private Image dbImage;
	private Graphics dbg;
	Font font = new Font("Arial", Font.ITALIC, 20);
		public class AL extends KeyAdapter{
			public void keyPressed(KeyEvent e){
				int keyCode = e.getExtendedKeyCode();
			if(keyCode == e.VK_LEFT){
				if(xaxis <= 5)
					xaxis = 5;
				else xaxis += -5;
			}
			if(keyCode == e.VK_RIGHT){
				if(xaxis >= 230)
					xaxis = 230;
				else xaxis += +5;
			}
			if(keyCode == e.VK_UP){
				if(yaxis <= 27)
					yaxis = 27;
				else yaxis += -5;
			}
			if(keyCode == e.VK_DOWN){
				if(yaxis >= 230)
					yaxis = 230;
				else yaxis += +5;
			}
		}
	}
	public JavaGame(){
		addKeyListener(new AL());
		setTitle("Javagame");
		setSize(250, 250);
		setResizable(false);
		setVisible(true);
		setBackground(Color.GRAY);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Rectangle r1 = new Rectangle(rectX, rectY, 15, 15);
		Rectangle t2 = new Rectangle(tokenX, tokenY, 10, 10);
		xaxis = 150;
		yaxis = 150;
		Random random = new Random();
		a = random.nextInt(230) + 10;
		b = random.nextInt(230) + 10;
		
	}
		
	public void paint(Graphics g){
		dbImage = createImage(getWidth(), getHeight());
		dbg = dbImage.getGraphics();
		paintComponent(dbg);
		paintTokens(dbg);
		g.drawImage(dbImage,0, 0, this);
	}
	public void paintComponent(Graphics g){
		 Rectangle r1 = new Rectangle(rectX, rectY, 15, 15);
		 g.setFont(font);
		 //g.drawString("Hello " + name + "!", 70, 50);
		 g.setColor(Color.red);
		 g.drawRect(xaxis, yaxis, 15, 15);
		 g.fillRect(r1.x, r1.y, r1.width, r1.height);
		
	repaint();
	}
	public void paintTokens(Graphics g){
		Rectangle r1 = new Rectangle(xaxis, yaxis, 15, 15);
		Rectangle t2 = new Rectangle(a , b, 7, 7);
		g.setColor(Color.yellow);
		g.drawRect(a, b, 7, 7);
		g.fillRect(t2.x, t2.y, t2.width, t2.height);	
			
		if (r1.intersects(t2)){
			Random random = new Random();
			a = random.nextInt(230) + 10;
			b = random.nextInt(230) + 10;
			g.drawRect(a, b, 7, 7);
		    g.fillRect(t2.x, t2.y, t2.width, t2.height);
			}
	}		
	
public static void main(String[] args){
	//System.out.println("Please enter your name: ");
	new JavaGame();
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Need help with first game

#2 StrongJoshua  Icon User is offline

  • D.I.C Head

Reputation: 47
  • View blog
  • Posts: 153
  • Joined: 19-July 13

Re: Need help with first game

Posted 31 July 2013 - 02:03 PM

Well, first of all, you never initialized rectX and rectY, as far as I could see, so the rectangle would have an x and y of 0... So when you initialize the Rectangles you should use xaxis and yaxis. When you draw the rectangle just use g.fillRect() you don't need to call g.drawRectangle() as well as g.fillRect() does the same thing, but fills the rectangle. Also, you should just create a local variable for r1 and t2 and not recreate temporary objects, and you should do the same for the Random() object for optimization purposes. Basically, look over your code with this criteria and look for these problems and similar ones I may have missed.
Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#3 Flippinroo2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 31-July 13

Re: Need help with first game

Posted 31 July 2013 - 02:13 PM

When I removed the g.drawRect() method nothing shows up. If you could explain what you mean by initialize rectX and rectY I would appreciate it. Here is my revised code, although it doesn't work.
package javaGame;
import java.awt.*;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import java.util.*;

public class JavaGame extends JFrame{
	
	int a, b, xaxis, yaxis, rectX, rectY, tokenX, tokenY;
	Rectangle r1 = new Rectangle(rectX, rectY, 15, 15);
	Rectangle t2 = new Rectangle(tokenX, tokenY, 10, 10);
    //Scanner scanner = new Scanner(System.in);
	//String name = scanner.nextLine();
    private Image dbImage;
	private Graphics dbg;
	Font font = new Font("Arial", Font.ITALIC, 20);
		public class AL extends KeyAdapter{
			public void keyPressed(KeyEvent e){
				int keyCode = e.getExtendedKeyCode();
			if(keyCode == e.VK_LEFT){
				if(xaxis <= 5)
					xaxis = 5;
				else xaxis += -5;
			}
			if(keyCode == e.VK_RIGHT){
				if(xaxis >= 230)
					xaxis = 230;
				else xaxis += +5;
			}
			if(keyCode == e.VK_UP){
				if(yaxis <= 27)
					yaxis = 27;
				else yaxis += -5;
			}
			if(keyCode == e.VK_DOWN){
				if(yaxis >= 230)
					yaxis = 230;
				else yaxis += +5;
			}
		}
	}
	public JavaGame(){
		addKeyListener(new AL());
		setTitle("Javagame");
		setSize(250, 250);
		setResizable(false);
		setVisible(true);
		setBackground(Color.GRAY);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		rectX = xaxis;
	    rectY = yaxis;
		xaxis = 150;
		yaxis = 150;
		Random random = new Random();
		a = random.nextInt(225) + 10;
		b = random.nextInt(225) + 10;
		
	}
		
	public void paint(Graphics g){
		dbImage = createImage(getWidth(), getHeight());
		dbg = dbImage.getGraphics();
		paintComponent(dbg);
		paintTokens(dbg);
		g.drawImage(dbImage,0, 0, this);
	}
	public void paintComponent(Graphics g){
		 g.setFont(font);
		 //g.drawString("Hello " + name + "!", 70, 50);
		 g.setColor(Color.red);
		 g.fillRect(r1.x, r1.y, r1.width, r1.height);
		
	repaint();
	}
	public void paintTokens(Graphics g){
		g.setColor(Color.yellow);
		g.fillRect(t2.x, t2.y, t2.width, t2.height);	
			
		if (r1.intersects(t2)){
			Random random = new Random();
			a = random.nextInt(225) + 10;
			b = random.nextInt(225) + 10;
		    g.fillRect(t2.x, t2.y, t2.width, t2.height);
			}
	}		
	
public static void main(String[] args){
	//System.out.println("Please enter your name: ");
	new JavaGame();
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 822
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: Need help with first game

Posted 31 July 2013 - 03:08 PM

What a strange way of painting. By recreating the BufferedImage every iteration of paint(), you have sacrificed performance, likely defeating the purpose of double buffering in the first place, but maybe not completely. Your paintComponent() is useless. That code could be moved to paint(), but you should avoid painting in paint() to begin with, as you run the risk of mixing passive and active rendering. You should find better videos on the YouTube machine, bro.

Double buffered and 100% thread safe.

public class Main {
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				
				class MainWindow extends JFrame {
					{
						setTitle("Main Window");
						setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
						add(new JPanel() {
							int x, y;
							Rectangle rect;
							Timer timer;
							{
								setPreferredSize(new Dimension(800,600));
								setBackground(Color.black);
								rect = new Rectangle(20,20,50,50);
								timer = new Timer(30, new ActionListener() {
									public void actionPerformed(ActionEvent e) {
										repaint();
										move();
									}
								});
								timer.start();
							}
			
							public void move() {
								//this movement is not frame independent, so it is technically not proper.
								rect.x +=5;
								if (rect.x > getWidth() - rect.width) {
									rect.x = 20;
								}
							}
							
							public void paintComponent(Graphics g) {
								super.paintComponent(g);
								((Graphics2D)g).setColor(Color.red);
								((Graphics2D)g).fill(rect);
							}
						});
						pack();
						setVisible(true);
					}
				}//end Mainwindow
				
				new MainWindow();
			}
		});//end SwingUtilities.invokeLater()
		
		new Main();
	}
	
}


This post has been edited by farrell2k: 31 July 2013 - 03:27 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1