How to draw two-dimensional graphics in Java?

I have 2 different lists. Each of them contains pairs of x and y values ​​(they have both positive and negative values). How can I draw them on a two-dimensional axis? I want to set pointsfor each value, and they will be blue for the first list and red for the second list.

Type of my lists:

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.

However, I need how to draw two-dimensional graphics in Java (a desktop application) and draw dots wherever I want, more important is to improve the code for my variables.

PS:

I want more and more simplethis kind of graphic: enter image description here

Sort of:

enter image description here

+3
source share
3 answers

, Swing , :

public class JImagePanelExample extends JPanel {

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

    // Let 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, BufferedImage Graphics2D.

+1

There is a Java 2D API: http://java.sun.com/products/java-media/2D/ , and many chart libraries are easy to find using web searches.

0
source

All Articles