Results 1 to 7 of 7
Thread: Functions Graph
- 10-22-2010, 09:53 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 3
- Rep Power
- 0
Functions Graph
Hi there, I would like to design a plotter, where the user enters min x / max x and min y and max y accordingly, and then presses plot button to see the points on the graph. I have managed setting the labels, buttons, combobox; all these entries are settled using the gridbay layout on the left.
Now on the right I have implemented the x and y coordinates which resizes accordingly in any size of the main frame window. How do I rescale the coordinates as well along the x and y coordinates when the main frame window is resized?
Thanks
- 10-24-2010, 06:18 PM #2
Member
- Join Date
- Sep 2010
- Posts
- 33
- Rep Power
- 0
There are 2 possible options. First you can calculate all painting coordinates with a factor for and factor for y. Alternate you calculated only the factors and set a transformation on the Graphics.
Volker Berlin
www.inetsoftware.de
- 10-24-2010, 07:30 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 3
- Rep Power
- 0
Can you please provide me a sample code, cause I tried many times and still cannot do it... thanks
- 10-24-2010, 08:00 PM #4
Member
- Join Date
- Sep 2010
- Posts
- 33
- Rep Power
- 0
I does not know what you expected. It is like how I have described in text.
Dimension realSize = size();
float factorX = realSite.width/virtualWidth;
float factorY = realSite.heigth/virtualHeight;
Graphics gr = g.createGraphics();
gr.setScale( factorX, factorY );
...Volker Berlin
www.inetsoftware.de
- 10-24-2010, 08:14 PM #5
Member
- Join Date
- Oct 2010
- Posts
- 3
- Rep Power
- 0
The following code is what I have done so far,
Moderator Edit: Code tags addedJava Code:import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.AffineTransform; import javax.swing.JPanel; public class GraphPanel extends JPanel { private double minX; @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2D = (Graphics2D) g; this.setBackground(Color.lightGray); // sets GraphPanel background // color; // Set graph panel width and height to resize accordingly; int graphWidth = getWidth(); int graphHeight = getHeight(); // Drawed the x and y coordinates; g.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2); // x g.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight()); // y AffineTransform oldtransform = g2D.getTransform(); g2D.scale(1.0, -1.0); g.translate(graphWidth / 2, graphHeight / 2); // shift to cartesian int noOfPoints= 10000; int maxX = 0; int xRange= (int) (minX + maxX); double x = this.minX; double increment = xRange/(1.0*noOfPoints); // calculate all the points. for (int i=0; i < noOfPoints; i++){ int xPoint = x; Object xPoints = null; Object yPoint = calculateY(xPoints); x += increment; // draw xPoint, yPoint here g.fillRect(x,x,1,1); } // restore old coordinate system. g2D.setTransform(oldtransform); } private Object calculateY(Object xPoints) { // TODO Auto-generated method stub return null; } }Last edited by Fubarable; 11-14-2010 at 12:45 AM. Reason: Moderator Edit: Code tags added
- 10-24-2010, 08:23 PM #6
Member
- Join Date
- Sep 2010
- Posts
- 33
- Rep Power
- 0
Seems ok. For calculateY you need to know the range of your points, the virtual size. Currently xPoint and yPoint are ever null.
Volker Berlin
www.inetsoftware.de
- 11-13-2010, 11:01 PM #7
after // calculate all the points you should define the xPoint as double and when you call fillRect cast the x as int.
Similar Threads
-
Help in implementing XOR functions
By Manfizy in forum New To JavaReplies: 3Last Post: 04-13-2010, 02:25 AM -
JSP Functions
By sysout in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 11-07-2009, 04:10 AM -
2 functions
By baze7 in forum New To JavaReplies: 3Last Post: 08-14-2009, 04:41 AM -
Functions in jsp
By samson in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 03-25-2009, 10:04 PM -
problems with functions
By kri in forum New To JavaReplies: 6Last Post: 02-07-2009, 08:09 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks