Results 1 to 7 of 7
  1. #1
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default help with a component please

    hi again, so i went to half priced books and got a new book because that one i was using i feel different cover tings the way i needed so i got the o riely Learning Java by patrick niemyer and johnathon lnudsen and holy crap this is a cool book but its a little out of date(j2se 5.0) and some of the code isnt working properly. the issue i am having is it is saying the g.drawString i tried is a illegl start of experssion and also when i compile i get this

    run:
    Exception in thread "main" java.lang.ClassCastException: com.blue2525989.HelloComponent2 cannot be cast to java.awt.event.MouseListener
    at com.blue2525989.HelloComponent2.<init>(HelloCompon ent2.java:18)
    at com.blue2525989.HelloJava2.main(HelloJava2.java:11 )
    C:\Users\skinny\AppData\Local\NetBeans\Cache\8.1\e xecutor-snippets\run.xml:53: Java returned: 1
    BUILD FAILED (total time: 2 seconds)

    here is the code.
    Java Code:
    package com.blue2525989;
    
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import javax.swing.JComponent;
    
    
    class HelloComponent2 extends JComponent
    implements MouseMotionListener
    {
        String theMessage;
        int messageX = 125, messageY = 95; //coordinates of message
        
        public HelloComponent2(String message) {
            theMessage = message;
            addMouseListener((MouseListener) this);
        }
        
        public void paintComponent(Graphics g) {
            g.drawString(theMessage, messageX, messageY,);
        }
        public void mouseDragged(MouseEvent e) {
            //save the mouse coordinates and paint the message
            messageX = e.getX();
            messageY = e.getY();
            repaint();
        }
        
        public void mouseMoved(MouseEvent e){}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: help with a component please

    You have made your class implementing a MouseMotionListener, but you are using it as if it implements a MouseListener interface; it doesn't; read the API documentation for both interfaces.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: help with a component please

    Thankyou very much that problem is solved. the next one is i use g.drawString(theMessage,messageX,messageY); and it says illegal start of expression. now i need to figure this out.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: help with a component please

    Quote Originally Posted by Blue2525989 View Post
    Thankyou very much that problem is solved. the next one is i use g.drawString(theMessage,messageX,messageY); and it says illegal start of expression. now i need to figure this out.
    There's a trailing comma in the parameter list of your original code; the compiler doesn't like that ...

    kind regards,

    Jos
    Last edited by JosAH; 10-02-2016 at 09:14 AM.
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

    Default Re: help with a component please

    Ah i see that, ha thanks buddy. Just needed some fresh eyes on it i was too tired.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: help with a component please

    Here's a small tip: carefully read what the compiler has to say; the Java compiler messages are excellent. Also pay special attention to the caret position below the offending line. If you ignore them, all the JVM can do is issue a far more cryptic error message when you try to run the incorrect code.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  7. #7
    Blue2525989 is offline Senior Member
    Join Date
    Sep 2016
    Posts
    108
    Rep Power
    0

Similar Threads

  1. Can't add another component
    By NitroDev in forum New To Java
    Replies: 3
    Last Post: 04-13-2014, 12:17 AM
  2. Calling methods of a component from a different component
    By Mate de Vita in forum AWT / Swing
    Replies: 0
    Last Post: 10-26-2012, 08:15 PM
  3. What's the name of the component that...?
    By balla in forum AWT / Swing
    Replies: 1
    Last Post: 07-06-2011, 03:03 AM
  4. Component to use ?
    By pbaudru in forum AWT / Swing
    Replies: 2
    Last Post: 02-17-2010, 09:55 AM
  5. add component to component
    By Omarero in forum New To Java
    Replies: 7
    Last Post: 05-17-2009, 02:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •