Thread: Java AWT

Page 2 of 2 First 12
  • Jump to page:
    #16
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    15
    Rep Power
    0

    Reply


    Originally Posted by NormR
    Code:
    double[][] cds= {{xu[i],yu[i]},{xl[i],yl[i]}};
    Can you explain the issue? That code looks ok.
    when u run the code u will find that only two points are getting plotted on the applet screen. is it because of [i]???????
  2. #17
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    The code for the array you posted shows that the array has 4 values in it. I don't know how that relates to two points.
  4. #18
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    15
    Rep Power
    0
    Originally Posted by NormR
    The code for the array you posted shows that the array has 4 values in it. I don't know how that relates to two points.

    you are right they are 4 values xu xl yu yl. i guess i must use only 2 values for 2 points..???
  6. #19
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    i guess i must use only 2 values for 2 points..???
    No idea what you are asking there.
  8. #20
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    15
    Rep Power
    0

    help with this code


    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    import javax.swing.event.MouseInputAdapter;
    
    
    public class DrawCurve extends JPanel implements ActionListener {
    	ButtonManager buttonManager;
    	Path2D.Double path;
    	Point2D.Double[] points;
    	Line2D.Double[] connectors;
    	boolean showConnections= false;
    	boolean removePoint= false;
    	boolean firstTime= true;
    	final double Factor = 500.0D;
    
    
    	DrawCurve() {
    
    
    	double[][] cds={{1.00036,	0.00121},{0.93711,	0.02932},{0.87332,	0.05412},{0.80933,	
    
    0.07555},{0.74551,	0.09361},{0.68221,	0.10832},{0.61979,	0.11971},{0.55862,	
    
    0.12788},{0.49906,	0.13293},{0.44148,	0.13502},{0.38625,	0.13435},{0.33371,	
    
    0.13113},{0.28417,	0.12564},{0.23796,	0.11819},{0.19533,	0.10908},{0.15651,	
    
    0.09868},{0.12172,	0.08733},{0.09110,	0.07539},{0.06478,	0.06320},{0.04285,	
    
    0.05108},{0.01233,	0.02815},{0.00378,	0.01779},{-0.00032,	0.00838},{0.00000,	
    
    0.00000},{0.00000,         0.0000},{0.00460,	-0.00701},{0.01333,	-0.01236},{0.02610,	
    
    -0.01609},{0.04279,	-0.01825},{0.06329,	-0.01891},{0.08746,	-0.01819},{0.11515,	
    
    -0.01620},{0.14623,	-0.01308},{0.18055,	-0.00900},{0.21797,	-0.00416},{0.25836,	
    
    0.00122},{0.30161,	0.00690},{0.34760,	0.01262},{0.39623,	0.01808},{0.44738,	
    
    0.02300},{0.50094,	0.02707},{0.55681,	0.02999},{0.61485,	0.03148},{0.67491,	
    
    0.03128},{0.73685,	0.02916},{0.86563,	0.01851},{0.86563,	0.01851},{0.93208,	0.00980},
    {0.99964,	-0.00121}
    
    
    
    };
    
    	points = new Point2D.Double[cds.length];
    
    	for(int j = 0; j < cds.length; j++) { // NOTE 1 based? -1 stops short here also
    		points[j] = new Point2D.Double(Factor * cds[j][0] + Factor, Factor * cds[j][1] + Factor);
    	}
    /*Path2D.Double*/ path = new Path2D.Double();
    	//System.out.println("1path bnds=" + path.getBounds());
    	}
    
    
    //----------------------------------------------------------------
    public void actionPerformed(ActionEvent e) {
    	//System.out.println("aP e=" + e);
    	String ac = e.getActionCommand();
    	if(ac.equals("add")) {
    	connectors = new Line2D.Double[points.length-1];
    	showConnections = true;
    	repaint();
    	}
    	if(ac.equals("cancel")) {
    	showConnections = false;
    	removePoint = false;
    	repaint();
    	}
    	if(ac.equals("remove")) {
    	removePoint = true;
    	}
    }
    //--------------------------------------------------------------------
    public void addPoint(Point2D.Double p, int index) {
    	int size = points.length;
    	Point2D.Double[] temp = new Point2D.Double[size+1];
    	System.arraycopy(points, 0, temp, 0, index);
    	temp[index] = p;
    	System.arraycopy(points, index, temp, index+1, size-index);
    	points = temp;
    	buttonManager.reset();
    	showConnections = false;
    	setPath();
    	repaint();
    }
    //---------------------------------------------------------------------
    public void removePoint(Point2D.Double p) {
    	int size = points.length;
    	Point2D.Double[] temp = new Point2D.Double[size-1];
    	for(int j = 0, k = 0; j < size; j++) {
    		if(points[j] == p)
    			continue;
    		temp[k++] = points[j];
    	}
    	points = temp;
    	buttonManager.reset();
    	removePoint = false;
    	setPath();
    	repaint();
    }
    //------------------------------------------------------------------------
    public void setPoint(Point2D.Double p,double x,double y) {
    	p.setLocation(x, y);
    	setPath();
    	repaint();
    }
    //---------------------------------------------------------------------------
    @Override
    protected void paintComponent(Graphics g) {
    	super.paintComponent(g);
    	//System.out.println("pC path bnds=" + path.getBounds()); //pC path 
    
    //bnds=java.awt.Rectangle[x=0,y=0,width=0,height=0]
    	Graphics2D g2 = (Graphics2D)g;
    	//Path2D.Double path = new Path2D.Double();
    	g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    			RenderingHints.VALUE_ANTIALIAS_ON);
    	g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
    			RenderingHints.VALUE_STROKE_PURE);
    	if(firstTime) {
    	firstTime = false;
    	setPath();
    	}
    	g2.setPaint(Color.green.darker());
    
    	g2.draw(path); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ????
    
    	g2.setPaint(Color.red);
    	for(int j = 0; j < points.length; j++) {
    	mark(g2, points[j]);
    	}
    // For adding a point.
    	if(showConnections) {
    	g2.setPaint(Color.yellow);
    	for(int j = 0; j < points.length-1; j++) {
    	connectors[j] = new Line2D.Double(points[j], points[j+1]);
    	g2.draw(connectors[j]);
    	}
        }
    } // end paintComponent()
    
    /**
    * P(t) = B(n,0)*P0 + B(n,1)*P1 + ... + B(n,n)*Pn
    * 0 <= t <= 1
    *
    * B(n,m) = mth coefficient of nth degree Bernstein polynomial
    * = C(n,m) * t^(m) * (1 - t)^(n-m)
    * C(n,m) = Combinations of n things, taken m at a time
    * = n! / (m! * (n-m)!)
    */
    private void setPath() {
    	//System.out.println("2path bnds=" + path.getBounds());
    	path.reset();
    	int n = points.length;
    	int w = getWidth();
    	for(int j = 0; j <= w; j++) {
    	double t = (double)j/w; // [0 <= t <= 1.0]
    	double x = 0;
    	double y = 0;
    	for(int k = 1; k < n; k++) {
    	//System.out.println("k=" + k + " points[k]=" + points[k]); //k=0 points[k]=null
    	x += B(n-1,k,t)*points[k].x;
    	y += B(n-1,k,t)*points[k].y;
    	//System.out.println("j=" + j + " x=" + x + ", y=" + y);
    	}
    	if(j > 0)
    		path.lineTo(x,y);
    	else
    		path.moveTo(x,y);
    	}
    	
    } // end setPath()
    
    private double B(int n, int m, double t) {
    	return C(n,m) * Math.pow(t, m) * Math.pow(1.0 - t, n-m);
    }
    
    private double C(int n, int m) {
    	return factorial(n) / (factorial(m)*factorial(n-m));
    }
    
    private double factorial(int n) {
    	return (n > 1) ? n*factorial(n-1) : 1;
    }
    
    private void mark(Graphics2D g2, Point2D.Double p) {
    	g2.fill(new Ellipse2D.Double(p.x-2, p.y-2, 4, 4));
    }
    
    private JPanel getButtonPanel() {
    	buttonManager = new ButtonManager();
    	String[] ids = { "add", "cancel", "remove" };
    	JPanel panel = new JPanel();
    	for(int j = 0; j < ids.length; j++) {
    		JButton button = new JButton(ids[j]);
    		button.setEnabled(j != 1);
    		buttonManager.add(button);
    		button.setActionCommand(ids[j]);
    		button.addActionListener(this);
    		panel.add(button);
    	}
    	return panel;
    }
    //----------------------------------------------------------------
    public static void main(String[] args) {
    	DrawCurve test = new DrawCurve();
    	PointMover mover = new PointMover(test);
    	test.addMouseListener(mover);
    	test.addMouseMotionListener(mover);
    	JFrame f = new JFrame();
    	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	f.add(test);
    	f.add(test.getButtonPanel(), "Last");
    	f.setSize(1200,800);
    	f.setLocation(200,200);
    	f.setVisible(true);
    	}
    }
    //-------------------------------------------------------
    class PointMover extends MouseInputAdapter {
    	DrawCurve component;
    	Point2D.Double selectedPoint;
    	Cursor cursor;
    	Cursor defaultCursor = Cursor.getDefaultCursor();
    	Point2D.Double offset = new Point2D.Double();
    	boolean dragging = false;
    	final int PROX_DIST = 500;
    
    	PointMover(DrawCurve cf) {
    		component = cf;
    		BufferedImage image = getImage();
    		Point hotspot = new Point(17,17);
    		cursor = Toolkit.getDefaultToolkit().createCustomCursor(image, hotspot, null);
    	}	
    
    	public void mousePressed(MouseEvent e) {
    		if(selectedPoint != null) {
    			if(component.removePoint) { // remove
    				component.removePoint(selectedPoint);
    			} else { // drag
    				offset.x = e.getX() - selectedPoint.x;
    				offset.y = e.getY() - selectedPoint.y;
    				dragging = true;
    				}
    		} else if(component.showConnections) { // add
    			Point p = e.getPoint();
    			Line2D.Double[] lines = component.connectors;
    			for(int j = 0; j < lines.length; j++) {
    				if(lines[j].ptSegDist(p) < PROX_DIST) {
    				component.addPoint(p, j+1);
    				break;
    				}
    			}
    		}
    	}
    
    	public void mouseReleased(MouseEvent e) {
    		dragging = false;
    	}
    
    	public void mouseDragged(MouseEvent e) {
    		if(dragging) {
    			double x = e.getX() - offset.x;
    			double y = e.getY() - offset.y;
    			component.setPoint(selectedPoint, x, y);
    		}
    	}	
    
    /** For point selection. */
    	public void mouseMoved(MouseEvent e) {
    		Point p = e.getPoint();
    		Point2D.Double[] pts = component.points;
    		boolean hovering = false;
    		for(int j = 0; j < pts.length; j++) {
    			if(pts[j].distance(p) < PROX_DIST) {
    				hovering = true;	
    				if(selectedPoint != pts[j]) {
    					selectedPoint = pts[j];
    					component.setCursor(cursor);
    					break;
    				}
    			}
    		}
    
    	if(!hovering && selectedPoint != null) {
    		selectedPoint = null;
    		component.setCursor(defaultCursor);
    		}
    	}
    
    	private BufferedImage getImage() {
    		int w = 27, h = 27,
    		type = BufferedImage.TYPE_INT_ARGB_PRE;
    		BufferedImage image = new BufferedImage(w, h, type);
    		Graphics2D g2 = image.createGraphics();
    		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    			RenderingHints.VALUE_ANTIALIAS_ON);
    		g2.setPaint(new Color(0x333333));
    		g2.draw(new Line2D.Double(w/2, 0, w/2, 8)); // n
    		g2.draw(new Line2D.Double(0, h/2, 8, h/2)); // w
    		g2.draw(new Line2D.Double(w/2, h-8, w/2, h)); // s
    		g2.draw(new Line2D.Double(w-8, h/2, w, h/2)); // e
    		g2.dispose();
    		return image;
    	}
    }
    
    //----------------------------------------
    class ButtonManager implements ActionListener {
    	JButton[] buttons = new JButton[0];
    
    	public void actionPerformed(ActionEvent e) {
    		String ac = e.getActionCommand();
    			if(ac.equals("add")) {
    			getButton("remove").setEnabled(false);
    			enable(true);
    		
    			} else if(ac.equals("remove")) {
    				getButton("add").setEnabled(false);
    				enable(true);
    				} else {
    
    					reset();
    					}
    				}
    	
    	public void reset() {
    		getButton("add").setEnabled(true);
    		getButton("remove").setEnabled(true);
    		enable(false);
    	}
    
    	private void enable(boolean enable) {
    		getButton("cancel").setEnabled(enable);
    	}
    
    	public void add(JButton button) {
    		button.addActionListener(this);
    		int size = buttons.length;
    		JButton[] temp = new JButton[size+1];
    		System.arraycopy(buttons, 0, temp, 0, size);
    		temp[size] = button;
    		buttons = temp;
    	}
    
    	private JButton getButton(String target) {
    		for(int j = 0; j < buttons.length; j++) {
    		if(buttons[j].getActionCommand().equals(target))
    		return buttons[j];
    	}
    	return null;
    	}
    
    } // end class
    When u run the code the green line starts somewhere from the left corner top of the applet window and does not start from the points. Can some on give me an explanation or help me over come this??????

    Please do run the code to check it out

    Thanks in advance
  10. #21
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Your code does not compile. I get the following error:
    Code:
    DrawCurve4.java:266: addPoint(java.awt.geom.Point2D.Double,int) in DrawCurve4 cannot be applied to (java.awt.Point,int)
    				component.addPoint(p, j+1);
    				         ^
    That statement was commented out in previous versions.
  12. #22
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    15
    Rep Power
    0

    Reply


    That line can be commented out.... sorry i guess i had removed that for another reason
  14. #23
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Please don't post code that doesn't compile, unless you are having problems with the compiling.
    It wastes everyones time.

    The green line is drawn where the points tell it to be drawn. Get rid of the 0,0 point and it won't start at the upper left corner.

    You need to check the logic of the code to see why the green line and the red dots are in different places.
  16. #24
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    15
    Rep Power
    0

    Reply


    Originally Posted by NormR
    Please don't post code that doesn't compile, unless you are having problems with the compiling.
    It wastes everyones time.

    The green line is drawn where the points tell it to be drawn. Get rid of the 0,0 point and it won't start at the upper left corner.

    You need to check the logic of the code to see why the green line and the red dots are in different places.
    The green line and the red dots will be in different places only as red dots are the control points of the curve. They are not the coordinates. moving the points changes the shape of the curve which is my desired output, except for the green line starting at the top left corner even when the [0,0 ] coordinates are removed
  18. #25
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    You need an engineer or mathematician to check your equations.
    The path's points are computed by a bunch of equations. The red dots by some other means.

    Correcting the math errors are not programming problems.
  20. #26
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    15
    Rep Power
    0

    can you help me with the post where you had entered a factor to get the points visibl


    Hello,
    Is there any method where i can get the points visible without using the factor you had entered in my first post.

    I need this because i want a coordinate system which has (0,0) at the center of the applet window and not at the top left corner of the applet window.
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo