By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,693 Members | 1,936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,693 IT Pros & Developers. It's quick & easy.

Problem with typing on JPanel

P: 8
Hi, Im having a problem with typing on the JPanel. The thing is, that it works perfectly fine when this line " content.add(jp, BorderLayout.NORTH); " is taken out (its the buttons panel) but when its added back in, i simply cannot type! HELP!!!!



Codes are as follow:


Expand|Select|Wrap|Line Numbers
  1.  import java.awt.*; 
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. import java.lang.StringBuffer;
  5.  
  6.  
  7. public class JPanels extends JFrame implements MouseListener, MouseMotionListener//, KeyListener
  8. {
  9.  
  10.     private Point oldPoint, currentPoint;
  11.     private String s;
  12.      private FontMetrics fm;
  13.      private JButton circBtn, rectBtn, lineBtn, eraseBtn, clearBtn, sendBtn;
  14.  
  15.     JPanel drawingArea = new JPanel();     
  16.     JPanel jp = new JPanel();     
  17.     TextField message = new TextField("Draw away! Click anywhere on canvas to type");
  18.      int option;
  19.  
  20.     int startx, starty;  // The location of the mouse when the dragging started.
  21.     int mousex, mousey;    // The location of the mouse during dragging.
  22.  
  23.     public void init() {
  24.  
  25.     //initialize the "old" point
  26.     oldPoint = new Point(0,0);
  27.  
  28.     this.setSize(new Dimension(600, 500));
  29.  
  30. }        
  31.  
  32.  
  33.  
  34. public static void main(String[] args) {
  35. new JPanels();
  36.  
  37.  
  38. }
  39.  
  40.  
  41.  
  42.  
  43. public JPanels() {
  44.  
  45.  
  46.     super("Welcome to Whiteboard!");
  47.  
  48.     Container content = getContentPane();
  49.     content.setBackground(Color.lightGray);
  50.  
  51.          circBtn = new JButton("Circle"); 
  52.         rectBtn = new JButton("Rectangle"); 
  53.         lineBtn = new JButton("Line"); 
  54.         eraseBtn = new JButton("Eraser"); 
  55.         clearBtn = new JButton("Clear"); 
  56.         sendBtn = new JButton("Send"); 
  57.  
  58.          jp.setLayout(new GridLayout(0,6));
  59.  
  60.          jp.add(circBtn,0);
  61.          jp.add(rectBtn,1);
  62.          jp.add(lineBtn,2);
  63.          jp.add(eraseBtn,3);
  64.          jp.add(clearBtn,4);
  65.          jp.add(sendBtn,5);
  66.  
  67.  
  68.     message.setBackground(Color.gray);
  69.     message.setEditable(false);
  70.  
  71.     drawingArea.setPreferredSize(new Dimension(500, 400));
  72.     drawingArea.setBorder (BorderFactory.createLineBorder (Color.blue, 2));
  73.     drawingArea.setBackground(Color.white);
  74.  
  75.  
  76.      content.add(jp, BorderLayout.NORTH);
  77.     content.add(drawingArea, BorderLayout.CENTER);
  78.     content.add(message, BorderLayout.SOUTH);
  79.  
  80.     pack();
  81.  
  82.         this.setVisible(true);
  83.         this.setResizable(true);
  84.  
  85.         drawingArea.addMouseListener(this);
  86.         drawingArea.addMouseMotionListener(this);
  87.  
  88.  
  89.         //ActionPerformed methods for the buttons on the side panel
  90.  
  91.          circBtn.setToolTipText("Draw a Circle"); 
  92.          circBtn.addActionListener(new ActionListener() { 
  93.              public void actionPerformed(ActionEvent e) 
  94.              { 
  95.                  circBtn_actionPerformed(e); 
  96.              } 
  97.  
  98.          }); 
  99.  
  100.          rectBtn.setToolTipText("Draw a Rectangle"); 
  101.          rectBtn.addActionListener(new ActionListener() { 
  102.              public void actionPerformed(ActionEvent e) 
  103.              { 
  104.                  rectBtn_actionPerformed(e); 
  105.              } 
  106.  
  107.          }); 
  108.  
  109.          lineBtn.setToolTipText("Draw a Line"); 
  110.          lineBtn.addActionListener(new ActionListener() { 
  111.              public void actionPerformed(ActionEvent e) 
  112.              { 
  113.                  lineBtn_actionPerformed(e); 
  114.              } 
  115.  
  116.          }); 
  117.  
  118.          eraseBtn.setToolTipText("Eraser"); 
  119.          eraseBtn.addActionListener(new ActionListener() { 
  120.              public void actionPerformed(ActionEvent e) 
  121.              { 
  122.                  eraseBtn_actionPerformed(e); 
  123.              } 
  124.  
  125.          }); 
  126.  
  127.          clearBtn.setToolTipText("Clear Canvas"); 
  128.          clearBtn.addActionListener(new ActionListener() { 
  129.              public void actionPerformed(ActionEvent e) 
  130.              { 
  131.                  clearBtn_actionPerformed(e); 
  132.              } 
  133.  
  134.          }); 
  135.  
  136.          sendBtn.setToolTipText("Send drawing to friend"); 
  137.          sendBtn.addActionListener(new ActionListener() { 
  138.              public void actionPerformed(ActionEvent e) 
  139.              { 
  140.                  sendBtn_actionPerformed(e); 
  141.              } 
  142.  
  143.          }); 
  144.  
  145.  
  146.  
  147. }
  148.  
  149.     private void circBtn_actionPerformed(ActionEvent e) 
  150.          { 
  151.              System.out.println("This is the Circle Button test"); 
  152.              // TODO: Add any handling code here 
  153.               option = 1;
  154.          } 
  155.  
  156.      private void rectBtn_actionPerformed(ActionEvent e) 
  157.          { 
  158.              System.out.println("This is the Rect Button test"); 
  159.              // TODO: Add any handling code here 
  160.               option = 2;
  161.          } 
  162.  
  163.      private void lineBtn_actionPerformed(ActionEvent e) 
  164.          { 
  165.              System.out.println("This is the Line Button test"); 
  166.              // TODO: Add any handling code here 
  167.               option = 3;
  168.          } 
  169.  
  170.      private void eraseBtn_actionPerformed(ActionEvent e) 
  171.          { 
  172.              System.out.println("This is the Erase Button test"); 
  173.              // TODO: Add any handling code here 
  174.               option = 4;
  175.          } 
  176.  
  177.      private void clearBtn_actionPerformed(ActionEvent e) 
  178.          { 
  179.              System.out.println("This is the Clear Button test"); 
  180.              // TODO: Add any handling code here 
  181.              drawingArea.repaint();
  182.              message.setText("Canvas cleared!");
  183.  
  184.          } 
  185.  
  186.      private void sendBtn_actionPerformed(ActionEvent e) 
  187.          { 
  188.              System.out.println("This is the Send Button test"); 
  189.              // TODO: Add any handling code here 
  190.  
  191.          } 
  192.  
  193.     //when the mouse is pressed, a point is formed
  194.     public void mousePressed(MouseEvent e){
  195.  
  196.         startx = mousex = e.getX();  // Save coords of mouse position.
  197.         starty = mousey = e.getY();
  198.  
  199.         oldPoint = e.getPoint();
  200.  
  201.         String msg = ("Mouse Pressed: (" + oldPoint.x + ", " + oldPoint.y + ")");    
  202.         System.out.println(msg);
  203.          message.setText(msg);
  204.  
  205.         s = "";
  206.  
  207.     }
  208.  
  209.     //mouseDragged will do the line drawing
  210.     public void mouseDragged(MouseEvent e){
  211.  
  212.         mousex = e.getX();
  213.         mousey = e.getY();
  214.         currentPoint = e.getPoint();
  215.  
  216.         String msg = "Mouse Dragged: (" + currentPoint.x + ", " + currentPoint.y + ")";
  217.         System.out.println(msg);
  218.         message.setText(msg);
  219.  
  220.          if(option == 1){
  221.  
  222.      //drawingArea.getGraphics().drawOval(currentPoint.x, currentPoint.y, oldPoint.x, oldPoint.y);
  223.         Graphics g = drawingArea.getGraphics();
  224.  
  225.         drawOval(g);    
  226.  
  227.  
  228.          }
  229.  
  230.         else if (option == 2){
  231.     //    drawingArea.getGraphics().drawRect(currentPoint.x, currentPoint.y, oldPoint.x, oldPoint.y);
  232.         Graphics g = drawingArea.getGraphics();
  233.         drawRect(g);    
  234.  
  235.         }         
  236.  
  237.          else if (option == 3){
  238.         drawingArea.getGraphics().drawLine(currentPoint.x, currentPoint.y, oldPoint.x, oldPoint.y);
  239.  
  240.         } 
  241.  
  242.         else if (option == 4){
  243.  
  244.         Graphics g = drawingArea.getGraphics();
  245.         g.setColor(Color.white);
  246.         g.fillOval(oldPoint.x,oldPoint.y,20,20);
  247.  
  248.         }
  249.  
  250.         oldPoint = currentPoint;//set the oldPoint to the most recent coordinates
  251.     }
  252.  
  253.  
  254.     public boolean keyDown(Event e, int key) {
  255.  
  256.         String s = String.valueOf((char)key);
  257.  
  258.         drawingArea.getGraphics().drawString(s, oldPoint.x, oldPoint.y);
  259.         System.out.println("this is s: ");
  260.  
  261.            Font font = new Font("Arial", Font.BOLD, 12);
  262.         fm = getFontMetrics(font);
  263.  
  264.     return(record(oldPoint.x + fm.stringWidth(s), oldPoint.y));
  265. }
  266.  
  267.  
  268.     protected boolean record(int x, int y) {
  269.     oldPoint.x = x;
  270.     oldPoint.y = y;
  271.     return(true);
  272.  
  273.     }
  274.  
  275.     public void paint(Graphics g)
  276.     {
  277.  
  278.     } 
  279.  
  280.  
  281.     void drawRect(Graphics g) {
  282.  
  283.                int x, y;  // Top left corner of the rectangle.
  284.                int w, h;  // Width and height of the rectangle.
  285.                    // x,y,w,h must be computed from the coordinates
  286.                    // of the two corner points.
  287.                if (mousex > startx) {
  288.                   x = startx;
  289.                   w = mousex - startx;
  290.                }
  291.                else {
  292.                   x = mousex;
  293.                   w = startx - mousex;
  294.                }
  295.                if (mousey > starty) {
  296.                   y = starty;
  297.                   h = mousey - starty;
  298.                }
  299.                else {
  300.                   y = mousey;
  301.                   h = starty - mousey;
  302.                }
  303.                g.setColor(Color.white);
  304.                g.fillRect(x, y, w, h);
  305.                g.setColor(Color.black);
  306.                g.drawRect(x, y, w-1, h-1);
  307.             }
  308.        // end drawRect()
  309.  
  310.        void drawOval(Graphics g) {
  311.  
  312.                int x, y;  // Top left corner of the rectangle.
  313.                int w, h;  // Width and height of the rectangle.
  314.                    // x,y,w,h must be computed from the coordinates
  315.                    // of the two corner points.
  316.                if (mousex > startx) {
  317.                   x = startx;
  318.                   w = mousex - startx;
  319.                }
  320.                else {
  321.                   x = mousex;
  322.                   w = startx - mousex;
  323.                }
  324.                if (mousey > starty) {
  325.                   y = starty;
  326.                   h = mousey - starty;
  327.                }
  328.                else {
  329.                   y = mousey;
  330.                   h = starty - mousey;
  331.                }
  332.                g.setColor(Color.white);
  333.                g.fillOval(x, y, w, h);
  334.                g.setColor(Color.black);
  335.                g.drawOval(x, y, w-1, h-1);
  336.             }
  337.        // end drawRect()
  338.  
  339.  
  340.     //abstract methods promised by the MouseListener and MouseMotionListener interfaces
  341.     public void mouseEntered(MouseEvent e){}
  342.     public void mouseExited(MouseEvent e){}
  343.     public void mouseReleased(MouseEvent e){}
  344.     public void mouseClicked(MouseEvent e){}
  345.     public void mouseMoved(MouseEvent e){}
  346.     public void keyReleased(KeyEvent e){}
  347.     public void keyPressed(KeyEvent e){}
  348.  
  349.  
  350. }
Jun 17 '07 #1
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
Hi, Im having a problem with typing on the JPanel. The thing is, that it works perfectly fine when this line " content.add(jp, BorderLayout.NORTH); " is taken out (its the buttons panel) but when its added back in, i simply cannot type! HELP!!!!



Codes are as follow:


1.) You forgot the code tags
2.) There exists in Java a switch statement.
3.) What are you trying to achieve with this program? Typing is only possible on components that support typing.
Jun 18 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.