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

Thread: simple problem w/ appelets which i cant figure out

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Unhappy simple problem w/ appelets which i cant figure out

    Im trying to figure out the mouse classes for a larger project im working on. This is a really simple applet i wrote, and based off of the sun micro systems tutorial it seems like it should work. The program should simply redraw the square where ever the mouse clicks. However it doesnt do it. As i undersatnd the applet should draw, wait for a mouse command, then redraw right? but in my program my mouseUp fires (i have console out to tell me every time it fires) but then it does fire paint again, so i must be mistaken about the way applets work. What am i missing, how do i get it to redraw? and how would i get this app to work?

    /////////
    import java.awt.*;
    import javax.swing.*;
    import java.applet.*;
     
    public class MovingSquare extends Applet
    {
    private int xPos = 0;
    private int yPos = 0;
    private Color currentColor = Color.BLACK;
     
        public void init()
        {
            addMouseListener(this);
            xPos = 200;
            yPos = 200;
            setBackground(Color.GRAY);
        }
     
        public void paint(Graphics g)
        {
            g.setColor(currentColor);
            g.fillRect(xPos,yPos,30,30);
            System.out.println("RUN_PAINT");
     
        }
     
        public boolean mouseUp (Event event, int x, int y)
        {
            System.out.println(x);
            System.out.println(y);
            xPos = x;
            yPos = y;
            return true;
        }
     
    }


  2. #2
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default Re: simple problem w/ appelets which i cant figure out

    also, normally i dont have the "addMouseListener(this);" line there. i know it wont compile like it is now(i dont know why), but i was trying to figure out why it wont work and forgot to take the line out.

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: simple problem w/ appelets which i cant figure out

    How about you try and implement the MouseListener interface, then your line should be alright there.

        public class MovingSquare extends Applet implements MouseListener


    // Json

  4. #4
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default Re: simple problem w/ appelets which i cant figure out

    that doesnt compile.MouseListener is in awt so it should find it right?, but it says it cant find it, where do i import it?

  5. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: simple problem w/ appelets which i cant figure out

    import java.awt.event.MouseListener;

    That should be enough.

    // Json

  6. #6
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default Re: simple problem w/ appelets which i cant figure out

    It'll compile now. I had assumed that java.awt.*; wouldcover it but i forgot that it wont go two deep.

    That however makesmy program worse not better. The mouseUp wont even call now where before it would.
    What exactly does the addMouseListener(this); do?
    And why doesnt paint(g) get called again after the mouseUp is called, and how do i make it do so because i cant call paint inside the mouseUp method.

  7. #7
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: simple problem w/ appelets which i cant figure out

    addMouseListener(this) will register this as in your current class as a mouse listener and all events for the mouse will be sent to the methods that you had to implement. In these methods you can then get a hold of the information you need.

    // Json

  8. #8
    Junior Member
    Join Date
    Aug 2009
    Posts
    8
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default Re: simple problem w/ appelets which i cant figure out

    just found out about the repaint method after search some example codes....
    i feel pretty stoopid now. but everything works,thanks for the help

Similar Threads

  1. help with simple java program
    By parvez07 in forum Java Theory & Questions
    Replies: 4
    Last Post: August 25th, 2009, 07:19 AM
  2. Simple graphics practice on previous Java code
    By amrawad_85 in forum AWT / Java Swing
    Replies: 5
    Last Post: June 19th, 2009, 10:30 AM
  3. Someone please help me write a simple program!!!
    By ocean123 in forum Loops & Control Statements
    Replies: 3
    Last Post: June 14th, 2009, 09:46 PM
  4. Generation of Palindrome number in Java
    By tina.goyal in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 26th, 2009, 08:49 AM
  5. [SOLVED] Java program to prompt and display the user input number as odd or even
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 22nd, 2009, 01:19 AM

Tags for this Thread