Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2004

Tip: Looking for answers? Try searching our database.

Problem with mouselistener

Thread view: 
Daniel - 07 Apr 2004 22:24 GMT
Hi all,
I have a problem with the mouselistener. In my opinion I should
receive the mouseevents, but I don't. This code is very much "work in
progress" so it's quite simple, and at points maybe bordering on
stupid ;-)
Anyway I'm fairly new to work in java.
What I want to do is to catch the mouse-click and then do some "work"
depending on where the click happnened. At present dummy code is
inserted just to check IF i recieve and events.
I have gotten this mouselistener to work before so I'm fairly sure
it's me doing something stupid here, I just can't figure out what, any
help or pointers to where to read more about this would be
appreciated.

And now the code.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/* My main class */
public class Mildemo extends JFrame implements MouseListener,
ActionListener {
   
    private Mainpanel main;
   
    public Mildemo(){
       
        Container c= this.getContentPane();
               
        main = new Mainpanel(this);
       
        c.setLayout(new GridLayout(1,1));
        c.add(main);
        //c.add(mainp);
       
       
        this.setSize(1024,768);
        this.setBackground(Color.BLACK);
       
       
        this.setVisible(true);
        //main.repaint();
    }
   
    public void actionPerformed(ActionEvent e){
    }
   
    public void mouseClicked(MouseEvent e) {
        System.out.println("Clicked: " +e.getX() +e.getY());
           
    }
   
    public void mouseEntered(MouseEvent e) {
        System.out.println("moving!");
       
    }
   
    public void mouseExited(MouseEvent e) {
        System.out.println("Outing!");
   
    }
   
    public void mousePressed(MouseEvent e) {
   
    }
   
    public void mouseReleased(MouseEvent e) {
    }
   
   
   
    public static void main(String args[]) {
        Mildemo demo = new Mildemo();
   
        return;
    }
   
   
   
}
/* The class that actually draw stuff, and do "work" */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Mainpanel extends Canvas implements MouseListener,
ActionListener {
    private Image meny;
    private Image base;
    private Image system;
    private Dater date;
    private String location;
    private int imsel=0;
    private Mildemo parent;
   
    public Mainpanel(Mildemo caller){
        parent=caller;
        date=new Dater();
        date.set(2200,0,1,0,0);
        meny=Toolkit.getDefaultToolkit().getImage("meny.png");
        base=Toolkit.getDefaultToolkit().getImage("base.png");

system=Toolkit.getDefaultToolkit().getImage("system.png");
        this.setBackground(Color.BLACK);
        this.setSize(1024,768);
       
        MediaTracker tracker = new MediaTracker(this);
       tracker.addImage(meny,0);
       tracker.addImage(base,1);
       tracker.addImage(system,2);
              
       try {
            tracker.waitForAll(0);
       }
       catch(InterruptedException ev){
            ev.printStackTrace();
            System.exit(1);
       }
              
       
       if(tracker.isErrorID(0)){
            System.err.println("Error loading images");
        }
       
        System.out.println("Loading Images: Done");
           
    }
    public void actionPerformed(ActionEvent e){
    }
   
    public void mouseClicked(MouseEvent e) {
        System.out.println("Clicked: " +e.getX() +e.getY());
           
    }
   
    public void mouseEntered(MouseEvent e) {
        System.out.println("moving!");
       
    }
   
    public void mouseExited(MouseEvent e) {
        System.out.println("Outing!");
   
    }
   
    public void mousePressed(MouseEvent e) {
        System.out.println("Pressing");
   
    }
   
    public void mouseReleased(MouseEvent e) {
        System.out.println("Releasing");
    }
   
   
   
    public void paint(Graphics g){
        g.setColor(Color.WHITE);
        g.drawImage(meny,10,30,parent);
        g.drawString("Installation: Moon",20,20);
        g.drawImage(base,5,180,parent);
        g.drawString(date.timeS(),800,45);
        g.drawString(date.dateS(),800,55);
   
       
       
    }

}

/* and last the Calendar class, but I think it has nothing to do with
the problem, but it's included for completeness. */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Calendar;

public class Dater extends Calendar {
    public void add(int field, int amount){
    }
    public void roll(int field, boolean up){
    }
    public int getMinimum(int field){
        return 0;
    }
    public int getMaximum(int field){
        return 0;
    }
    public int getLeastMaximum(int field){
        return 0;
    }
    public int getGreatestMinimum(int field){
        return 0;
    }
    public int getGreatestmaximum(int field){
        return 0;
    }
    protected void computeTime(){
    }
    protected void computeFields(){
    }
    public String timeS(){
        int hour = this.get(this.HOUR);
        int min = this.get(this.MINUTE);
    //    String hours = new String(Integer.toString(hour));;
   
   
        String timeh=new String((hour < 10) ?
"0"+Integer.toString(hour):Integer.toString(hour));
        String timem=new String((min<10)?
"0"+Integer.toString(min):Integer.toString(min));
       
        return timeh +":" +timem;
       
    }
    public String dateS(){
        int day = this.get(this.DAY_OF_MONTH);
        int month= this.get(this.MONTH);
        String days = new String((day<10) ?
"0"+Integer.toString(day):Integer.toString(day));
        String months= new String();
        switch(month){
            case 0: months=new String(" January "); break;
            case 1: months=new String(" February ");
break;
            case 2: months=new String(" March "); break;
            case 3: months=new String(" April "); break;
            case 4: months=new String(" May "); break;
            case 5: months=new String(" June "); break;
            case 6: months=new String(" July "); break;
            case 7: months=new String(" August "); break;
            case 8: months=new String(" September ");
break;
            case 9: months=new String(" October "); break;
            case 10: months=new String(" November ");
break;
            case 11: months=new String(" December ");
break;
        }
        return days +months
+Integer.toString(this.get(this.YEAR));
    }
}

As I said, any help is welcome!
/Daniel
Knute Johnson - 07 Apr 2004 22:50 GMT
> As I said, any help is welcome!
> /Daniel

Daniel:

You've got a lot of code there but nowhere do you add either of your
MouseListeners to a component.  Unless you need all of the methods of a
Listener, use the appropriate Adapters.  It will save a lot of typing.

Component.addMouseListener(new MouseAdapter() {
    public void mouseSomethinged(MouseEvent me) {
    }
});

Signature

Knute Johnson
email s/nospam/knute/
Molon labe...

Daniel - 08 Apr 2004 11:51 GMT
Thanks. as I stated I was doing something stupid.
I missed the addMouseListener method.
Oh well, now it works :-)
Thanks

/Daniel

>> As I said, any help is welcome!
>> /Daniel
[quoted text clipped - 9 lines]
>     }
>});


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.