Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By jim829

Thread: Drawing connected lines with fast mouse motions. Do I need buffering?

  1. #1
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    136
    Rep Power
    0

    Default Drawing connected lines with fast mouse motions. Do I need buffering?

    Hello, I am trying to do a dot connection programming interface. The problem is that the repaint seems slow or I don`t use it right. When I move the mouse too fast it draws just points not connected line. This is my code:
    Java Code:
    package com.ilian.Quiz;
    
    
    import com.ilian.Utils.CustomAlgorithms; //for the cusom parsers 
    import com.ilian.Utils.CustomParser;
    
    import javax.swing.JComponent;
    import javax.swing.SwingUtilities;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.BorderFactory;
    import javax.swing.border.BevelBorder;
    
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics; 
    import java.awt.Polygon;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.MouseMotionAdapter; 
    
    import java.awt.Rectangle;
    /*
     * Mitata: Т.е. отсечката с т. А (2,3) и т. В (11,4) ще я представиш като y=((4-3)/(11-2))x + y0 (където правата пресича ординатата).
     */
    public class QCanvas extends JPanel {
    	// draw multiple squares on the canvas
    	private int[] x;
    	private int[] y;
    	private QCanvas self = this;
    	private static final int WIDTH = 20;
    	boolean mouseIsMoving = false;
    	int lineX;
    	int lineY;
    	/* private constructor */
    	
    	private void populateShapes() {
    		
    		
    		
    	}
    	private void repaintShapes(int x, int y) {
    		this.lineX = x; this.lineY = y;
    		this.repaint(lineX, lineY, 5, 5);
    	}
    	private QCanvas() { };
    	
    	private QCanvas(Builder b) {
    		this.setPreferredSize(new Dimension(50,50));
    		this.x = b.xcoords;
    		this.y = b.ycoords;
    		this.rects = b.rects;
    		this.addMouseMotionListener(new MouseMotionListener() {
    
    			@Override
    			public void mouseDragged(MouseEvent e) {
    				// TODO Auto-generated method stub
    				
    			}
    
    			@Override
    			public void mouseMoved(MouseEvent e) {
    				// TODO Auto-generated method stub
    				mouseIsMoving = true;
    				repaintShapes( e.getX() ,
    							e.getY() );
    			}
    
    						
    		});
    	}
    // Inner class for rectangles 
    	private Rectangle[] rects ;
    	/* BUILDER ----------------------------------------- */
    	public static class Builder {
    		private int[] xcoords;
    		private int[] ycoords;
    		private Rectangle[] rects;
    		boolean isCanvasActive = false;
    		Builder(boolean a) { 
    			/* unused for now */
    			isCanvasActive = a;
    		};
    		QCanvas build() {
    			if ( !isCanvasActive ) return null;
    			else return new QCanvas(this);
    		}
    		Builder setRectangles() {
    			this.rects = new Rectangle[xcoords.length];
    			for (int i=0; i < rects.length; i++ ) {
    				rects[i] = new Rectangle(xcoords[i], ycoords[i], 20,20);
    				System.out.println("Rect:"+rects[i].x);
    			}
    			return this;
    		}
    		Builder setXYCoords(String[] s) {
    			if (s.length != 2) return null; 
    			try {
    				//BUUUUUUUG!!!!
    				xcoords =  CustomParser.toIntegerArray(
    						CustomParser.splitBySymbol(',', s[0]));
    				ycoords = CustomParser.toIntegerArray(
    						CustomParser.splitBySymbol(',', s[1]));
    				System.out.println("XCOORDS:"+xcoords.toString());
    				System.out.println("YCOORDS:"+ycoords.toString());
    				return this;
    			} catch (Exception ex) {
    				System.out.println("Exception in QCanvas Builder setXYCoords()");
    				
    				return null;
    			}	
    		}
    		
    		Builder setXCoords(String s) {
    			xcoords = CustomParser.toIntegerArray(
    					CustomParser.splitBySymbol(',', s));
    			return this;
    		}
    		Builder setYCoords(String s) {
    			ycoords = CustomParser.toIntegerArray(
    					CustomParser.splitBySymbol(',', s));
    		
    			return this;
    		}
    		
    	}
    	/* ------------------------------------------------- */
    	
    	
    	protected void paintComponent(Graphics g) {
    		super.paintComponent(g); 
    		//add he graph custom logics below
    		g.setColor(Color.red);
    		if ( mouseIsMoving ) {
    			g.setColor(Color.black) ;
    			g.fillRect(lineX, lineY, 5, 5);
    		}
    		for (int i=0; i < rects.length; i++)
    			g.fillRect(rects[i].x, rects[i].y, 20, 20);
    			
    	}
    	
    	
    	/* UNIT TESTING ------------------------------ */
    	
    	  public static void main(String[] args) {
    	        SwingUtilities.invokeLater(new Runnable() {
    	            public void run() {
    	                createAndShowGUI(); 
    	            }
    	        });
    	    }
    	 /////////////////////////////////////////////////////////////////////////////////
    	  /* 06.07.2014 - test passed -ok workign */
    	    private static void createAndShowGUI() {
    	        JFrame f = new JFrame("QCanvas test");
    	        JPanel jp = new JPanel();
    	       
    	        String[] answers = {"1,10,20,30,90", "1,10,20,30,90"};
    	        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    	        jp.add(new QCanvas.Builder(true).setXCoords(answers[0])
    	        		.setYCoords(answers[1])
    	        		.setRectangles().build());
    	        f.add(jp);
    	        f.setPreferredSize(new Dimension(200,200));
    	        f.pack();
    	        f.setVisible(true);
    	    } 
    	}
    And this is the result ( assuming I am in my main application ) in the screenshot. THe dot`s are not connected to each other when I move the mouse fast. Shall I represent the drawing area with WxH array then flag/unflag the coords then repaint? Kind of 2x buffering or there is simpler solution?
    Attached Thumbnails Attached Thumbnails Drawing connected lines with fast mouse motions. Do I need buffering?-screenshot-07112014-04-51-33-pm.png  

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,424
    Rep Power
    5

    Default Re: Drawing connected lines with fast mouse motions. Do I need buffering?

    As you move the mouse, store the points in an array or List implementation (e.g. ArrayList). In your paint routine, instead of drawing the points, draw a line between successive points.

    Regards,
    Jim
    heatblazer likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,424
    Rep Power
    5

    Default Re: Drawing connected lines with fast mouse motions. Do I need buffering?

    Also, I looked at your code more closely. The graphics context passed is Graphics. You can cast that to a Graphics2D context and have more methods available. One of them is the setStroke(Stroke s) method. It lets you adjust the line width and end cap styles. So if you set the stroke to the line width, you don't have to artificially change the size with rectangles, etc. Not certain if this helps but I leave it up to you to decide if it is useful. Look at the JDK Stroke interface documentation to see which implementation you can use.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. My program won't recognize the mouse's motions~
    By jim.f.hurley in forum New To Java
    Replies: 1
    Last Post: 09-20-2012, 09:26 PM
  2. Problems drawing lines on a Graph
    By rhexis in forum New To Java
    Replies: 0
    Last Post: 02-17-2012, 05:24 PM
  3. Double-buffering and mouse=event help please
    By Parkournerd in forum New To Java
    Replies: 38
    Last Post: 01-13-2012, 01:24 PM
  4. Drawing Lines using looping statements
    By trebor11584 in forum New To Java
    Replies: 13
    Last Post: 07-06-2011, 12:00 PM
  5. Demonstration of drawing lines in SWT
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 06-28-2008, 09:27 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
  •