2013-01-03 72 views
1

我必須在HoltSoft的Ready to Program中使用Console類。我不應該使用揮杆,所以如果我不能沒有揮杆的話,請不要忽視這一點。爲什麼點擊時不顯示圓圈?

//imports 
import java.awt.*; 
import java.awt.event.*; 
import hsa.*; 

public class DrawLines extends Panel implements MouseListener, MouseMotionListener 
{ 
    Console c; 
    int startX, startY, prevX, prevY; //mouse coordinates 
    private boolean dragging; //whether or not the mouse is being dragged 
    MouseEvent e; 
    public DrawLines() 
    { 
     c = new Console(); //creates console window 
     addMouseListener (this); //detects press/release 
     addMouseMotionListener (this);//detects dragging 
    } 


    public void mousePressed (MouseEvent e) 
    { 
     while (!dragging) 
     { 
      try 
      { 
       startX = e.getX();//get the 
       startY = e.getY();//original co-ordinates 
       dragging = true; 
      } 
      catch (NullPointerException q) //because I kept getting this error 
      { 
      } 
     } 
    } 


    public void mouseDragged (MouseEvent e) 
    { 
     while (dragging) 
     { 
      try 
      { 
       int x = e.getX(); //gets and 
       int y = e.getY(); //updates 
       prevX = x;   //the mouse 
       prevY = y;   //coordinates 
      } 
      catch (NullPointerException q)//because I kept getting this error 
      { 
      } 
     } 
    } 


    public void mouseReleased (MouseEvent e) 
    { 
     dragging = false; //stopped dragging 
    } 


    public void drawTheLine() 
    { 
     mousePressed (e); 
     mouseDragged (e); 
     c.setColor (Color.black); 
     c.fillOval (prevX, prevY, 50, 50); //draws a circle where the mouse is 
     mouseReleased (e); 
    } 


    public void mouseMoved (MouseEvent e){} 
    public void mouseEntered (MouseEvent e){} 
    public void mouseExited (MouseEvent e){} 
    public void mouseClicked (MouseEvent e){} 

    public static void main (String[] args) 
    { 
     DrawLines a = new DrawLines(); 
     a.drawTheLine(); 
    } 
} 

我一直試圖在控制檯中使用MouseListener和MouseMotionListener。起初,該程序不斷給我錯誤,所以我添加了try/catch結構。現在它不會崩潰,但屏幕上不顯示任何內容。爲什麼?幫幫我?

如果我不應該使用try/catch來忽略它,我該怎麼辦?

我不允許在此程序中使用Console()以外的任何其他功能。這是一門課程任務。

+7

當你得到一個異常,通常意味着什麼是錯的。僅僅忽視它(帶有空白的捕獲聲明)將不能解決潛在的問題。 – assylias

+0

林不知道,什麼是控制檯()?你應該使用'JPanel'並重載'paintComponent' –

+0

我覺得while循環的使用是相當危險的......這個程序是否完成?在我看來,它有很多可能無限期掛起,如mouseDragged(..) –

回答

2

看看這個:

public void drawTheLine() 
{ 
    while (true) 
    { 
     mousePressed (e); 
     mouseDragged (e); 
     c.setColor (Color.black); 
     c.fillOval (prevX, prevY, 50, 50); //draws a circle where the mouse is 
     mouseReleased (e); 
    } 
} 

參數 「E」 你傳遞爲空。在此聲明:

public class DrawLines extends Panel 
    implements MouseListener, MouseMotionListener 
{ 
    MouseEvent e; // IT IS NEVER SET TO ANYTHING! IT IS NULL!!! 

某處在你的構造函數,你應該這樣做,所以它不再是空:

e = (something); 
+0

我可以設置什麼?是否有MouseEvent變量的值? – ijustdontgetit

+0

@helpmeimdumb根本不應該使用MouseEvent對象。從我所看到的,它看起來像一個java.awt.Point2D將適合您的需求。 – Aaron

+0

「看看這個......」這是什麼意思?鑑於他們已經這樣做了,可能意味着他們不知道更好 – MadProgrammer

2

Swing是一個事件驅動的系統,是一個單線程系統。

這意味着您的應用程序「等待」事件發生(Event Dispatching Thread將爲您處理),並且阻止EDT的任何人(例如循環,長時間運行的進程或阻止IO)都會阻止你的應用程序接收到這些事件的通知,使你的應用程序無法運行。

因此,如果我們看看這個...

while (true) 
    { 
     mousePressed (e); 
     mouseDragged (e); 
     c.setColor (Color.black); 
     c.fillOval (prevX, prevY, 50, 50); 
     mouseReleased (e); 
    } 
} 

這表明...一,你不明白事件是如何在Swing和兩個產生,在美國東部時間的實際工作原理。

與一些UI框架不同,您不需要實現事件循環,這由Swing照顧。阻斷EDT這樣,可以防止它處理事件

相反,刪除drawLineMethod,因爲它是爲你做什麼絕對與類似取代你的主要方法...

public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      try { 
       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
      } catch (Exception ex) { 
      } 

      JFrame frame = new JFrame("Test"); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.setLayout(new BorderLayout()); 
      frame.add(new DrawLines()); 
      // I prefer pack, but you've not specified a preferred size for your panel... 
      //frame.pack(); 
      frame.setSize(400, 400); 
      frame.setLocationRelativeTo(null); 
      frame.setVisible(true); 
     } 
    }); 
} 

現在。我不知道什麼Console類或具體,但在你的鼠標事件的方法,你將需要更新,以便它可以更新它的輸出...

已更新,例如

enter image description here

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (Exception ex) { 
       } 

       JFrame frame = new JFrame("Test"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new DrawPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class DrawPane extends JPanel { 

     private Point center; 
     private int radius; 

     public DrawPane() { 
      MouseAdapter handler = new MouseAdapter() { 

       @Override 
       public void mousePressed(MouseEvent e) { 
        center = e.getPoint(); 
        radius = 0; 
        repaint(); 
       } 

       @Override 
       public void mouseDragged(MouseEvent e) { 
        int width = Math.max(e.getX(), center.x) - Math.min(e.getX(), center.x); 
        int height = Math.max(e.getY(), center.y) - Math.min(e.getY(), center.y); 
        radius = Math.max(width, height); 
        repaint(); 
       } 

      }; 
      addMouseListener(handler); 
      addMouseMotionListener(handler); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(400, 400); 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      if (center != null) { 
       g.setColor(Color.RED); 
       g.fillOval(center.x - 2, center.y - 2, 4, 4); 

       g.drawOval(center.x - (radius/2), center.y - (radius/2), radius, radius); 

      } 
     } 
    } 
} 

我建議你花點時間讀一讀......

更新與純AWT版本

正如有人向我指出的是,OP使用,而不是搖擺,爲什麼,因爲他們似乎能夠AWT ...

public class DrawCircleAWT { 

    public static void main(String[] args) { 
     new DrawCircleAWT(); 
    } 

    public DrawCircleAWT() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       Frame frame = new Frame("Testing"); 
       frame.addWindowListener(new WindowAdapter() { 
        @Override 
        public void windowClosing(WindowEvent e) { 
         System.exit(0); 
        } 
       }); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new DrawPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class DrawPane extends Panel { 

     private Point center; 
     private int radius; 

     public DrawPane() { 
      MouseAdapter handler = new MouseAdapter() { 
       @Override 
       public void mousePressed(MouseEvent e) { 
        center = e.getPoint(); 
        radius = 0; 
        repaint(); 
       } 

       @Override 
       public void mouseDragged(MouseEvent e) { 
        int width = Math.max(e.getX(), center.x) - Math.min(e.getX(), center.x); 
        int height = Math.max(e.getY(), center.y) - Math.min(e.getY(), center.y); 
        radius = Math.max(width, height); 
        repaint(); 
       } 
      }; 
      addMouseListener(handler); 
      addMouseMotionListener(handler); 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(400, 400); 
     } 

     @Override 
     public void paint(Graphics g) { 
      super.paint(g); 

      if (center != null) { 
       g.setColor(Color.RED); 
       g.fillOval(center.x - 2, center.y - 2, 4, 4); 

       g.drawOval(center.x - (radius/2), center.y - (radius/2), radius, radius); 

      } 
     } 
    } 
} 
+0

我有沒有想念什麼。你對這個問題的編輯?代碼的導入是純AWT,它擴展了'Panel'而不是'JPanel'。我認爲[tag:swing]應該是[tag:awt]。但是,對於OP的問題 - 「爲什麼要使用AWT?」。 –

+0

錯過了使用AWT的OP,但是(其他的paintComponent),其餘的都是大致相同的方法 – MadProgrammer