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

Thread: Help with my GUI

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with my GUI

    Hi guys, im new to GUI in Java and to this website. Ive self taught myself Java so far, I would consider myself knowing the basics of Java and I am ready to learn more advanced things in java.
    Im currently making this Java GUI game which has a simple concept; the user clicks on the the screen when the small circle is in the center of the big circle and gains a point. I made a black circle by 100x100 and a white circle 50x50. This my code so far:
    GUI
            import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.io.IOException;
    import java.util.Random;
     
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    import javax.swing.Timer;
    import javax.swing.JPanel;
            public class gui extends JPanel implements ActionListener, MouseListener {
     
    	static final int permanentposx = 75;
    	static final int permanentposy = 75;
    	int posy = 75;
    	int posx = 75;
    	int score;
    	Timer t = new Timer(500, this);
     
    	public gui() {
    		t.start();
    	}
     
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		g.setColor(Color.black);
    		g.fillOval(50, 50, 100, 100);
    		g.setColor(Color.WHITE);
    		g.fillOval(posx, posy, 50, 50);
     
    	}
     
    	public void actionPerformed(ActionEvent e) {
    posy = permanentposy;
    		posx = permanentposx;		
    boolean checkifmoved = false;
    		int x;
    		Random rnd = new Random();
    		x = rnd.nextInt(999);
     
    		if (x % 3 == 0) {
    			posy = posy - 25;
    			posx = permanentposx;
    			checkifmoved = true;
     
    		} else if (x % 27 == 0) {
    			posy = posy + 25;
    			posx = permanentposx;
    			checkifmoved = true;
    		} else if (x % 13 == 0) {
    			posx = posx + 25;
    			posy = permanentposy;
    			checkifmoved = true;
    		} else  {
    			posx = posx - 25;
    			posy = permanentposy;
    			checkifmoved = true;
     
    		}
     
    		this.repaint();
     
    		if (checkifmoved == true) {
    			int y;
    			y = rnd.nextInt(10);
    			if(y % 3 == 0){
    			posy = permanentposy;
    			posx = permanentposx;
    			checkifmoved = false;
    			}
    		this.repaint();
     
    	}
     
    }
     
    	@Override
    	public void mouseClicked(MouseEvent ee) {
     
     
    	}

    MAIN
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    public class main {
     
    	public static void main(String[] args) {
    		JFrame frame = new JFrame("Circle enlarger");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(400,400);
    		frame.setVisible(true);
     
    		gui guiobject = new gui();
    		frame.add(guiobject);
     
     
     
    	}
    }

    As you can see, on my actionlistener method in my GUI class, I tried making the circle move up,down,left or right randomly by creating a random number and dividing that by a certain number. The problem im currently having is that I dont really know how code the part where the computer knows when the user has clicked and when the small circle is in the center of the black circle...
    Ive tried implementing mouselistener but dont know which statement to use. I was thinking of MOUSE_CLICKED but that didnt work out right...
    Last edited by tloz; March 16th, 2014 at 09:54 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Help with my GUI

    that didnt work out right.
    Please explain what happened and what the problem was. Post any printn debug output that shows what you are talking about.


    NOTE: The posted code needs import statements to be able to compile.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with my GUI

    Quote Originally Posted by Norm View Post
    Please explain what happened and what the problem was. Post any printn debug output that shows what you are talking about.


    NOTE: The posted code needs import statements to be able to compile.
    Oh yeah, I forgot to copy and past the import codes but they are in my class.
    Everything works fine BUT I dont know how to make the GUI realize that user gains 1 point when the user clicks when the white circle is in the center of the black circle.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Help with my GUI

    I need to be able to compile the code to test it. I can not compile without the import statements.
    that didnt work out right
    Please explain what happened and what the problem was. Post any printn debug output that shows what you are talking about.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with my GUI

    There is no error bugs or anything, I need help writing the mouselistener codes.
    Ive edited the source code, ive added the import codes.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Help with my GUI

    Ok, that gets rid of some compiler errors. There is still a compiler error in the code that needs to be fixed before it can be executed.

    I need help writing the mouselistener codes.
    Can you describe in detail what the listener needs to do?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with my GUI

    I can compile it perfectly, Im getting no errors. Let me try and copy and paste it again:
    GUI
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.io.IOException;
    import java.util.Random;
     
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    import javax.swing.Timer;
    import javax.swing.JPanel;
     
    public class gui extends JPanel implements ActionListener, MouseListener {
     
    	static final int permanentposx = 75;
    	static final int permanentposy = 75;
    	int posy = 75;
    	int posx = 75;
    	int score;
    	Timer t = new Timer(200, this);
     
    	public gui() {
    		t.start();
    	}
     
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		g.setColor(Color.black);
    		g.fillOval(50, 50, 100, 100);
    		g.setColor(Color.WHITE);
    		g.fillOval(posx, posy, 50, 50);
     
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		posy = permanentposy;
    		posx = permanentposx;
    		boolean checkifmoved = false;
    		int x;
    		Random rnd = new Random();
    		x = rnd.nextInt(999);
     
    		if (x % 3 == 0) {
    			posy = posy - 25;
    			posx = permanentposx;
    			checkifmoved = true;
     
    		} else if (x % 27 == 0) {
    			posy = posy + 25;
    			posx = permanentposx;
    			checkifmoved = true;
    		} else if (x % 13 == 0) {
    			posx = posx + 25;
    			posy = permanentposy;
    			checkifmoved = true;
    		} else  {
    			posx = posx - 25;
    			posy = permanentposy;
    			checkifmoved = true;
     
    		}
     
    		this.repaint();
     
    		if (checkifmoved == true) {
    			int y;
    			y = rnd.nextInt(10);
    			if(y % 3 == 0){
    			posy = permanentposy;
    			posx = permanentposx;
    			checkifmoved = false;
    			}
    		this.repaint();
     
     
     
    	}
     
    }
     
    	@Override
    	public void mouseClicked(MouseEvent ee) {
     
     
    	}
     
    	@Override
    	public void mouseEntered(MouseEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void mouseExited(MouseEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void mousePressed(MouseEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    	@Override
    	public void mouseReleased(MouseEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
    }

    MAIN
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
     
    public class main {
     
    	public static void main(String[] args) {
    		JFrame frame = new JFrame("Circle enlarger");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(400,400);
    		frame.setVisible(true);
     
    		gui guiobject = new gui();
    		frame.add(guiobject);
     
     
     
    	}
    }


    --- Update ---

    I need the mouselistener to realize to increment the score by 1 everytime the user clicks on the screen when the white circle is in the center of the black circle

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Help with my GUI

    Ok, this time the MouseListener methods are there.
    Now what about this part:

    I need help writing the mouselistener codes.
    Can you describe in detail what the listener needs to do?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with my GUI

    I need the mouselistener to increment the score by 1 everytime the user clicks on the screen when the small circle is in the center of the big circle

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Help with my GUI

    Ok, how is the listener going to do that? Make a detailed list of the steps the listener needs to take to do that.

    For testing add some println statements to print a message when the mouse is clicked and to show the location of the click.
    Also print out a message if the small circle is in the center of the big circle so you can see when the two events happen.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with my GUI

    I was thinking of writing this in the mouseClicked method:
    public void mouseClicked(MouseEvent ee) {
    if (posx == permanentposx && posy == permanentposy) {
    score++;
    System.out.println(score);
    }

    }
    But that doesn't seem to work

    EDIT = I just made it work. That method does work, I just forgot to add the mouselisenter object in my main class!! Thanks for the help. Is there a way of printing of the score onto the screen and it will continuously update itself everytime the user clicks?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Help with my GUI

    printing of the score onto the screen
    The Graphics class's drawString() method will write a String.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with my GUI

    Thanks. I added this to the paintComponent method:
    g.drawString(String.valueOf(score), 25, 25);

    It now does it automatically, thanks for your help. I was trying to figure out what was the problem for days, Ive consulted stackoverflow and they didnt help that much.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,618
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Help with my GUI

    Glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need help with GUI
    By Marcusjamaal in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 16th, 2012, 06:41 AM
  2. GUI HELP
    By Kshahzada in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 3rd, 2012, 07:05 PM
  3. Replies: 3
    Last Post: January 31st, 2010, 11:24 PM