2014-05-23 48 views
0

我正在製作一個根據用戶輸入繪製不同形狀的程序。當光標被按下並拖動時,繪製形狀。但由於某種原因,每當光標被按下時,形狀開始在光標下方約25個像素處。任何人都知道爲什麼以及如何解決它?鼠標聽衆沒有給出正確的分數

我的代碼

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


public class DrawShapes extends JFrame 
{ 
    private JLabel display; 
    private Point point1, point2; 
    private ShapeDrawer drawer; 

public DrawShapes() 
{ 
    Container contain = getContentPane(); 

    point1 = null; 
    point2 = null; 

    //Setup JLabel and add it to container 
    display = new JLabel("Line"); 
    contain.add(display, BorderLayout.SOUTH); 

    //Setup JPanel and add it to top of container 
    drawer = new ShapeDrawer(); 
    drawer.setPreferredSize(new Dimension(500,500)); 
    contain.add(drawer, BorderLayout.NORTH); 

    //Setup listeners 
    ShapeListener listener = new ShapeListener(); 
    addKeyListener(listener); 
    addMouseListener(listener); 
    addMouseMotionListener(listener); 

    setTitle("Press l for line, o for oval, r for rectangle"); 
    pack(); 
    setVisible(true); 
} 

//Class for drawing shapes 
private class ShapeDrawer extends JPanel 
{ 
    public void paintComponent(Graphics paint) 
    { 
     super.paintComponent(paint); 

     Graphics2D g2 = (Graphics2D) paint; 
     setBackground(Color.white); 
     g2.setColor(Color.black); 

     //Draw different shapes based on user input 
     if(point1 != null && point2 != null) 
     { 
      if(display.getText().equals("Circle")) 
      { 
       g2.fillOval(point1.x - 6, point1.y - 6, 
         6* 2, 6 * 2); 
      } 
      else if(display.getText().equals("Oval")) 
      { 
       g2.drawOval(point1.x,point1.y,point2.x,point2.y); 
      } 
      else if(display.getText().equals("Line")) 
      { 
       g2.drawLine(point1.x, point1.y, point2.x, point2.y); 
      } 
      else if(display.getText().equals("Rectangle")) 
      { 
       g2.drawRect(point1.x, point1.y, point2.x, point2.y); 
      } 
     } 
    } 
} 

private class ShapeListener implements MouseListener, MouseMotionListener ,KeyListener 
{ 
    //Change the label if something else is pressed 
    public void keyPressed(KeyEvent event) 
    { 
     if(event.getKeyText(event.getKeyCode()).equals("C")) 
      display.setText("Circle"); 
     else if(event.getKeyText(event.getKeyCode()).equals("O")) 
      display.setText("Oval"); 
     else if(event.getKeyText(event.getKeyCode()).equals("R")) 
      display.setText("Rectangle"); 
     else if(event.getKeyText(event.getKeyCode()).equals("L")) 
      display.setText("Line"); 
    }  

    public void keyReleased(KeyEvent event){}; 
    public void keyTyped(KeyEvent event){}; 

    //Get first point where mouse is pressed 
    public void mousePressed(MouseEvent event) 
    { 
     point1 = event.getPoint(); 

    } 

    //Get the point while mouse is being pressed 
    public void mouseDragged(MouseEvent event) 
    { 
     point2 = event.getPoint(); 
     drawer.repaint(); 

    } 

    public void mouseReleased(MouseEvent event) {} 
    public void mouseEntered(MouseEvent event) {} 
    public void mouseExited(MouseEvent event) {} 
    public void mouseClicked(MouseEvent event) {} 
    public void mouseMoved(MouseEvent event) {} 



} 
} 

和主要方法

import javax.swing.JFrame; 
class ShapesViewer 
{ 
public static void main(String[] args) 
{ 
    DrawShapes shapes = new DrawShapes(); 

    shapes.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
} 
} 

回答

2

您需要的的MouseListener添加到圖紙的JPanel沒有到JFrame。

更改此:

ShapeListener listener = new ShapeListener(); 
addMouseListener(listener); 
addMouseMotionListener(listener); 

這樣:

drawer.ShapeListener listener = new ShapeListener(); 
drawer.addMouseListener(listener); 
drawer.addMouseMotionListener(listener); 

由於在JFrame的點的位置是25點,因爲菜單欄的上面的JPanel。

+0

的感謝其現在的工作。 – user3667275

+0

@ user3667275:不客氣! –

1

不要將偵聽器添加到框架,直接將它們添加到ShapeDrawerMouseEvent與生成事件的組件的來源是上下文相關的。

框架的左上角將爲0x0,但由於框架的邊框,框架內容將被框架內的若干像素偏移。

例如...

Example

考慮使用KeyListenerkey bindings API