2011-05-27 79 views
3

我有2個不同的列表。他們每個人擁有x和y值對(他們有正面和負面的價值)。我如何在2D軸上繪製它們?我想把points爲每個值,他們將藍色的第一個列表和紅色的第二個列表。如何在Java中繪製二維圖形?

我的清單類型:

List<List<Double>> 

List<Double> inside of List<...> has 2 variables, first of it for x value and the second one is for y value. 

不過只是我需要怎麼畫在爪哇(桌面應用程序)的二維圖形,並把點的地方我想,提高了代碼爲我的變量是不那麼重要了。

PS:

我想要的那種圖形的more and more simpleenter image description here

喜歡的東西:

enter image description here

回答

1

假設你正在使用的Swing用面板,可以使用以下內容:

public class JImagePanelExample extends JPanel { 

    private BufferedImage image; 
    private Graphics2D drawingBoard; 
    private int x, y; // Image position in the panel 

    // Let's assume image is a chart and you need to draw lines 
    public JImagePanelExample(BufferedImage image, int x, int y) { 

     super(); 
     this.image = image; 

     // Retrieving a mean to draw lines 
     drawingBoard = image.createGraphics(); 

     // Draw what you need to draw (see other methods too) 
     drawingBoard.drawLine(0, 10, 35, 55); 

    } 

    // Called by Swing to draw the image in the panel 
    @Override 
    protected void paintComponent(Graphics g) { 
     super.paintComponent(g); 
     g.drawImage(image, x, y, null); 
    } 

} 

如果您不想使用Swing,而您只需要繪製2D,則只需關注BufferedImageGraphics2D