简体   繁体   中英

Code for line graph using swing, containing x,y points and plotting them

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM