Results 1 to 10 of 10
  1. #1
    Drezir is offline Member
    Join Date
    Nov 2014
    Posts
    8
    Rep Power
    0

    Question Dragging mosue + repaint() = flashing line

    Hello,
    i have got a problem with drawing line when dragging mouse by using paintComponent() & repaint() method. Problem is when i dragg a mouse then line is blinking and when i do not move with mouse but button is pressed then i can not see anything. After releasing mouse button the coordinates are saved as a new object to an ArrayList. Right after drawing shapes from ArrayList i can see what i drew before. I would be greatful for your tips. Thank you.
    Pastebin: http://pastebin.com/defa1cEY


    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.util.ArrayList;

    public class Panel extends javax.swing.JPanel {

    private ArrayList<Objekt> tvary = new ArrayList(); //list of shapes
    private Point pocatek, konec; //start & end point for drawing
    private byte tvar = 0; //0..line, 1...rect, 2...oval ......type of shape
    Color barva;
    boolean shift = false;

    public Panel() {
    initComponents();
    }

    @Override
    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    vykresliTvary(g);
    kresli();
    repaint();
    }

    private void vykresliTvary(Graphics g) { ///draw all shapes in ArrayList()
    for (Objekt tvar : tvary) {
    if (tvar instanceof Obdelnik) {
    ((Obdelnik) tvar).Kresli(g);
    } else if (tvar instanceof Kruznice) {
    ((Kruznice) tvar).Kresli(g);
    } else {
    tvar.Kresli(g);
    }
    }
    }

    public void setAktualniTvar(byte tvar) { //setting selected type of shape from Jframe radio buttons
    this.tvar = tvar;
    pocatek = null;
    konec = null;
    }

    public void setBarva(Color barva) { // setter for color
    this.barva = barva;
    }

    private void kresli() { //deciding what to draw depending on actual type
    if (pocatek != null) {
    Graphics g = getGraphics();
    g.setColor(barva);
    switch (tvar) {
    case 0:
    kresliCaru(g);
    break;
    case 1:
    kresliObdelnik(g);
    break;
    case 2:
    kresliKruznici(g);
    break;
    }
    }
    }

    private void kresliKruznici(Graphics g) { //drawing oval
    if (shift) {
    g.fillOval(Math.min(pocatek.x, konec.x), Math.min(pocatek.y, konec.y), Math.abs(pocatek.x - konec.x), Math.abs(pocatek.y - konec.y));
    } else {
    g.drawOval(Math.min(pocatek.x, konec.x), Math.min(pocatek.y, konec.y), Math.abs(pocatek.x - konec.x), Math.abs(pocatek.y - konec.y));
    }
    }

    private void kresliCaru(Graphics g) { //drawing line
    g.drawLine(pocatek.x, pocatek.y, konec.x, konec.y);
    }

    private void kresliObdelnik(Graphics g) { //drawing rectangle
    if (shift) {
    g.fillRect(Math.min(pocatek.x, konec.x), Math.min(pocatek.y, konec.y), Math.abs(pocatek.x - konec.x), Math.abs(pocatek.y - konec.y));
    } else {
    g.drawRect(Math.min(pocatek.x, konec.x), Math.min(pocatek.y, konec.y), Math.abs(pocatek.x - konec.x), Math.abs(pocatek.y - konec.y));
    }
    }

    @Override
    public Dimension getPreferredSize() {
    return new Dimension(500, 500);
    }

    private void formMousePressed(java.awt.event.MouseEvent evt) {
    pocatek = evt.getPoint();
    konec = evt.getPoint();
    }

    private void formMouseDragged(java.awt.event.MouseEvent evt) {
    konec = evt.getPoint();
    shift = evt.isShiftDown();
    kresli();
    }
    private void uloz(byte tvar) { //saving shape into ArrayList
    int sirka, vyska;
    switch (tvar) {
    case 0:
    tvary.add(new Objekt(barva, pocatek, konec));
    break;
    case 1:
    sirka = Math.abs(konec.x - pocatek.x);
    vyska = Math.abs(konec.y - pocatek.y);
    pocatek.x = Math.min(pocatek.x, konec.x);
    pocatek.y = Math.min(pocatek.y, konec.y);
    tvary.add(new Obdelnik(barva,shift, pocatek, sirka, vyska));
    break;
    case 2:
    sirka = Math.abs(konec.x - pocatek.x);
    vyska = Math.abs(konec.y - pocatek.y);
    pocatek.x = Math.min(pocatek.x, konec.x);
    pocatek.y = Math.min(pocatek.y, konec.y);
    tvary.add(new Kruznice(barva,shift, pocatek, sirka, vyska));
    break;
    }
    }
    private void formMouseReleased(java.awt.event.MouseEvent evt) {
    uloz(tvar);
    }
    }
    Last edited by Drezir; 11-03-2014 at 03:51 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Dragging mosue + repaint() = flashing line

    I have only scanned your code but you should never call repaint from within a paintComponent() method. Your should call repaint when it is time to paint. That means when you have updated something which needs repainting. So if you move your mouse, the appropriate mouse listener method is called and you grab the coordinates. From that method you should call repaint.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Dragging mosue + repaint() = flashing line

    Just to back that up: the above situation is basically an infinite loop.

    paint() -> paintComponent() -> repaint() -> paintComponent() -> repaint() -> etc.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Dragging mosue + repaint() = flashing line

    Quote Originally Posted by gimbal2 View Post
    Just to back that up: the above situation is basically an infinite loop.
    Right, and the only thing that saves the application from locking up is the coalescing of pending paint events.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Drezir is offline Member
    Join Date
    Nov 2014
    Posts
    8
    Rep Power
    0

    Default Re: Dragging mosue + repaint() = flashing line

    Quote Originally Posted by jim829 View Post
    I have only scanned your code but you should never call repaint from within a paintComponent() method. Your should call repaint when it is time to paint. That means when you have updated something which needs repainting. So if you move your mouse, the appropriate mouse listener method is called and you grab the coordinates. From that method you should call repaint.

    Regards,
    Jim
    Thank you for your tip but i have already tried put repaint() method into DragMouse listener and it did not work.

  6. #6
    Drezir is offline Member
    Join Date
    Nov 2014
    Posts
    8
    Rep Power
    0

    Default Re: Dragging mosue + repaint() = flashing line

    Quote Originally Posted by gimbal2 View Post
    Just to back that up: the above situation is basically an infinite loop.

    paint() -> paintComponent() -> repaint() -> paintComponent() -> repaint() -> etc.
    I know. Infinite loop causes flashing line. But how to avoid that? How to use repaint() method without blinking line?

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Dragging mosue + repaint() = flashing line

    Well, you need to paint after you update what ever it is to be painted. Perhaps there are other problems with your code. I recommend you submit a Short, Self Contained, Correct Example to demonstrate the problem.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Drezir is offline Member
    Join Date
    Nov 2014
    Posts
    8
    Rep Power
    0

    Default Re: Dragging mosue + repaint() = flashing line

    Quote Originally Posted by jim829 View Post
    Well, you need to paint after you update what ever it is to be painted. Perhaps there are other problems with your code. I recommend you submit a Short, Self Contained, Correct Example to demonstrate the problem.

    Regards,
    Jim
    I have solved this already. Thank guys for your help. Problem was that paintComponent() has drawn into JPanel directly (on each mouse move i have called repaint() => blinking). Now, each mouse move means creating new Line/Rect/Oval object and adding it to the list (not drawing it directly - drawing only from paintComponent()) So basically it works like save objects first & draw later. My old bad solution was draw first & save objects later

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Dragging mosue + repaint() = flashing line

    Glad you solved it!

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Dragging mosue + repaint() = flashing line

    Good for you, and next time you have a question, don't forget the code tags.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. repaint without flashing
    By natdizzle in forum AWT / Swing
    Replies: 5
    Last Post: 01-15-2011, 05:06 PM
  2. Flashing JFrame
    By ilyvatar in forum AWT / Swing
    Replies: 3
    Last Post: 12-01-2010, 09:52 AM
  3. MouseListener - Flashing label
    By Adomini in forum New To Java
    Replies: 4
    Last Post: 11-29-2010, 09:31 PM
  4. drawing Line by dragging mouse !!!
    By h9h in forum Java 2D
    Replies: 14
    Last Post: 10-23-2009, 05:10 AM
  5. Flashing
    By Supamagier in forum Java 2D
    Replies: 6
    Last Post: 04-29-2009, 03:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •