Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: please help? simple fix that someone who knows bout java can prolly help me

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default please help? simple fix that someone who knows bout java can prolly help me

    im making kind of like a shooting game where u have a rectangle on side of the screen and they shoot each other, im trying to make one fo the rectangles move up with the letter 'o' but it doesnt seem to be working, thanks.
     
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.ImageObserver;
    import java.io.*;
    import java.net.*;
    import java.applet.AudioClip;
    import javax.imageio.ImageIO;
    import sun.audio.*;
     
    public class Shooter extends JFrame{
    	int WIDTH = 500;
    	int HEIGHT = 500;
    	int x,y = 0;
    	int height,width = 0;
    	int health = 10;
    	Rectangle p1 = new Rectangle(480, 210, 15, 100);
    	Rectangle p2 = new Rectangle(0,210,15,100);
     
    	public Shooter(){
    		super("Shooter");
    		setSize(WIDTH,HEIGHT);
    		setResizable(false);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBackground(Color.black);
    		setVisible(true);
     
    	}
    	public class Player implements KeyListener{
    		public void run(){
    			addKeyListener(this);
    		}
     
     
    		public void keyPressed(KeyEvent event) {}
     
     
    		public void keyReleased(KeyEvent event) {}
     
     
    		public void keyTyped(KeyEvent event) {
    			if(event.getKeyChar()=='o'){
    				p1.y--;
    			}
     
     
     
     
    		}
     
     
     
     
    	}
     
    	public void paint(Graphics g){
    		g.setColor(Color.white);
    		g.fillRect(0,210,15,100);
    		g.fillRect(p1.x,p1.y, 15, 100);
    		Rectangle p1 = new Rectangle(480, 210, 15, 100);
    		Rectangle p2 = new Rectangle(0,210,15,100);
     
    	}
    	public static void main(String args[]){
    		new Shooter();
     
    	}
     
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: please help? simple fix that someone who knows bout java can prolly help me

    Quote Originally Posted by joelmeler View Post
    it doesnt seem to be working.
    So what does it do instead?

    I just took a copy of your code and tried to run it. It doesn't paint correctly, so I would start there.
    Improving the world one idiot at a time!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: please help? simple fix that someone who knows bout java can prolly help me

    Try debugging your code by adding printlns to all the methods that you expect to be called to show that they are being called.

  4. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: please help? simple fix that someone who knows bout java can prolly help me

    I looked at your code, fixed the Override and made Shooter actually call Player.. it prints two white boxes on either side of a black background

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class Shooter extends JFrame
    {
    	/**
    	 * serialVersionUID
    	 */
    	private static final long serialVersionUID = -4420393074204026863L;
    	int WIDTH = 500;
    	int HEIGHT = 500;
    	int x,y = 0;
    	int height,width = 0;
    	int health = 10;
    	Rectangle p1 = new Rectangle(480, 210, 15, 100);
    	Rectangle p2 = new Rectangle(0,210,15,100);
     
     
    	public Shooter(){
    		super("Shooter");
    		setSize(WIDTH,HEIGHT);
    		setResizable(false);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBackground(Color.black);
    		setVisible(true);
    		Thread t = new Thread(new Player()); //This actually calls the Player class
    		t.start(); //Start it!
    	}
    	public class Player implements KeyListener, Runnable
    	{
    		public Player()
    		{
     
    		}
    		@Override
    		public void run()
    		{
    			addKeyListener(this);
    		}
     
    		@Override
    		public void keyPressed(KeyEvent event) {}
     
    		@Override
    		public void keyReleased(KeyEvent event) {}
     
    		@Override
    		public void keyTyped(KeyEvent event) {
    			if(event.getKeyChar()=='o'){
    				p1.y--;
    			}
     
     
     
     
    		}
     
     
     
     
    	}
    	@Override
    	public void paint(Graphics g)
    	{
    		g.setColor(Color.white);
    		g.fillRect(0,210,15,100);
    		g.fillRect(p1.x,p1.y, 15, 100);
    		p1 = new Rectangle(480, 210, 15, 100);
    		p2 = new Rectangle(0,210,15,100);
     
    	}
    	public static void main(String args[])
    	{
    		new Shooter();
     
    	}
     
    }
    but then the movement wasn't working.. so I switched it into three classes.

    Shooter.java
     
    import javax.swing.*;
    import java.awt.*;
     
    public class Shooter extends JPanel
    {
    	/**
    	 * serialVersionUID
    	 */
    	private static final long serialVersionUID = -4420393074204026863L;
    	public JFrame frame;
    	static int WIDTH = 500;
    	static int HEIGHT = 500;
    	int x,y = 0;
    	int height,width = 0;
    	int health = 10;
    	public Rectangle p1 = new Rectangle(480, 210, 15, 100);
    	public Rectangle p2 = new Rectangle(0,210,15,100);
     
     
    	public Shooter(JFrame frame)
    	{
    		this.frame = frame;
    	}
     
    	@Override
    	public void paint(Graphics g)
    	{
    		Graphics2D g2d = (Graphics2D) g; //I prefer Graphics2D, not sure if it matters
    		g2d.fillRect(0, 0, HEIGHT, WIDTH); //Clear the background (This is necessary or it doesn't paint right..)
    		g2d.setColor(Color.white);
    		g2d.fillRect(0,210,15,100);
    		g2d.fillRect(p1.x,p1.y, 15, 100);
    		Toolkit.getDefaultToolkit().sync();
    		//p1 = new Rectangle(480, 210, 15, 100);
    		//p2 = new Rectangle(0,210,15,100);
     
    	}
    	public synchronized void decrementY() //Make guy go up
    	{
    		p1.y--;
    		repaint();
    	}
    	public synchronized void incrementY() //Make guy go down
    	{
    		p1.y++;
    		repaint();
    	}
     
    }

    Player.java
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
     
     
    public class Player implements KeyListener
    {
    	private Shooter shooter;
    	public Player(Shooter shooter)
    	{
    		this.shooter = shooter;
    	}
    	@Override
    	public void keyPressed(KeyEvent event) {}
     
    	@Override
    	public void keyReleased(KeyEvent event) {}
     
    	@Override
    	public void keyTyped(KeyEvent event) 
    	{
    		if(event.getKeyChar()=='o')
    		{
    			shooter.decrementY(); //Make the guy go up
    		}else if(event.getKeyChar()=='l')
    		{
    			shooter.incrementY(); //Make the guy go down
    		}
    	}
    }

    Main.java
    import java.awt.Color;
     
    import javax.swing.JFrame;
     
     
    class Main extends JFrame
    {
    	private static final long serialVersionUID = 1L;
    	public Main()
    	{
    		super("Shooter");
    		Shooter s = new Shooter(this);
    		add(s); //Add the Shooter
    		addKeyListener(new Player(s)); //Add the key listener
    		setBackground(Color.black); //Set background
    		setDefaultCloseOperation(EXIT_ON_CLOSE); //Make it so it closes
    		setSize(Shooter.WIDTH, Shooter.HEIGHT);
    		setLocationRelativeTo(null); //Not sure
    		setVisible(true); //Set visible
    		setResizable(false); //Don't let it get resized
    	}
    	public static void main(String[] args)
    	{
    		new Main(); //Self-Explanatory
    	}
    }

    Sorry that I kind of did it for you.. but I added lots of comments
    Last edited by Tjstretch; August 11th, 2011 at 10:45 AM.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: please help? simple fix that someone who knows bout java can prolly help me

    Sorry that I kind of did it for you.
    What good is giving the OP code? Did you take the time to comment what the problem was, tell the OP how you found the problem and the approach you took to solve it? Or did you just want to show-off?
    Read this: http://www.javaprogrammingforums.com...n-feeding.html

    You should override paintComponent in a Swing class. Don't use paint()

  6. #6
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: please help? simple fix that someone who knows bout java can prolly help me

    While I see what you are going at, I find it about 95% useless to just say "Just add printlns" at every problem, operational code is much more useful in both understanding and debugging. I tried adding println's to what he was doing.. it didn't help at all. This is the same argument as people have on this against this

  7. #7
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: please help? simple fix that someone who knows bout java can prolly help me

    As for paintComponent vrs paint, thanks I didn't realize there was a difference

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: please help? simple fix that someone who knows bout java can prolly help me

    If you don't have an interactive debugger, the only way to see what your code is doing is by adding printlns. This is a useful skill for programmers to know: how and where to add print outs.
    Believe it or not most beginning programmers don't know how to debug their code.
    Many of them post their code here and wait for people like you to fix it for them. What do they learn?

    I tried adding println's to what he was doing.. it didn't help at all.
    Then obviously you don't know how to use them. The first thing you could see was that the key listener methods were NOT being called. Why was that??? Because it had never been added as a listener.

  9. #9
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: please help? simple fix that someone who knows bout java can prolly help me

    Oh that I saw without printlns.. it was just obvious.. hence the Threads o.O I suppose you could use a println there.. What I was more worried about at that stage was that the method wasn't printing quite right.

  10. #10
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: please help? simple fix that someone who knows bout java can prolly help me

    However I admit that println's could help, but I strongly feel that the use of every thread has a "Add println's" post on it. If you looked at it he was trying to use the run method like a main method. Anyway Dead Thread

Similar Threads

  1. New to Java I need help with a simple mistake
    By Reynalto in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2010, 04:12 PM
  2. simple java help needed
    By miss confused in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 27th, 2010, 12:29 PM
  3. Java object help(should be simple)
    By Mirak in forum Object Oriented Programming
    Replies: 5
    Last Post: May 8th, 2010, 09:43 PM
  4. PLEASE HELP!!!! simple java program...
    By parvez07 in forum Object Oriented Programming
    Replies: 5
    Last Post: August 26th, 2009, 06:38 AM
  5. help with simple java program
    By parvez07 in forum Java Theory & Questions
    Replies: 4
    Last Post: August 25th, 2009, 07:19 AM