Question

I want a line graph code in which I can change the x,y coordinate value and using the points, I want to plot a line graph. Please help me.

public class PaintExample
{
    public static void main(String args[])
    {
        JFrame  f;
        f=new PaintExampleFrame();
        f.setVisible(true);
    }


}
class PaintExampleFrame extends JFrame
{

PaintExampleFrame()
{
    System.out.println("Yay");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("Paint Attempts");
    setSize(300,200);
    setLocation(new Point(500,500));
    setVisible(true);
    setBackground(Color.black);

}

public void paint(Graphics g)
        {
            g.setColor(Color.red);
            g.drawLine(0,0,200,200);
            g.setColor(Color.blue);
            g.drawLine(200,200,70,133);
            g.setColor(Color.orange);
            g.drawLine(70,133,400,15);
    }


}

The above code is not working for me. Please post some new code a/c to my requirement.

Was it helpful?

Solution

Take a look at Custom Painting Approaches.

It shows how to draw a "colored Rectangle" from an object in an ArrayList. You should be able to modify the code to draw a "colored Line" from an object in an ArrayList.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top