2012-04-16 68 views
1

如下面的代碼所示,我從數據庫中獲取了線條的x和y值。然後將它們存儲在一個數組x中。之後,我試圖在框架上繪製這條線,但它沒有被繪製。我如何在框架上畫線?爲什麼我無法在JFrame上繪製線條

public class TestFrame{ 
    static JFrame test; 
    public static void main(String ar[]){ 
     test=new JFrame("Test"); 
     JButton openLine=new JButton(new AbstractAction("Open Line"){ 

      public void actionPerformed(ActionEvent e) { 
       String lineId=JOptionPane.showInputDialog("Enter Line id"); 
       ImageComponent image=new ImageComponent(); 
       image.openLine(lineId); 
      } 

     }); 
     test.add(openLine, BorderLayout.NORTH); 
     test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     test.setSize(600,600); 
     test.setVisible(true); 

    } 
    static class ImageComponent extends JComponent{ 
     static int[] x=new int[100]; 
     static ArrayList al=new ArrayList(); 
     public void openLine(String line_id){ 

          try { 

           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
           Connection con=DriverManager.getConnection("jdbc:odbc:image"); 
           Statement pstm=con.createStatement(); 
           ResultSet rs=pstm.executeQuery("select * from Line where ID= '"+line_id+"'"); 
           while(rs.next()){ 
            x[0]=rs.getInt(3); 
            x[1]=rs.getInt(4); 
            x[2]=rs.getInt(5); 
            x[3]=rs.getInt(6); 

            al.add(x); 

           } 

           repaint(); 

          } catch (Exception ex) { 
           System.out.println("Exception : "+ex); 
          } 
       } 
       public Graphics2D gd; 
       Line2D[] line=new Line2D[100]; 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
         gd=(Graphics2D)g; 

         gd.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 

          for(int i=0;i<al.size();i++){ 
           line[i]=new Line2D.Double(x[0], x[1],x[2],x[3]); 
           gd.draw(line[i]); 

          } 

         } 
       } 
} 

回答

4

下面是一個使用一個BufferedImage as a rendering surface一種方法。

在這個問題的任擇議定書是問一個小程序,但我在選擇窗格中顯示的圖像。它也適合在框架中顯示等,而不會混淆是否覆蓋paint(Graphics)paintComponent(Graphics)。 ;)

+0

對不起,但是,我沒有明白你的觀點... – Parth 2012-04-16 12:08:30

+0

Umm .. substitute'draw line of框架'與'在圖像上繪製線條(如果你願意的話,把它放在框架上)「,並有一個工作(動畫)的例子。你嘗試運行代碼嗎? – 2012-04-16 12:57:55

1

可以使用Graphics對象和覆蓋paint()方法,像這樣:

public void paint(Graphics g) { 
    Graphics2D g2 = (Graphics2D) g; 
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
     RenderingHints.VALUE_ANTIALIAS_ON); 

    g2.setPaint(Color.gray); 
    int x = 5; 
    int y = 7; 

    g2.draw(new Line2D.Double(x, y, 200, 200)); 
    g2.drawString("Line2D", x, 250); 

    } 

here

+0

我需要使用'paintComponent()'畫線,因爲還有其他一些方法在這裏沒有顯示..所以,我該怎麼做這樣的事情? – Parth 2012-04-16 10:43:02

1

送你不應該直接在JFrame。所有在JFrame主體中的呈現都是在內容窗格中完成的。菜單內容在JMenuBar中完成

+0

有沒有其他方法可以用來繪製線條? – Parth 2012-04-16 10:44:35

+0

*「你不應該直接在JFrame上」*代碼中聲明'ImageComponent extends JComponent {' - OP不直接繪製到框架上。 – 2012-04-16 11:00:16

1

一個問題是每次單擊按鈕時都會創建一個新的ImageComponent,但它不會添加到您的框架中。 另一個是你填入一個數組列表(al),但不要用它來真正畫出你的線。

這適用於我虛擬值對於x [0],X [1],X [2],X [3]:

static JFrame test; 
static ImageComponent image = new ImageComponent(); //declared as a class member 

public static void main(String ar[]) { 
    test = new JFrame("Test"); 
    JButton openLine = new JButton(new AbstractAction("Open Line") { 

     public void actionPerformed(ActionEvent e) { 
      String lineId = JOptionPane.showInputDialog("Enter Line id"); 
      image.openLine(lineId); 
     } 
    }); 
    test.add(openLine, BorderLayout.NORTH); 
    test.add(image); //ADD THE IMAGE TO THE FRAME 
    image.setVisible(true); 
    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    test.setSize(600, 600); 
    test.setVisible(true); 

} 
+0

請問您是否顯示了適用於您的整個代碼,我正在嘗試,但它不適用於我.. – Parth 2012-04-19 19:02:43