2014-01-25 137 views
0

我可以繪製水平線,但無法繪製垂直線。請幫幫我。如何在Swing中繪製垂直線

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

class Success extends JFrame{ 

    public Success(){ 
     JPanel panel=new JPanel(); 
     getContentPane().add(panel); 
     setSize(450,450); 

     JButton button =new JButton("press"); 
     panel.add(button); 
    } 

    public void paint(Graphics g) { 
     super.paint(g); // fixes the immediate problem. 
     Graphics2D g2 = (Graphics2D) g; 
     Line2D lin = new Line2D.Float(20, 40, 850, 40); 
     g2.draw(lin); 
    } 

    public static void main(String []args){ 
     Success s=new Success(); 
     s.setVisible(true); 
    } 
} 

在此先感謝。

+2

我猜你不明白圖上的X和Y軸是什麼意思? –

+2

不要覆蓋JFrame,你不會添加任何UFC tionality。不要重寫paint,尤其是JFrame,它是一個非常複雜的組件,它很容易填充paint鏈,也不是雙緩衝。改爲從JPanel擴展並重寫paontComponent。然後,您可以將其添加到JFrame的一個實例... – MadProgrammer

回答

1

我注意到有兩件事情,他們中有些人已經指出:

  1. 直接回答你的問題,這是何等的(X,Y)座標看起來像Swing組件this is what the (x, y) coordinates look like for Swing components http://i43.tinypic.com/16ga04i.png保持X座標相同的垂直線。如果您在創建線構造函數時不知道x座標在哪裏,請查看構造函數的文檔。如果您使用Eclipse,這意味着您應該將鼠標懸停在包含構造函數的代碼上。
  2. 您的線路超出了您的JFrame範圍;相反,如果您希望它從頭到尾,請使用getWidth()getHeight()方法。
  3. 每次重新繪製組件時,都不應該創建新行。相反,你應該在你的Success類的某個地方創建一條線,實現ActionListener,這樣你就可以在每一幀更新你的代碼,並且在那個更新中,調整你的線的大小,然後只留下重繪到paintComponent
  4. 在這種情況下,您不應該覆蓋JFrame,您通常不應該這樣做。
  5. 您應該覆蓋paintComponent方法,而不是paint方法。
  6. 我不認爲你是正確的雙緩衝,但我幫不了你。
  7. 覆蓋getPreferredSize如果你想控制它的大小,那麼JPanel的方法很方便,但在這種情況下它甚至不是必需的,因爲將它添加到JFrame會自動調整它的大小。

在Swing幕後出現很多東西,它可能會讓人困惑,因爲通常你必須明確地說出東西,但繼續玩這個例子,你應該安全一段時間。

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

class Success extends JPanel implements ActionListener{ 

    private final Timer timer = new Timer(20, this); // Create a timer that will go off every 20 ms 
    Line2D horizontalLine; // Declare your variables here, but don't initialize them 
    Line2D verticalLine; // That way, they can be accessed later in actionPerformed and repaint 

    // You might want to try frame.setResizable(false) if you want your frame 
    // and your panel to stay the same size. 
    private final Dimension prefPanelSize = new Dimension(450, 450); 

    public Success(){ 
     super(); // Call the constructor of JPanel, the class this subclasses. 
     JButton button =new JButton("press"); 
     this.add(button); 
     this.setSize(prefPanelSize); 
     horizontalLine = new Line2D.Float(0, 40, prefPanelSize.width, 40); 
     verticalLine = new Line2D.Float(prefPanelSize.width/2, 0, 
       prefPanelSize.width/2, prefPanelSize.height); 

     timer.start(); // Start the timer 
    } 

    @Override 
    public void paintComponent(Graphics g) { 
     super.paintComponent(g); // fixes the immediate problem. 
     Graphics2D g2 = (Graphics2D) g; 

     g2.draw(horizontalLine); 
     g2.draw(verticalLine); 
    } 

    @Override 
    public Dimension getPreferredSize() 
    { 
     return prefPanelSize; 
    } 

    public static void main(String []args){ 
     Success s = new Success(); 
     JFrame frame = new JFrame(); 
     frame.setVisible(true); 
     frame.setSize(new Dimension(450, 450)); 
     frame.add(s); 
    } 

    // This method is called ever 20 ms because of the timer. 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     int currWidth = getWidth(); 
     int currHeight = getHeight(); 

     horizontalLine.setLine(0, 40, currWidth, 40); 
     verticalLine.setLine(currWidth/2, 0, currWidth/2, currHeight); 
    } 
} 
4

保持在x座標y座標的相同的和變化值如下所示

Line2D lin = new Line2D.Float(20, 40, 20, 150);

首先兩個值是該線的起點的(X1,Y1)的值和行的最後兩個值(x2,y2)終點。現在我希望你能理解爲什麼你的代碼產生了一條水平線,以及需要做什麼來繪製垂直線。