The Artima Developer Community
Sponsored Link

Java Answers Forum
What's wrong??

1 reply on 1 page. Most recent reply: Jan 16, 2004 10:03 PM by Craig Wood

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Honey Maverick

Posts: 8
Nickname: maverick01
Registered: Jan, 2004

What's wrong?? Posted: Jan 16, 2004 9:17 PM
Reply to this message Reply
Advertisement
Pls see the following program.
I dont know why i have 2 errors.







package paintprogproject;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class paintprog extends Applet{
String str;
Point prev, current;
public void init() {
str = "Initialized";
resize(300, 300);

addMouseMotionListener(new MouseMotionHandler());
addMouseListener(new MouseHandler());
prev = new Point(0, 0);
current = new Point(0, 0);
showStatus("Initializing");

}

public void paint(Graphics g) {
showStatus("Applet Painted");
}

//handler inner classes

class MouseHandler
implements MouseListener {
public void mousePressed(MouseEvent e) {
prev.x = current.x = e.getX();
prev.y = current.y = e.getY();
}

public void mouseClicked(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

}

class MouseMotionListener
implements MouseMotionListener {
public void mouseDragged(MouseEvent e) {
str = "Mouse Dragged";
showStatus(str);

current.x = e.getX();
current.y = e.getY();

Graphics g = getGraphics();
g.setColor(Color.blue);
g.drawLine(prev.x, prev.y, current.x, current.y);

prev.x = current.x;
prev.y = current.y;

}

public void MouseMoved(MouseEvent e) {
str = "Mouse Moved";
showStatus(str);
}

}
}


Craig Wood

Posts: 1
Nickname: whirlwind
Registered: Jan, 2004

Re: What's wrong?? Posted: Jan 16, 2004 10:03 PM
Reply to this message Reply

C:\jexp>javac painttest.java
painttest.java:39: interface expected here
class MouseMotionListener implements MouseMotionListener {
^
painttest.java:14: cannot resolve symbol
symbol : class MouseMotionHandler
location: class PaintTest
addMouseMotionListener(new MouseMotionHandler());
^
2 errors

Error one: Do not name your class with same name as the interface you are implementing. Confuses the compiler.

Error two: Wrong spelling:
  addMouseMotionListener(new [i]ClassName[/i]());

must match:
  class [i]ClassName[/i] implements MouseMotionListener {

Another typo:
  public void MouseMoved(MouseEvent e) {

This prev.x = current.x = e.getX(); can be reduced to prev.x = e.getX();
//  <applet code="PaintTest" width="300" height="300"></applet>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
 
public class PaintTest extends Applet {
    String str;
    Point prev, current;
 
    public void init() {
        str = "Initialized";
 
        addMouseMotionListener(new MotionHandler());
        addMouseListener(new MouseHandler());
        prev = new Point(0, 0);
        current = new Point(0, 0);
        showStatus("Initializing");
    }
 
    public void paint(Graphics g) {
        showStatus("Applet Painted");
    }
 
    //handler inner classes
 
    class MouseHandler extends MouseAdapter {
        public void mousePressed(MouseEvent e) {
            prev.x = e.getX();
            prev.y = e.getY();
        }
    }
 
    class MotionHandler extends MouseMotionAdapter {
        public void mouseDragged(MouseEvent e) {
            str = "Mouse Dragged";
            showStatus(str);
 
            current.x = e.getX();
            current.y = e.getY();
 
            Graphics g = getGraphics();
            g.setColor(Color.blue);
            g.drawLine(prev.x, prev.y, current.x, current.y);
 
            prev.x = current.x;
            prev.y = current.y;
        }
 
        public void mouseMoved(MouseEvent e) {
            str = "Mouse Moved";
            showStatus(str + " x = " + e.getX() + ", y = " + e.getY());
        }
    }
}

Flat View: This topic has 1 reply on 1 page
Topic: Difference between Interface and Abstract Classes Previous Topic   Next Topic Topic: Intermittent problem launching default browser

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use