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 7 of 7

Thread: Need help with this little code

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Location
    Romania
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Need help with this little code

    It`s a small game, if I could call it like that. A game I made pretty much time ago, but still, I can`t figure out what`s wrong...

    Heres the code

    import java.applet.Applet;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.Window;
    import java.awt.event.InputEvent;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
     
    import javax.swing.JOptionPane;
    import javax.swing.UIManager;
     
     
    public class shooterTest extends Applet implements MouseListener {
    	int pos1 ;
    	int pos2 ;
    	int pos3 ;
    	int pos4 ;
    	int score = 0;
    	int bullets = 10;
     
    	public void init(){
    		setBackground(Color.lightGray);
    		setSize(700, 500);
    		try{
    		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    		}catch(Exception e){
    			JOptionPane.showMessageDialog(null, "Look and feel may have not been initialised !","System",JOptionPane.ERROR_MESSAGE);
    		}
    		addMouseListener(this);
     
    	}
    	public void paint(Graphics g){
    		pos1 = (int) (Math.random() * 300);
    		pos2 = (int) (Math.random() * 300);
    		pos3 = (int) (Math.random() * 290);
    		pos4 = (int) (Math.random() * 290);
    		g.drawRect(pos1, pos2, pos3, pos4);
    		Font font = new Font("Font", Font.BOLD, 16);
    		g.setFont(font);
    		g.drawString("Score : "+score, 10, 20);
    		g.drawString("Bullets : "+getBullets(), 10, 40);
    	}
     
     
     
     
    	public void mouseClicked(MouseEvent e) {
    		if(e.getModifiers()==InputEvent.BUTTON1_MASK){
    			if(bullets > 0){
    			bullets--;
    			repaint();
     
    			}else{
    				JOptionPane.showMessageDialog(null, "You can`t shoot ! You`re out of bullets !","You have to recharge !",JOptionPane.WARNING_MESSAGE);
    			}
     
    			if(e.getX() > pos1 && e.getY() > pos2 && e.getX() < pos3 && e.getY() < pos4){
    				score += 10;
    				repaint();
    			}
    			}else if(e.getModifiers()==InputEvent.BUTTON3_MASK){
    				setBullets(10);
    				repaint();
    			}
     
    	}
     
     
    	public void mouseEntered(MouseEvent arg0) {
     
    	}
     
     
    	public void mouseExited(MouseEvent arg0) {
     
    	}
     
    	public void mousePressed(MouseEvent arg0) {
     
    	}
     
    	public void mouseReleased(MouseEvent arg0) {
    	}
     
    	static final shooterTest applet = new shooterTest();
     
    	public static void main(String args[]){
    		Frame fereastra = new Frame("Java Shooter I");
    		fereastra.add(applet, BorderLayout.CENTER);
    		fereastra.setBounds(300, 300, 700, 500);
    		fereastra.addWindowListener(new WindowAdapter() {
    			public void windowClosing(WindowEvent e){
    				applet.destroy();
    				Window win = e.getWindow();
    				win.dispose();
    			}
    		});
    		fereastra.setResizable(false);
     
    		applet.init();
    		applet.start();
    		fereastra.setVisible(true);
     
    	}
    	public int getBullets() {
    		return bullets;
    	}
    	public void setBullets(int bullets) {
    		this.bullets = bullets;
    	}
     
    }



    It was meant to be a shooter, the player had to click inside a rectangle with random positions, if he did, to the score would have been added 10 points, and from the bullets substracted 1. When he ran out of bullets, he had to click the right button to recharge.

    The problem is this :
    You had to click in the upper left corner of the rectangle and not anywhere inside it.


    What I found out is that, if the rectangle had these positions : x, y, 400, 300 ; you had to click : x, y, 300, 200... I don`t know if that helps, but anyway, I said everything I knew...

    Thank you everyone in advance !

    P.S. I`m not sure that "substracted" is the right word for it, with "substracted 1" I meant "bullets--;".I hope you understand what I meant...
    System.out.println(Signature.getSignature());


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help with this little code

    I would test re-loading first in your mouseClicked event handler. This will give you what you want, and also keeps me from having to figure out the correct logic for testing the conditions

  3. #3
    Junior Member
    Join Date
    Sep 2009
    Location
    Romania
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help with this little code

    Did it, re-loading works fine.
    Anyway, I`ll remake it, this time it`ll be a stand-alone application... perhaps it will work ...

    Thank you for taking the time to answer my thread (and all that thanky stuff...)! ^,..,^
    System.out.println(Signature.getSignature());

  4. #4
    Junior Member
    Join Date
    Sep 2009
    Location
    Romania
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help with this little code

    But until then, could someone take a look at this ? (yes, another code (and yes, i`ve got a lot of spare time))

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.InputEvent;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.JFrame;
     
     
    public class paint extends JFrame implements MouseListener{
    	boolean mouseOn;
    	int p1, p2, p3, p4;
     
    	public paint(){
    		setBackground(Color.lightGray);
    		setSize(300, 300);
    		setBounds(250, 250, 300, 300);
    		setResizable(false);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setTitle("Paint");
    		setVisible(true);
    	}
     
    	public static void main(String args[]){
    		new paint();
    	}
     
    	public void paint(Graphics g){
    		g.clearRect(0, 0, getWidth(), getHeight());
    		g.setColor(Color.lightGray);
    		g.fillRect(0, 0, getWidth(), getHeight());
    		g.setColor(Color.black);
     
    		if(mouseOn == true){
    			g.drawLine(p1, p2, p3, p4);
    		}
     
    	}
     
     
    	@Override
    	public void mouseClicked(MouseEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void mouseEntered(MouseEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void mouseExited(MouseEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
     
    	public void mousePressed(MouseEvent e) {
    		if(e.getModifiers() == InputEvent.BUTTON1_MASK){
    			p1 = e.getX();
    			p2 = e.getY();
    		}
     
    	}
     
     
    	public void mouseReleased(MouseEvent e) {
    		if(e.getModifiers() == InputEvent.BUTTON1_MASK){
    			p3 = e.getX();
    			p4 = e.getY();
     
    			mouseOn = true;
    			repaint();
    			mouseOn = false;
     
    		}
     
    	}
     
     
    }

    It was supposed to be some kind of drawing program, to draw a line between the locations that the user enters through the mouse draging.I can see nothing wrong with it, but for some reason, it doesn`t work ...


    Thanks, again, to every one trying to help me ! I hope I`m not being a bottle !
    System.out.println(Signature.getSignature());

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help with this little code

    You're going to run into problems by calling your class paint because there's a method called paint, but it's not a constructor. Remember, Java is case sensitive. As a general convention, name classes starting with upper case letters and methods/variables starting with lower case letters. It's also good practice to specify the super constructor, even if it's the default super constructor.

    I'm not sure how the mouse pressed method is activated in mouse listeners, but I think the signal is re-sent every now and then. Mouse pressed should really only update p1/p2 the first time the mouse is pressed. mouseReleased shouldn't have this problem, I think.

    But your main problem is you forgot to set your object as a mouse listener from the frame.
    this.addMouseListener(this);

    It's kind of hard to be a bottle
    Last edited by helloworld922; October 17th, 2009 at 01:47 AM.

  6. #6
    Junior Member
    Join Date
    Sep 2009
    Location
    Romania
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help with this little code

    Thank you for your advice, I changed the code according to what you said, and the mousePressed method now looks like this :

    public class Paint{
    boolean first = false;
    .........
    .........
    	public void mousePressed(MouseEvent e) {
    		if(e.getModifiers() == InputEvent.BUTTON1_MASK){
    			if(first == false){
    			p1 = e.getX();
    			p2 = e.getY();
    			first = true;
     
    			}else if(first == true){}
    		}
     
    	}
    .........
    .........

    Now I got three things to say :
    1.Is this code what you meant ?
    2.(if the program would work) Wouldn`t the program draw a line from p1/p2 (everytime being the same location) to p3/p4 ?
    3.I could use a vector instead of p1, p2, p3 and p4, couldn`t I ?

    Thanks again for advice !

    P.S. I took a look in a dictionary, and it`s actually bother not bottler, sorry .
    System.out.println(Signature.getSignature());

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need help with this little code

    I'd have a boolean that was set (true) when the mouse is first pressed (like you have) and then reset (false) when the mouse button is released. Your code has half of that problem solved (probably my fault for neglecting the second half).

    Lol, it's ok. We don't mind you having question (that's what we're here for) as long as you demonstrate some effort in trying to do what it is you need help with