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: Beginner java problem

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    47
    My Mood
    Amused
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Beginner java problem

    Hello everyone!

    I've been messing around withJava swing for a while now. I'm trying a draw a text in the mouse position everytime a mousebutton is pressed,
    but the problem is that everytime you release the mousebutton the string stays on the screen.

    package windowtraining;
    import javax.swing.SwingUtilities;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.Color;
     
     
     
     
    public class Windowtraining{
     
        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable(){
                public void run(){
                    createAndShowGUI();
                }
            });
        }    
     
        public static void createAndShowGUI(){
     
            JFrame f = new JFrame("JFrame test!");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new myPanel());
            f.pack();
            f.setVisible(true);
     
        }
     
    }
     
    class myPanel extends JPanel{
     
        private int squarex;
        private int squarey;
        private int squarew = 32;
        private int squareh = 32;
        private int switchmouse = 0;
        private int smilex;
        private int smiley;
     
     
        public myPanel(){
     
            addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e){
                moveSquare(e.getX(),e.getY());
            }
            });
     
            addMouseMotionListener(new MouseAdapter(){
                public void mouseDragged(MouseEvent e){
                    switchmouse = 1;
                    smilex = e.getX(); smiley = e.getY();
                }
            });
     
            addMouseMotionListener(new MouseAdapter(){
                public void mouseReleased(MouseEvent e){
                    switchmouse = 0;
                }
            });
     
     
            addMouseMotionListener(new MouseAdapter(){
                public void mouseDragged(MouseEvent e){
                    moveSquare(e.getX(),e.getY());
                }
            });
     
    }
     
     
        private void moveSquare(int x, int y){
            int Offset = 1;
            if (squarex != x || squarey != y)
            {
            repaint(squarex,squarey,squarew,squareh);
            squarex = x - 16;
            squarey = y - 16;
            repaint(squarex,squarey,squarew,squareh);
            }
     
        }
     
        /*private void drawSmile(int x, int y){
     
            if (smilex != x || smiley != y){
     
               update(smilex, smiley );
     
            }
     
        }*/
     
     
     
     
     
        public Dimension getPreferredSize(){
     
            return new Dimension(640,480);
     
        }
     
        public void paintComponent(Graphics g){
     
            super.paintComponent(g);
            {
            super.paintComponent(g);
     
            g.setColor(Color.RED);
           g.fillRect(squarex, squarey, squarew, squareh);
     
            //if (dragSwitch == 1)
            g.setColor(Color.WHITE);
            g.fillRect(0,0,640,480);
     
     
            g.setColor(Color.BLUE);
     
            if (switchmouse == 1)
                    {g.drawString(":D",smilex,smiley);}
     
     
            }
     
     
     
        }
     
     
     
    }


    What should I do to draw a string in the mouse position when the player presses a
    mouse button but which disappears when you release it?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Beginner java problem

    disappears when you release it?
    There is a listener for mouse released. Have it tell the paintComponent method not to draw the text.
    You need to call the repaint() method so that the jvm will call the paintComponent method to implement the change in what should be displayed.

    Make sure to use {}s with if statements to enclose the statements that are controlled by the if statement.

    Don't hide the {}s by having code on the same line after a { or before a }
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    47
    My Mood
    Amused
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Beginner java problem

    Quote Originally Posted by Norm View Post
    There is a listener for mouse released. Have it tell the paintComponent method not to draw the text.
    You need to call the repaint() method so that the jvm will call the paintComponent method to implement the change in what should be displayed.

    Make sure to use {}s with if statements to enclose the statements that are controlled by the if statement.

    Don't hide the {}s by having code on the same line after a { or before a }
    Should I call the repaint component inside the mouseReleased bacets?:

    addMouseMotionListener(new MouseAdapter(){
    public void mouseReleased(MouseEvent e){
    switchmouse = 0;
    repaint(smilex, smiley);
    }
    });
    It gives me an error though. I don't really understand... Could you explain a bit further?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Beginner java problem

    See the API doc for the Component class's repaint() method. Don't use any args.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2013
    Posts
    47
    My Mood
    Amused
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Beginner java problem

    I can't find any document where its says how to use repaint :/, or at least no ducument which I understand. I'm very new to Java so I would really appreciate if you could explain where and how I should use repaint. If it isn't too much of a bother.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Beginner java problem

    Code the call to repaint like this:
    repaint();
    when you want the paintComponent() method to be called.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    kinkita (April 30th, 2013)

  8. #7
    Member
    Join Date
    Mar 2013
    Posts
    47
    My Mood
    Amused
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Beginner java problem

    I think I got it now, The string gets refreshed and does not multiply. But (sorry for being a pain in the ass) the string does not disappear when the mousebutton is released, which I would like it to do. How do I tell the program not to keep drawing the string. I tryed just using if statements to controll the drawings but that didn't work.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Beginner java problem

    Is the variable's value changing when the mouse is released?
    Try debugging the code by adding some println statements to print out the value of the variable when it is changed and when it is used, to see what is happening. The print out will show you when and if the variable's value is changed.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Member
    Join Date
    Mar 2013
    Posts
    47
    My Mood
    Amused
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Beginner java problem

    Yes! I got it! I added another mouse_listener to change the variable to zero when the mousebutton was released, which worked

    The listener that was supposed to change the variabel before was a motion listener so it only worked when it was moving and released, or something like that.
    But now the variabel could be changed to zero when the mousebutton was released when not moving.

    Thank you so much for your help, I've learnt quite a lot!

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Beginner java problem

    Read the API doc for the MouseMotionListener interface to see what methods it uses.
    Compare that with what the MouseListener interface uses.

    Using a MouseAdapter can be convenient but it requires care because it has empty methods for all the interfaces and there is no compiler error message if you override the wrong one. Also it's a good idea to use the @Override notation when using the MouseAdapter so that the compiler checks that the method you are coding overrides an existing method vs creating a new and unused method.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Beginner Java problem help.
    By Fishwick in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 30th, 2013, 06:43 AM
  2. Beginner Java Problem Help.
    By darkr166 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 24th, 2013, 02:47 AM
  3. Java Speech API problem very beginner
    By yugeshshrestha in forum Java Theory & Questions
    Replies: 3
    Last Post: July 4th, 2012, 12:52 PM
  4. Replies: 10
    Last Post: October 26th, 2011, 02:22 PM
  5. Beginner Problem
    By Melvrick in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 15th, 2011, 12:29 PM