Results 1 to 3 of 3

Thread: Event headache

  1. #1
    kammenos is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default Event headache

    2 days now i am trying to implement something that i think is easy for someone that knows better java than me. Actually, i know java for...... 3days now... :D

    But i know very very good visual basic, and i am trying to make a custom event. I will explain. I have a class, for example a class named ClassMenuButton (heh)... This class will create a canvas with an image inside and some text. This canvas is supposed to be an Image-Icon. That is the idea. I want to make an image icon. So, this means that when the user click on the canvas, the class "ClassMenuButton" should raise an event that another class (that i have name "Main") should listen to it.

    Please help. 2 days i am googling the net but no help from anywhere. I feel my head 3 tons.

    Following i include the classes. First the ClassMenuButton
    Java Code:
    package MainP_Package;
    import java.awt.*;
    import java.lang.*;
    import java.applet.*;
    import java.awt.image.*;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import java.awt.event.*;
    
    class ClassMenuButton extends Canvas implements ActionListener{
        Image img;
        Color LightBorderColor= Color.BLACK;
        Color DarkBorderColor= Color.LIGHT_GRAY;
        Color ShadowColor= Color.GRAY;
        Color ButtonColor= Color.WHITE;
        URL url = null;
        int BorderWidth=8,BorderShadow=2,ButtonOverMove=1,ButtonOffset=0;
        String ButtonText="";
    
        public void SetImage(String NewImagename){
            MediaTracker tr;
    
            tr = new MediaTracker(this);
            Toolkit toolkit = Toolkit.getDefaultToolkit();
    
            img = toolkit.getImage(NewImagename);
            tr.addImage(img, 0);
            try {tr.waitForID(0);}
            catch (Exception e) {System.out.println  ("Nope");}
        }
        public void SetText(String NewText){
            ButtonText=NewText;
        }
    
        public void paint(Graphics g) {
            Rectangle r = bounds();
    
            g.setColor(ShadowColor);
            g.fillRect(BorderWidth+BorderShadow, BorderWidth+BorderShadow,r.width-BorderWidth*2+BorderShadow,r.height-BorderWidth*2+BorderShadow);
    
            g.setColor(ButtonColor);
            g.fillRect(BorderWidth+ButtonOffset, BorderWidth+ButtonOffset,r.width-BorderWidth*2+ButtonOffset,r.height-BorderWidth*2+ButtonOffset);
    
            g.setColor(DarkBorderColor);
            g.drawLine(BorderWidth+ButtonOffset,BorderWidth+ButtonOffset,r.width-BorderWidth+ButtonOffset,BorderWidth+ButtonOffset);
            g.drawLine(BorderWidth+ButtonOffset,r.height-BorderWidth+ButtonOffset,BorderWidth+ButtonOffset,BorderWidth+ButtonOffset);
    
            g.setColor(LightBorderColor);
            g.drawLine(r.width-BorderWidth+ButtonOffset,BorderWidth+ButtonOffset,r.width-BorderWidth+ButtonOffset,r.height-BorderWidth+ButtonOffset);
            g.drawLine(r.width-BorderWidth+ButtonOffset,r.height-BorderWidth+ButtonOffset,BorderWidth+ButtonOffset,r.height-BorderWidth+ButtonOffset);
    
            //System.out.println  ("My ");
            g.drawImage(img, (r.width-32)/2+ButtonOffset,10+ButtonOffset, this);
            
            Font f = new Font("MS Sans Sheriff",Font.PLAIN, 10);
            FontMetrics fm = getFontMetrics(f);
            g.setFont(f);
            //String s = "Next";
            int xstart = (this.size().width -fm.stringWidth(ButtonText)) / 2;        
            int ystart = r.height-BorderWidth-4;
            
            g.drawString(ButtonText, xstart+ButtonOffset, ystart+ButtonOffset);
        } 
    
        public boolean handleEvent(Event evt) {
            
            
            switch(evt.id) {
            // This can be handled
            case Event.MOUSE_ENTER: {
                ButtonOffset=ButtonOverMove;
                this.repaint();
                break;
            }    
            case Event.MOUSE_EXIT: {
                ButtonOffset=0;
                this.repaint();
                break;
            }    
            case Event.MOUSE_UP: {
                System.out.println  ("Me pressed!!! ");
                break;
            }    
    
            }
            return false;
        }
    }

    And the Main class:
    Java Code:
    package MainP_Package;
    
    import java.awt.*;
    import java.lang.*;
    import java.applet.*;
    
    public class Main extends Applet {
    
        ClassPanelCanvas PanelCanvas;
        ClassPatchCanvas PatchCanvas;
    
        public void init() {
            PanelCanvas = new ClassPanelCanvas();
            PatchCanvas = new ClassPatchCanvas();
    
            Button b;
            Label l;
            resize(1000, 700); 
    
            GridBagLayout g = new GridBagLayout();
            setLayout(g);
            GridBagConstraints gbc = new GridBagConstraints();
    
            gbc.fill = GridBagConstraints.BOTH;
    
            gbc.gridwidth = 10;
            gbc.weighty = 10;
            gbc.weightx = 1.0;
            g.setConstraints(PanelCanvas, gbc);
            add(PanelCanvas);
    
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.weighty = 5.0;
            g.setConstraints(PatchCanvas, gbc);
            add(PatchCanvas);
    
            ClassMenuButton c;
     
            gbc.weightx = 1.0; 
            //for (int i = 1; i <= 14; ++i) {
            gbc.weighty = 1.0;
            gbc.gridwidth = 1;
            ///////////////////////////////////////////////////////
            // button 1
            c = new ClassMenuButton();
            c.SetImage("/home/kammenos/Desktop/Temp/prev.png");
            c.SetText("Previous");
            g.setConstraints(c, gbc);add(c);
            ///////////////////////////////////////////////////////
            // button 2
            c = new ClassMenuButton();
            c.SetImage("/home/kammenos/Desktop/Temp/next.png");
                c.SetText("Next");
            g.setConstraints(c, gbc);add(c);
            ///////////////////////////////////////////////////////
            // button 3
            c = new ClassMenuButton();
            c.SetImage("/home/kammenos/Desktop/Temp/next.png");
            c.SetText("Previous");
            g.setConstraints(c, gbc);add(c);
            ///////////////////////////////////////////////////////
            // button 4
            c = new ClassMenuButton();
            c.SetImage("/home/kammenos/Desktop/Temp/next.png");
            c.SetText("Previous");
            g.setConstraints(c, gbc);add(c);
            ///////////////////////////////////////////////////////
            // button 5
            c = new ClassMenuButton();
            c.SetImage("/home/kammenos/Desktop/Temp/next.png");
            c.SetText("Previous");
            g.setConstraints(c, gbc);add(c);
            ///////////////////////////////////////////////////////
            // button 6
            c = new ClassMenuButton();
            c.SetImage("/home/kammenos/Desktop/Temp/next.png");
            c.SetText("Previous");
            g.setConstraints(c, gbc);add(c);
            ///////////////////////////////////////////////////////
            // button 7
            c = new ClassMenuButton();
            c.SetImage("/home/kammenos/Desktop/Temp/next.png");
            c.SetText("Previous");
            g.setConstraints(c, gbc);add(c);
            ///////////////////////////////////////////////////////
            // button 8
            c = new ClassMenuButton();
            c.SetImage("/home/kammenos/Desktop/Temp/next.png");
            c.SetText("Previous");
            g.setConstraints(c, gbc);add(c);
            ///////////////////////////////////////////////////////
            // button 9
            c = new ClassMenuButton();
            c.SetImage("/home/kammenos/Desktop/Temp/next.png");
            c.SetText("Previous");
            g.setConstraints(c, gbc);add(c);
            ///////////////////////////////////////////////////////
            // button 10
            c = new ClassMenuButton();
            c.SetImage("/home/kammenos/Desktop/Temp/next.png");
            c.SetText("Previous");
            g.setConstraints(c, gbc);add(c);
            ///////////////////////////////////////////////////////
            // button 11
            c = new ClassMenuButton();
            c.SetImage("/home/kammenos/Desktop/Temp/next.png");
            c.SetText("Previous");
            g.setConstraints(c, gbc);add(c);
            ///////////////////////////////////////////////////////
            // button 12
            c = new ClassMenuButton();
            c.SetImage("/home/kammenos/Desktop/Temp/next.png");
            c.SetText("Previous");
            g.setConstraints(c, gbc);add(c);
            ///////////////////////////////////////////////////////
            gbc.weightx = 1.0;
            
        }
    }

  2. #2
    kammenos is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default

    Anyone? Please?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your code doesn't compile so it's hard to know what's not working here. The biggest problem is that it appears to be missing some classes. And your first class is missing the actionPerformed method.

    Also, myself, I know Swing much better than AWT, and from my experience here and in other fora, I think that I'm more the rule than the exception. Why are you using the older less flexible technology of AWT and not Swing? Finally, how much of this code above is yours and how much has been obtained elsewhere?

Similar Threads

  1. MouseDragged Event
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 03-04-2008, 06:09 AM
  2. Event
    By nt5515 in forum New To Java
    Replies: 0
    Last Post: 02-15-2008, 05:44 PM
  3. Listener for SWT event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-08-2008, 10:04 AM
  4. SWT Event Handling
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-30-2007, 01:21 PM
  5. Event Handling
    By luisarca in forum Sun Java Wireless Toolkit
    Replies: 5
    Last Post: 05-07-2007, 07:05 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
  •