Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: keylistener small problem

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default keylistener small problem

    Hello,

    I wrote this simple and neat code and implemented a mouselistener and a keylistener, but for reason or another the keylistener is not doing its job...can anyone help please?
    package chapter2;
     
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
     
    public class DrawShapes extends JApplet implements ActionListener {
      public static void main(String s[]) {
        JFrame frame = new JFrame();
        frame.setTitle("Drawing Geometric Shapes");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JApplet applet = new DrawShapes();
        applet.init();
        frame.getContentPane().add(applet);
        frame.pack();
        frame.setVisible(true);
     
      }
     
      JavaDraw2DPanel panel = null;
     
     
      public void init() {
        JMenuBar mb = new JMenuBar();
        setJMenuBar(mb);
        JMenu menu = new JMenu("Objects");
        mb.add(menu);
        JMenuItem mi = new JMenuItem("Rectangle");
        mi.addActionListener(this);
        menu.add(mi);
        mi = new JMenuItem("RoundRectangle");
        mi.addActionListener(this);
        menu.add(mi);
        mi = new JMenuItem("Ellipse");
        mi.addActionListener(this);
        menu.add(mi);
        mi = new JMenuItem("Arc");
        mi.addActionListener(this);
        menu.add(mi);
        mi = new JMenuItem("Line");
        mi.addActionListener(this);
        menu.add(mi);
        mi = new JMenuItem("QuadCurve");
        mi.addActionListener(this);
        menu.add(mi);
        mi = new JMenuItem("CubicCurve");
        mi.addActionListener(this);
        menu.add(mi);
        mi = new JMenuItem("Polygon");
        mi.addActionListener(this);
        menu.add(mi);
        panel = new JavaDraw2DPanel();
        getContentPane().add(panel);
     
     
      }
     
      public void actionPerformed(ActionEvent ev) {
     
        String command = ev.getActionCommand();
        if ("Rectangle".equals(command)) {
          panel.shapeType = panel.RECTANGLE;
        } else if ("RoundRectangle".equals(command)) {
          panel.shapeType = panel.ROUNDRECTANGLE2D;
        } else if ("Ellipse".equals(command)) {
          panel.shapeType = panel.ELLIPSE2D;
        } else if ("Arc".equals(command)) {
          panel.shapeType = panel.ARC2D;
        } else if ("Line".equals(command)) {
          panel.shapeType = panel.LINE2D;
        } else if ("QuadCurve".equals(command)) {
          panel.shapeType = panel.QUADCURVE2D;
        } else if ("CubicCurve".equals(command)) {
          panel.shapeType = panel.CUBICCURVE2D;
        } else if ("Polygon".equals(command)) {
          panel.shapeType = panel.POLYGON;
        }
      }
     
     
     
    }
     
    class JavaDraw2DPanel extends JPanel implements MouseListener, MouseMotionListener, KeyListener{
      private Vector shapes = new Vector();
      static final int RECTANGLE = 0;
      static final int ROUNDRECTANGLE2D = 1;
      static final int ELLIPSE2D = 2;
      static final int ARC2D = 3;
      static final int LINE2D = 4;
      static final int QUADCURVE2D = 5;
      static final int CUBICCURVE2D = 6;
      static final int POLYGON = 7;
      static final int GENERAL = 8;
      static final int AREA = 9;
     
     
      int shapeType = RECTANGLE;
      // vector of input points
      Vector points = new Vector();
      int pointIndex = 0;
      Shape partialShape = null;
      Point p = null;
     
      public JavaDraw2DPanel() {
        super();
        setBackground(Color.white);
        setPreferredSize(new Dimension(640, 480));
        addKeyListener(this);
        addMouseListener(this);
        addMouseMotionListener(this);
      }
     
      public void paintComponent(Graphics g) {
     
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        for (int i = 0; i < shapes.size(); i++) {
          Shape s = (Shape)shapes.get(i);
          g2.draw(s);
        }
      }
     
     
     
     
     
     
      public void mouseClicked(MouseEvent ev) {
      }
     
      public void mouseEntered(MouseEvent ev) {
      }
     
      public void mouseExited(MouseEvent ev) {
      }
     
      public void mousePressed(MouseEvent ev) {
        points.add(ev.getPoint());
        pointIndex++;
        p = null;
      }
     
      public void mouseReleased(MouseEvent ev) {
      }
     
      public void mouseMoved(MouseEvent ev) {
      }
     
      public void mouseDragged(MouseEvent ev) {
        Graphics2D g = (Graphics2D)getGraphics();
        g.setXORMode(Color.white);
        Point p1 = (Point)points.get(pointIndex-1);
        switch (shapeType) {
          case RECTANGLE:
            if (p != null) g.drawRect(p1.x, p1.y, p.x-p1.x, p.y-p1.y);
            p = ev.getPoint();
            g.drawRect(p1.x, p1.y, p.x-p1.x, p.y-p1.y);
            break;
          case ROUNDRECTANGLE2D:
            if (p != null) g.drawRoundRect(p1.x, p1.y, p.x-p1.x, p.y-p1.y,10,10);
            p = ev.getPoint();
            g.drawRoundRect(p1.x, p1.y, p.x-p1.x, p.y-p1.y,10,10);
            break;
          case ELLIPSE2D:
            if (p != null) g.drawOval(p1.x, p1.y, p.x-p1.x, p.y-p1.y);
            p = ev.getPoint();
            g.drawOval(p1.x, p1.y, p.x-p1.x, p.y-p1.y);
            break;
          case ARC2D:
            if (p != null) g.drawArc(p1.x, p1.y, p.x-p1.x, p.y-p1.y, 30, 120);
            p = ev.getPoint();
            g.drawArc(p1.x, p1.y, p.x-p1.x, p.y-p1.y, 30, 120);
            break;
          case LINE2D:
          case POLYGON:
            if (p != null) g.drawLine(p1.x, p1.y, p.x, p.y);
            p = ev.getPoint();
            g.drawLine(p1.x, p1.y, p.x, p.y);
            break;
          case QUADCURVE2D:
            if (pointIndex == 1) {
              if (p != null) g.drawLine(p1.x, p1.y, p.x, p.y);
              p = ev.getPoint();
              g.drawLine(p1.x, p1.y, p.x, p.y);
            } else {
              Point p2 = (Point)points.get(pointIndex-2);
              if (p != null) g.draw(partialShape);
              p = ev.getPoint();
              partialShape = new QuadCurve2D.Float(p2.x, p2.y, p1.x, p1.y, p.x, p.y);
              g.draw(partialShape);
            }
            break;
          case CUBICCURVE2D:
            if (pointIndex == 1) {
              if (p != null) g.drawLine(p1.x, p1.y, p.x, p.y);
              p = ev.getPoint();
              g.drawLine(p1.x, p1.y, p.x, p.y);
            } else if (pointIndex == 2) {
              Point p2 = (Point)points.get(pointIndex-2);
              if (p != null) g.draw(partialShape);
              p = ev.getPoint();
              partialShape = new QuadCurve2D.Float(p2.x, p2.y, p1.x, p1.y, p.x, p.y);
              g.draw(partialShape);
            } else {
              Point p2 = (Point)points.get(pointIndex-2);
              Point p3 = (Point)points.get(pointIndex-3);
              if (p != null) g.draw(partialShape);
              p = ev.getPoint();
              partialShape = new CubicCurve2D.Float(p3.x, p3.y, p2.x, p2.y, p1.x, p1.y, p.x, p.y);
              g.draw(partialShape);
            }
            break;
        }
      }
     
        public void keyTyped(KeyEvent e) {
            System.out.println("test");
            throw new UnsupportedOperationException("Not supported yet.");
        }
     
        public void keyPressed(KeyEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
     
        public void keyReleased(KeyEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
    Last edited by copeg; November 14th, 2010 at 05:28 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: keylistener small problem

    Quote Originally Posted by matecno View Post
    Hello,

    I wrote this simple and neat code and implemented a mouselistener and a keylistener, but for reason or another the keylistener is not doing its job...can anyone help please?

    package chapter2;
     
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
     
    public class DrawShapes extends JApplet implements ActionListener {
      public static void main(String s[]) {
        JFrame frame = new JFrame();
        frame.setTitle("Drawing Geometric Shapes");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JApplet applet = new DrawShapes();
        applet.init();
        frame.getContentPane().add(applet);
        frame.pack();
        frame.setVisible(true);
     
      }
     
      JavaDraw2DPanel panel = null;
     
     
      public void init() {
        JMenuBar mb = new JMenuBar();
        setJMenuBar(mb);
        JMenu menu = new JMenu("Objects");
        mb.add(menu);
        JMenuItem mi = new JMenuItem("Rectangle");
        mi.addActionListener(this);
        menu.add(mi);
        mi = new JMenuItem("RoundRectangle");
        mi.addActionListener(this);
        menu.add(mi);
        mi = new JMenuItem("Ellipse");
        mi.addActionListener(this);
        menu.add(mi);
        mi = new JMenuItem("Arc");
        mi.addActionListener(this);
        menu.add(mi);
        mi = new JMenuItem("Line");
        mi.addActionListener(this);
        menu.add(mi);
        mi = new JMenuItem("QuadCurve");
        mi.addActionListener(this);
        menu.add(mi);
        mi = new JMenuItem("CubicCurve");
        mi.addActionListener(this);
        menu.add(mi);
        mi = new JMenuItem("Polygon");
        mi.addActionListener(this);
        menu.add(mi);
        panel = new JavaDraw2DPanel();
        getContentPane().add(panel);
     
     
      }
     
      public void actionPerformed(ActionEvent ev) {
     
        String command = ev.getActionCommand();
        if ("Rectangle".equals(command)) {
          panel.shapeType = panel.RECTANGLE;
        } else if ("RoundRectangle".equals(command)) {
          panel.shapeType = panel.ROUNDRECTANGLE2D;
        } else if ("Ellipse".equals(command)) {
          panel.shapeType = panel.ELLIPSE2D;
        } else if ("Arc".equals(command)) {
          panel.shapeType = panel.ARC2D;
        } else if ("Line".equals(command)) {
          panel.shapeType = panel.LINE2D;
        } else if ("QuadCurve".equals(command)) {
          panel.shapeType = panel.QUADCURVE2D;
        } else if ("CubicCurve".equals(command)) {
          panel.shapeType = panel.CUBICCURVE2D;
        } else if ("Polygon".equals(command)) {
          panel.shapeType = panel.POLYGON;
        }
      }
     
     
     
    }

    class JavaDraw2DPanel extends JPanel implements MouseListener, MouseMotionListener, KeyListener{
      private Vector shapes = new Vector();
      static final int RECTANGLE = 0;
      static final int ROUNDRECTANGLE2D = 1;
      static final int ELLIPSE2D = 2;
      static final int ARC2D = 3;
      static final int LINE2D = 4;
      static final int QUADCURVE2D = 5;
      static final int CUBICCURVE2D = 6;
      static final int POLYGON = 7;
      static final int GENERAL = 8;
      static final int AREA = 9;
     
     
      int shapeType = RECTANGLE;
      // vector of input points
      Vector points = new Vector();
      int pointIndex = 0;
      Shape partialShape = null;
      Point p = null;
     
      public JavaDraw2DPanel() {
        super();
        setBackground(Color.white);
        setPreferredSize(new Dimension(640, 480));
        addKeyListener(this);
        addMouseListener(this);
        addMouseMotionListener(this);
      }
     
      public void paintComponent(Graphics g) {
     
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        for (int i = 0; i < shapes.size(); i++) {
          Shape s = (Shape)shapes.get(i);
          g2.draw(s);
        }
      }
     
     
     
     
     
     
      public void mouseClicked(MouseEvent ev) {
      }
     
      public void mouseEntered(MouseEvent ev) {
      }
     
      public void mouseExited(MouseEvent ev) {
      }
     
      public void mousePressed(MouseEvent ev) {
        points.add(ev.getPoint());
        pointIndex++;
        p = null;
      }
     
      public void mouseReleased(MouseEvent ev) {
      }
     
      public void mouseMoved(MouseEvent ev) {
      }
     
      public void mouseDragged(MouseEvent ev) {
        Graphics2D g = (Graphics2D)getGraphics();
        g.setXORMode(Color.white);
        Point p1 = (Point)points.get(pointIndex-1);
        switch (shapeType) {
          case RECTANGLE:
            if (p != null) g.drawRect(p1.x, p1.y, p.x-p1.x, p.y-p1.y);
            p = ev.getPoint();
            g.drawRect(p1.x, p1.y, p.x-p1.x, p.y-p1.y);
            break;
          case ROUNDRECTANGLE2D:
            if (p != null) g.drawRoundRect(p1.x, p1.y, p.x-p1.x, p.y-p1.y,10,10);
            p = ev.getPoint();
            g.drawRoundRect(p1.x, p1.y, p.x-p1.x, p.y-p1.y,10,10);
            break;
          case ELLIPSE2D:
            if (p != null) g.drawOval(p1.x, p1.y, p.x-p1.x, p.y-p1.y);
            p = ev.getPoint();
            g.drawOval(p1.x, p1.y, p.x-p1.x, p.y-p1.y);
            break;
          case ARC2D:
            if (p != null) g.drawArc(p1.x, p1.y, p.x-p1.x, p.y-p1.y, 30, 120);
            p = ev.getPoint();
            g.drawArc(p1.x, p1.y, p.x-p1.x, p.y-p1.y, 30, 120);
            break;
          case LINE2D:
          case POLYGON:
            if (p != null) g.drawLine(p1.x, p1.y, p.x, p.y);
            p = ev.getPoint();
            g.drawLine(p1.x, p1.y, p.x, p.y);
            break;
          case QUADCURVE2D:
            if (pointIndex == 1) {
              if (p != null) g.drawLine(p1.x, p1.y, p.x, p.y);
              p = ev.getPoint();
              g.drawLine(p1.x, p1.y, p.x, p.y);
            } else {
              Point p2 = (Point)points.get(pointIndex-2);
              if (p != null) g.draw(partialShape);
              p = ev.getPoint();
              partialShape = new QuadCurve2D.Float(p2.x, p2.y, p1.x, p1.y, p.x, p.y);
              g.draw(partialShape);
            }
            break;
          case CUBICCURVE2D:
            if (pointIndex == 1) {
              if (p != null) g.drawLine(p1.x, p1.y, p.x, p.y);
              p = ev.getPoint();
              g.drawLine(p1.x, p1.y, p.x, p.y);
            } else if (pointIndex == 2) {
              Point p2 = (Point)points.get(pointIndex-2);
              if (p != null) g.draw(partialShape);
              p = ev.getPoint();
              partialShape = new QuadCurve2D.Float(p2.x, p2.y, p1.x, p1.y, p.x, p.y);
              g.draw(partialShape);
            } else {
              Point p2 = (Point)points.get(pointIndex-2);
              Point p3 = (Point)points.get(pointIndex-3);
              if (p != null) g.draw(partialShape);
              p = ev.getPoint();
              partialShape = new CubicCurve2D.Float(p3.x, p3.y, p2.x, p2.y, p1.x, p1.y, p.x, p.y);
              g.draw(partialShape);
            }
            break;
        }
      }
     
        public void keyTyped(KeyEvent e) {
            System.out.println("test");
            throw new UnsupportedOperationException("Not supported yet.");
        }
     
        public void keyPressed(KeyEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
     
        public void keyReleased(KeyEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
    Let's see what the problem is.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: keylistener small problem

    What key typed are you looking for?

    You never told it what key in your keyListener.

  4. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: keylistener small problem

    hello,

    Thanks for the reply guys,

    I just want it to print something on the console as soon as I press any key....I building it from the ground up...I am planning to use either the menu or the key-shortcuts on the keyboard to select a shape to draw

    Thanks again guys

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: keylistener small problem

    First, a component must have focus for it to receive KeyEvents, which may be the problem. You can try to call requestFocus and see if that rectifies things. Second, not sure why you are using a JApplet instead of a JPanel. Last, In your situation you might look into using Key Bindings

Similar Threads

  1. Small Project
    By 3XiLED in forum Paid Java Projects
    Replies: 7
    Last Post: March 1st, 2010, 08:35 AM
  2. My KeyListener is not Working!!
    By DarrenReeder in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2010, 05:18 PM
  3. small problem ... I need help
    By Ashar in forum Loops & Control Statements
    Replies: 4
    Last Post: December 4th, 2009, 11:26 AM
  4. Problem with KeyListener
    By r12ki in forum AWT / Java Swing
    Replies: 3
    Last Post: October 1st, 2009, 01:18 PM
  5. Small Project Ideas
    By Freaky Chris in forum Project Collaboration
    Replies: 20
    Last Post: August 12th, 2009, 12:49 PM