0

Hi I am trying to create parabolic curves using lines in awt java but previous lines are getting removed as soon as some new lines are drawn.

I am using this code in paint method

static int x1 = 0;
static int y1 = 300;
static int x2 = 300;
static int y2 = 300;

@Override
protected void paintComponent(Graphics g) {
    //super.paintComponent(g);
    g.setColor(Color.RED);
    for(int i = 0; i < 15;i++,x1 += 4, y2 -= 2) {
        g.drawLine(x2, y2, x1, y1);
    }
    //repaint();
}

but if i iterate loop for 10 times then only its drawing lines correctly.

My ouputenter image description here

what I want to acheive is draw parabolic curves in each quadrant like this

enter image description here

ignore the red text in second image. Ref - https://www.mrsmilewski.com/parabolic-curve.html Any help appreciated.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Bruce_Wayne
  • 1,564
  • 3
  • 18
  • 41
  • 2
    The the line information in some kind of structure (like `Line2D.Double`), when you add new lines, add them to a `List` of some kind. When `paintComponent` I called, loop over this `List` and paint the lines (`Line2D` is paintable by `Graphics2D` via its `draw(Shape)` method ;)). Painting in Swing is destructive, you are expected to, when ever a paint pass occurs, completely repaint the current state of the component – MadProgrammer Jul 12 '18 at 03:28
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) But.. `//super.paintComponent(g);` ..that is likely to cause problems. It seems the code is 'fighting' the way to do custom painting in Swing / AWT. The correct way is to draw ***everything* that needs drawing, every time the paint method is called.** Before that, call the super method to erase any previous drawing and paint (again) whatever needs drawing. The only way I know of to 'build up' a drawing is to paint it to a `BufferedImage` which can then be displayed in a `JLabel`. – Andrew Thompson Jul 12 '18 at 13:17

0 Answers0