Results 1 to 7 of 7
- 11-01-2010, 06:49 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 9
- Rep Power
- 0
how to plot a curve which has double datatype coordinates
Greetings,
I have a set of coordinates as 0.00231,0.0034,-0,3245....etc.When i start to plot them i am not able to view the points but i see only one point on top left corner. Can some one help me with this. Here is my code
Initially i had used a factor such asJava 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; //import java.awt.Dimension; //import java.awt.Toolkit; public class DrawCurve extends JPanel implements ActionListener { ButtonManager buttonManager; //Point[] points; Path2D.Double path; Point2D.Double[] points; Line2D.Double[] connectors; boolean showConnections= false; boolean removePoint= false; boolean firstTime= true; // Get screen size in pixels, width by height. //Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); // Get screen resolution in dots per inch. //int dpi = Toolkit.getDefaultToolkit().getScreenResolution(); DrawCurve() { double[][] cds={{0.00000, 0.00000},{-0.00032, 0.00838},{0.00378, 0.01779}, {0.01233, 0.02815},{0.02536, 0.03931},{0.04285, 0.05108},{0.06478, 0.06320}, {0.09110, 0.07539},{0.12172, 0.08733},{0.15651, 0.09868},{0.19533, 0.10908}, {0.23796, 0.11819},{0.28417, 0.12564},{0.33371, 0.13113},{0.38625, 0.13435}, {0.44148, 0.13502},{0.49906, 0.13293},{0.55862, 0.12788},{0.61979, 0.11971}, {0.68221, 0.10832},{0.74551, 0.09361},{0.80933, 0.07555},{0.87332, 0.05412}, {0.93711, 0.02932},{1.00036, 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(cds[j][0] , cds[j][1]); } /*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; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); g2.setPaint(Color.green.darker()); // AffineTransform saveAT = g2.getTransform(); g2.translate(getWidth() / 2, getHeight() / 2);//when i use this i see a point //in the middle of the screen g2.draw(path); if(firstTime) { firstTime = false; setPath(); } // g2.scale(-1,-1); // g2.setTransform(saveAT); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ???? 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,1-t)*points[k].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(500,500); 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 = 5; PointMover(DrawCurve cf) { component = cf; BufferedImage image = getImage(); Point hotspot = new Point(17,17); cursor = Toolkit.getDefaultToolkit().createCustomCursor(image, hotspot, null); } @Override 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; } } } } @Override public void mouseReleased(MouseEvent e) { dragging = false; } @Override public void mouseDragged(MouseEvent e) { if(dragging) { double x = e.getX() - offset.x; double y = e.getY() - offset.y; component.setPoint(selectedPoint, x, y); } } @Override /** 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
and replaced it at the DrawCurve{}.Java Code:final double Factor = 500.0D; points[j] = new Point2D.Double(Factor * cds[j][0] + Factor, Factor * cds[j][1] + Factor);
But since my calculations are based totally on the coordinates i cant use this factor anymore. Can someone help me out with this??
I have not posted this question in anyother forum but some else had helped and given me the tip to use the factor to get this accomplished.
Here is the link to the thread
Page 2 - Java AWT - Dev Shed
- 11-02-2010, 09:41 AM #2
Member
- Join Date
- Sep 2010
- Posts
- 9
- Rep Power
- 0
can some one please help me out with the above problem
Hey guys can some one please help me out with the above problem. The main issue is that the points are in the top left corner of the applet window.Can you please help me out in getting the coordinates to the center.
- 11-02-2010, 10:23 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
You are using a Graphics2D object in your code. You can scale world coordinates so that your range [xmin, xmax] [ymin, ymax] maps to the coordinate space of your JComponent [0, xc] [0, yc] (read about the AffineTransform class). If your coordinates should be connected also read about the Path2D class (it implements the Shape interface).
kind regards,
Jos
- 11-02-2010, 01:51 PM #4
Member
- Join Date
- Sep 2010
- Posts
- 9
- Rep Power
- 0
I have done some changes but i am not successful yet
Hey Josh,Java 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; //import java.awt.Dimension; //import java.awt.Toolkit; import java.awt.geom.AffineTransform; public class DrawCurve extends JPanel implements ActionListener { ButtonManager buttonManager; //Point[] points; Path2D.Double path; Point2D.Double[] points; Line2D.Double[] connectors; boolean showConnections= false; boolean removePoint= false; boolean firstTime= true; AffineTransform tx = new AffineTransform(); public Dimension size = new Dimension( 600, 600 ); Point origin = new Point( size.width / 2, size.height / 2 ); float scalingFactor = 1.0f; DrawCurve() { double[][] cds={{0.00000, 0.00000},{-0.00032, 0.00838},{0.00378, 0.01779}, {0.01233, 0.02815},{0.02536, 0.03931},{0.04285, 0.05108},{0.06478, 0.06320}, {0.09110, 0.07539},{0.12172, 0.08733},{0.15651, 0.09868},{0.19533, 0.10908}, {0.23796, 0.11819},{0.28417, 0.12564},{0.33371, 0.13113},{0.38625, 0.13435}, {0.44148, 0.13502},{0.49906, 0.13293},{0.55862, 0.12788},{0.61979, 0.11971}, {0.68221, 0.10832},{0.74551, 0.09361},{0.80933, 0.07555},{0.87332, 0.05412}, {0.93711, 0.02932},{1.00036, 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(cds[j][0] , cds[j][1]); } /*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); g2.setPaint(Color.green.darker()); g2.draw(path); // g2.transform(transformer); if(firstTime) { firstTime = false; setPath(); } g2.setPaint(Color.red); for(int j = 0; j < points.length; j++) { setSize( size ); setVisible( true ); tx.translate( origin.x, origin.y ); for( int x = 0; x < 100; x++ ) { scalingFactor += 3; tx.translate( scalingFactor, scalingFactor ); mark(g2, points[j]); repaint(); } } // 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,1-t)*points[k].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(500,500); 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 = 5; // private Point mousePressedPt = null; public PointMover(DrawCurve cf) { component = cf; BufferedImage image = getImage(); Point hotspot = new Point(17,17); cursor = Toolkit.getDefaultToolkit().createCustomCursor(image, hotspot, null); } @Override public void mousePressed(MouseEvent e) { Point p = e.getPoint(); 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; } } } } @Override public void mouseReleased(MouseEvent e) { dragging = false; } @Override public void mouseDragged(MouseEvent e) { if(dragging) { double x = e.getX() - offset.x; double y = e.getY() - offset.y; component.setPoint(selectedPoint, x, y); } } @Override /** 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
I have tried to do some changes here but i am not successful. Can you please guide me through.
- 11-02-2010, 02:51 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 11-02-2010, 03:38 PM #6
Member
- Join Date
- Sep 2010
- Posts
- 9
- Rep Power
- 0
Hey Josh
I had already tried some examples i mean some small snippets and all transformations were fine, but i am really not able to make out what is wrong in my code. Alright i will try to play some more.. hope so i get some success...
- 11-02-2010, 04:14 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
char datatype
By frejon26 in forum New To JavaReplies: 5Last Post: 05-04-2010, 11:21 PM -
how to find a datatype
By hasysf in forum New To JavaReplies: 1Last Post: 09-06-2009, 10:41 AM -
Problem in using Object datatype
By mfaizan24 in forum New To JavaReplies: 6Last Post: 05-05-2009, 11:51 PM -
Problems with a complex datatype in a webservice
By lichtbringer in forum Web FrameworksReplies: 2Last Post: 10-29-2008, 05:32 AM -
Arc2D.Double coordinates
By alley in forum Java 2DReplies: 2Last Post: 11-07-2007, 10:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks