Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback, please email oracle-forums_moderators_us@oracle.com

Can someone optimise the following code ?

843805Aug 2 2006 — edited Aug 2 2006
Hi,
Recently i wrote a (simple) piece of code to test a functionality in JPanel. It was ,to
draw a rectangle as the user drags the mouse in the Panel. Just like in file browser , to select
multiple files by dragging the mouse.

In this, I see the box flickering so much, whenever
the user drags the mouse.As the box is getting repainted with every co-ordinates as it moved.
I dont have any idea to optimise the code and to stop fickering ..
as far as i can think of.

can someone help me with this ?

Well , when i tried the same thing in Ubuntu linux , i couldnt the box at all !!!
That was totally strange !!!! . Any solution for this ???
import java.awt.event.MouseMotionAdapter;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import java.awt.Point;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Graphics;
import java.awt.AlphaComposite;

public class Dragtest extends JFrame{
    
    boolean blPaintRect = false;
    Polygon d = new Polygon();
    Point srcPoint = null;
    Point destPoint = null;
    boolean blchecked;
    AlphaComposite alcomp = AlphaComposite.getInstance(
                    AlphaComposite.SRC_OVER, 0.4f);

    public Dragtest() {
        getContentPane().setLayout(new java.awt.FlowLayout());
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        
        pack();

        setSize(300, 300);
        setLocation(300, 100);
        setTitle("Java Speed Test");
        setVisible(true);

        addMouseMotionListener(new MouseMotionAdapter(){
            public void mouseDragged(MouseEvent e){
                // blPaintRect is made true and repaint is called , to draw the box
                blPaintRect = true;
                if(srcPoint == null){
                    srcPoint = e.getPoint();
                    destPoint = e.getPoint();
                }
                else{
                    destPoint = e.getPoint();
                }
                // As the user drags the mouse , the new co-ordinates are stored
                // in destPoint variable and the intial point in
                // srcPoint variable
                
                d.reset();
                d.addPoint(srcPoint.x, srcPoint.y);
                d.addPoint(destPoint.x, srcPoint.y);
                d.addPoint(destPoint.x, destPoint.y);
                d.addPoint(srcPoint.x, destPoint.y);
                repaint();
            }
        } );

        addMouseListener(new MouseAdapter(){
            public void mouseReleased(MouseEvent e) {
                blPaintRect = false;
                srcPoint = null;
                repaint();
            }
        });
        
    }

    public void paint(Graphics g){
        if(blPaintRect){
            super.paint(g);
            Graphics2D  g2d = (Graphics2D) g;            
            g2d.setComposite(alcomp);
            // One optimisation is that ,
            // drawPolygon  be skipped , and only fillPolygon can be used 
            g2d.drawPolygon(d);
            g2d.fillPolygon(d);
        }
        else{
            super.paint(g);
        }
    }

    public static void main(String args[]) {
        new Dragtest().setVisible(true);
    }
    
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 30 2006
Added on Aug 2 2006
4 comments
286 views