Why keylistener is not working here?

Posted by swift on Stack Overflow See other posts from Stack Overflow or by swift
Published on 2010-04-10T07:41:01Z Indexed on 2010/04/10 7:43 UTC
Read the original article Hit count: 422

Filed under:

i have implemented keylistener interface and implemented all the needed methods but when i press the key nothing happens here, why?

package swing;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridLayout;
    import java.awt.Point;
    import java.awt.RenderingHints;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.image.BufferedImage;

    import javax.swing.BorderFactory;
    import javax.swing.BoxLayout;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLayeredPane;
    import javax.swing.JPanel;
    import javax.swing.JTextArea;



    class   Paper extends JPanel implements MouseListener,MouseMotionListener,ActionListener,KeyListener
        {
            static BufferedImage image;
            String shape;
            Color color=Color.black;
            Point start;
            Point end;
            Point mp;
            Button elipse=new Button("elipse");
            int x[]=new int[50];
            int y[]=new int[50];
            Button rectangle=new Button("rect");
            Button line=new Button("line");
            Button roundrect=new Button("roundrect");
            Button polygon=new Button("poly");
            Button text=new Button("text");
            ImageIcon erasericon=new ImageIcon("images/eraser.gif");
            JButton erase=new JButton(erasericon);
            JButton[] colourbutton=new JButton[9];
            String selected;
            Point label;
            String key;
            int ex,ey;//eraser
            //DatagramSocket dataSocket;
            JButton button = new JButton("test");
            JLayeredPane layerpane;
            Point p=new Point();
            int w,h;
            public Paper() 
            {        
                JFrame frame=new JFrame("Whiteboard");
             frame.setVisible(true);
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setSize(640, 480);
             frame.setBackground(Color.black);



             layerpane=frame.getLayeredPane();

             setWidth(539,444);
            setBounds(69,0,555,444);



            layerpane.add(this,new Integer(2));
            layerpane.add(this.addButtons(),new Integer(0));

                setLayout(null);
                setOpaque(false);


                addMouseListener(this);
                addMouseMotionListener(this);

                setFocusable(true);
                addKeyListener(this);

                System.out.println(isFocusable());
                setBorder(BorderFactory.createLineBorder(Color.black));         
            }
               public  void paintComponent(Graphics g) 
               {
                try
                {
                    super.paintComponent(g);
                    g.drawImage(image, 0, 0, this);
                    Graphics2D g2 = (Graphics2D)g;
                    if(color!=null)
                    g2.setPaint(color);
                    if(start!=null && end!=null)
                    {
                        if(selected==("elipse"))
                            g2.drawOval(start.x, start.y,(end.x-start.x),(end.y-start.y));
                        else if(selected==("rect"))
                            g2.drawRect(start.x, start.y, (end.x-start.x),(end.y-start.y));
                        else if(selected==("rrect"))
                            g2.drawRoundRect(start.x, start.y, (end.x-start.x),(end.y-start.y),11,11);
                        else if(selected==("line"))
                            g2.drawLine(start.x,start.y,end.x,end.y);
                        else if(selected==("poly"))
                            g2.drawPolygon(x,y,2);
                    }
                }
                    catch(Exception e)
                    {}
            }
            //Function to draw the shape on image
            public  void draw()
            {       
                Graphics2D g2 = image.createGraphics();
                g2.setPaint(color);
                if(start!=null && end!=null)
                {
                    if(selected=="line")
                            g2.drawLine(start.x, start.y, end.x, end.y);
                    else if(selected=="elipse")
                            g2.drawOval(start.x, start.y, (end.x-start.x),(end.y-start.y));
                    else if(selected=="rect")
                            g2.drawRect(start.x, start.y, (end.x-start.x),(end.y-start.y));
                    else if(selected==("rrect"))
                        g2.drawRoundRect(start.x, start.y, (end.x-start.x),(end.y-start.y),11,11);
                    else if(selected==("poly"))
                        g2.drawPolygon(x,y,2);

                }
                if(label!=null)
                {
                    JTextArea textarea=new JTextArea();

                    if(selected==("text"))
                    {
                        textarea.setBounds(label.x, label.y, 50, 50);
                        textarea.setMaximumSize(new Dimension(100,100));
                        textarea.setBackground(new Color(237,237,237));

                        add(textarea);

                        g2.drawString("key",label.x,label.y);
                    }
                }
                start=null;
                repaint();
                g2.dispose();
            } 
            public void text()
            {
                System.out.println(label);

            }
            //Function which provides the erase functionality
            public  void erase() 
            {
                Graphics2D pic=(Graphics2D) image.getGraphics();
                Color erasecolor=new Color(237,237,237);
                pic.setPaint(erasecolor);
                if(start!=null)
                pic.fillRect(start.x, start.y, 10, 10);
            }
            //To set the size of the image
            public void setWidth(int x,int y)
            {
                System.out.println("("+x+","+y+")");
                w=x;
                h=y;
                image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
             }
            //Function to add buttons into the panel, calling this function returns a panel  
            public JPanel addButtons()
            {
                JPanel buttonpanel=new JPanel();
                buttonpanel.setMaximumSize(new Dimension(70,70));
                JPanel shape=new JPanel();
                JPanel colourbox=new JPanel();


                shape.setLayout(new GridLayout(4,2));
                shape.setMaximumSize(new Dimension(70,140));

                colourbox.setLayout(new GridLayout(3,3));
                colourbox.setMaximumSize(new Dimension(70,70));

                buttonpanel.setLayout(new BoxLayout(buttonpanel,BoxLayout.Y_AXIS));

                elipse.addActionListener(this);
                elipse.setToolTipText("Elipse");
                rectangle.addActionListener(this);
                rectangle.setToolTipText("Rectangle");
                line.addActionListener( this);
                line.setToolTipText("Line");
                erase.addActionListener(this);
                erase.setToolTipText("Eraser");
                roundrect.addActionListener(this);
                roundrect.setToolTipText("Round rect");
                polygon.addActionListener(this);
                polygon.setToolTipText("Polygon");
                text.addActionListener(this);
                text.setToolTipText("Text");
                shape.add(elipse);
                shape.add(rectangle);
                shape.add(line);
                shape.add(erase);
                shape.add(roundrect);
                shape.add(polygon);
                shape.add(text);
                buttonpanel.add(shape);

                for(int i=0;i<9;i++)
                {
                    colourbutton[i]=new JButton();
                    colourbox.add(colourbutton[i]);
                    if(i==0)
                        colourbutton[0].setBackground(Color.black);
                    else if(i==1)
                        colourbutton[1].setBackground(Color.white);
                    else if(i==2)
                        colourbutton[2].setBackground(Color.red);
                    else if(i==3)
                        colourbutton[3].setBackground(Color.orange);
                    else if(i==4)
                        colourbutton[4].setBackground(Color.blue);
                    else if(i==5)
                        colourbutton[5].setBackground(Color.green);
                    else if(i==6)
                        colourbutton[6].setBackground(Color.pink);
                    else if(i==7)
                        colourbutton[7].setBackground(Color.magenta);
                    else if(i==8)
                        colourbutton[8].setBackground(Color.cyan);
                    colourbutton[i].addActionListener(this);
                }
                buttonpanel.add(colourbox);
                buttonpanel.setBounds(0, 0, 70, 210);
                return buttonpanel;
            }

            public void mouseClicked(MouseEvent e) 
            {
                if(selected=="text")
                {
                    label=new Point();
                    label=e.getPoint();

                    draw();
                }
            }
            @Override
            public void mouseEntered(MouseEvent arg0) 
            {
            }
            public void mouseExited(MouseEvent arg0) {

            }
            public void mousePressed(MouseEvent e) 
            {
                    if(selected=="line"||selected=="erase"||selected=="text")
                    {
                        start=e.getPoint();

                    }
                    else if(selected=="elipse"||selected=="rect"||selected=="rrect")
                    {
                        mp = e.getPoint();

                    }
                    else if(selected=="poly")
                    {
                        x[0]=e.getX();
                        y[0]=e.getY();
                    }

            }
            public void mouseReleased(MouseEvent e) 
            {
                if(start!=null)
                {
                    if(selected=="line")
                    {
                        end=e.getPoint();           

                    }
                    else if(selected=="elipse"||selected=="rect"||selected=="rrect")
                    {
                        end.x = Math.max(mp.x,e.getX());
                        end.y = Math.max(mp.y,e.getY());

                    }
                    else if(selected=="poly")
                    {
                        x[1]=e.getX();
                        y[1]=e.getY();
                    }           
                    draw();
                }
            }
            public void mouseDragged(MouseEvent e) 
            {
                if(end==null)
                    end = new Point();

                if(start==null)
                        start = new Point();

                 if(selected=="line")
                 {
                    end=e.getPoint();

                 }
                else if(selected=="erase")
                {
                     start=e.getPoint();
                     erase();

                }
                else if(selected=="elipse"||selected=="rect"||selected=="rrect")
                {
                    start.x = Math.min(mp.x,e.getX());
                    start.y = Math.min(mp.y,e.getY());
                    end.x = Math.max(mp.x,e.getX());
                    end.y = Math.max(mp.y,e.getY());

                }
                else if(selected=="poly")
                {
                    x[1]=e.getX();
                    y[1]=e.getY();
                }

                repaint();
            }
            public void mouseMoved(MouseEvent arg0) {} 
            public void actionPerformed(ActionEvent e) 
            {
                if(e.getSource()==elipse)
                    selected="elipse";
                else if(e.getSource()==line)
                    selected="line";    
                else if(e.getSource()==rectangle)
                    selected="rect";
                else if(e.getSource()==erase)
                {
                    selected="erase";
                    System.out.println(selected);
                    erase();
                }
                else if(e.getSource()==roundrect)
                        selected="rrect";
                else if(e.getSource()==polygon)
                    selected="poly";
                else if(e.getSource()==text)
                    selected="text";


                if(e.getSource()==colourbutton[0])
                    color=Color.black;
                else if(e.getSource()==colourbutton[1])
                    color=Color.white;
                else if(e.getSource()==colourbutton[2])
                    color=Color.red;
                else if(e.getSource()==colourbutton[3])
                    color=Color.orange;
                else if(e.getSource()==colourbutton[4])
                    color=Color.blue;
                else if(e.getSource()==colourbutton[5])
                    color=Color.green;
                else if(e.getSource()==colourbutton[6])
                    color=Color.pink;
                else if(e.getSource()==colourbutton[7])
                    color=Color.magenta;
                else if(e.getSource()==colourbutton[8])
                    color=Color.cyan;

            }
            @Override
            public void keyPressed(KeyEvent e) 
            {
                System.out.println("pressed");
            }
            @Override
            public void keyReleased(KeyEvent e) 
            {
                System.out.println("key released");
            }
            @Override
            public void keyTyped(KeyEvent e) 
            {
                System.out.println("Typed");
            }
            public static void main(String[] a)
            {
                new Paper();
            }
        } 

        class Button extends JButton
        {
            String name;
            public Button(String name) 
            {
                this.name=name; 
            }
            public void paintComponent(Graphics g)
            {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D)g;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                //g2.setStroke(new BasicStroke(1.2f));
                if (name == "line")     g.drawLine(5,5,30,30);   
                if (name == "elipse") g.drawOval(5,7,25,20);
                if (name== "rect") g.drawRect(5,5,25,23);
                if (name== "roundrect") g.drawRoundRect(5,5,25,23,10,10);
                int a[]=new int[]{20,9,20,23,20};
                int b[]=new int[]{9,23,25,20,9};
                if (name== "poly") g.drawPolyline(a, b, 5);
                if (name== "text") g.drawString("Text",5, 22);
            }
        }

© Stack Overflow or respective owner

Related posts about s