Results 1 to 13 of 13
Thread: Java Project
- 08-26-2012, 04:46 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 4
- Rep Power
- 0
Java Project
Hi Everyone!
I am new to java and having a hard time learning it. Can anyone please help me with this question?
This question consists of making a class which could take in a M x 3 array of data (first column is the x-data, second column is the first set of y-data, and the third column is the second set of y-data).
Also please note that there should be a method in the class which allows for the plotting of data.
Below are the plot requirements:
1. It should show "Calculated" results plotted in black lines from point to point
2. It should consist Theoretical results plotted using red lines from point to point
3. X and Y axes plotted. Make the axes thicker than the data lines
4. For every 50 segments, display the x-values on the x axis, and the y values on the y-axis
Hints:
1. Think about mathematical relationships that could be useful for plotting based on the x,y locations on a frame
2. Create a test suit to check and confirm your results.
3. Look for objects and methods that exist in Java for required plotting
Thanks a lot!
:)Last edited by sam114; 08-26-2012 at 05:12 AM.
-
Re: Java Project
If you are in true need of our help, please consider putting in the effort to post a complete question, one that doesn't require folks to go off to a link. Your efforts will be appreciated and will likely result in getting better and quicker answers.
Also, rather than posting just requirements, you will want to post what you've tried so far to solve the assignment and any *specific* questions you may have. Usually just posting an assignment without showing your efforts will only allow us to post very general recommendations such as links to tutorials while specific questions will often get more specific and more helpful answers.Last edited by Fubarable; 08-26-2012 at 05:11 AM.
-
Re: Java Project
- 08-26-2012, 05:44 AM #4
Member
- Join Date
- Aug 2012
- Posts
- 4
- Rep Power
- 0
Re: Java Project
Thanks,
First I am having trouble in understand the question.
This is what I understand, correct me if I am wrong please
a) I should have some random data as x, y1,y2
b) and then I need a way to plot these data in a graph. But how can we plot y1 and y2 data at the same time.
c) Continued from b) so does this mean that the Calculated data can be taken as y1 and the Theoretical data can be considered as y2 ?
Thanks again!
-
Re: Java Project
You should have some data I think with which to test your code. I don't see that it is specified as random though, and it is conceivable that your instructor will use a different set of data to test your code as well. Perhaps your code can be written to accept any matrix of data that conforms to certain rules, such as a 2-dimensional array of positive ints. I imagine that the x data would be monotonically increasing and uniformly spaced such as 5, 10, 15, 20, 25, 30, 35, 40,...
You would likely draw them in your JPanel's paintComponent method (if this is Swing), and would loop through each column of data to draw each point and connecting line. So I would loop through the y1 data and draw its plot (also using the x data to know where to put the points in the x dimension), then do the same with the y2 data column.b) and then I need a way to plot these data in a graph. But how can we plot y1 and y2 data at the same time.
Possibly. Per my reading of the instructions, this does not appear to be clear. Perhaps you can get clarification from your instructor?c) Continued from b) so does this mean that the Calculated data can be taken as y1 and the Theoretical data can be considered as y2 ?
- 08-26-2012, 07:02 AM #6
Member
- Join Date
- Aug 2012
- Posts
- 4
- Rep Power
- 0
Re: Java Project
Thanks again. Can you please tell me if this code is okay as for the x data points and both y1,y2 data points in one plot?
package verticalplot;
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
public class VerticalPlot
{
public VerticalPlot()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new VerticalPlotPanel());
f.setSize(400,400);
f.setLocation(200,200);
f.setVisible(true);
}
public static void main(String[] args)
{
new VerticalPlot();
}
}
class VerticalPlotPanel extends JPanel
{
final int PAD = 20;
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASIN G,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
// abcissas- bottom
g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
// labels
Font font = getFont().deriveFont(14f);
g2.setFont(font);
FontRenderContext frc = g2.getFontRenderContext();
String[] s = { "1", "5", "10", "15", "20" };
for(int j = 0; j < s.length; j++)
{
float width = (float)font.getStringBounds(s[j], frc).getWidth();
float height = font.getLineMetrics(s[j], frc).getAscent();
float sx = PAD + j *(w - 2*PAD)/(s.length-1) - width/2;
float sy = h - PAD + height + 4;
g2.drawString(s[j], sx, sy);
}
// middle
double y = PAD + (h-2*PAD)*2/3;
g2.draw(new Line2D.Double(PAD, y, w-PAD, y));
// top
y = PAD + (h-2*PAD)/3;
g2.draw(new Line2D.Double(PAD, y, w-PAD, y));
// ordinate
g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
// labels - top
double vSpan = (h-4*PAD)/3;
s = new String[] { "20", "10", "0", };
double yInc = vSpan/(s.length-1);
for(int j = 0; j < s.length; j++)
{
float width = (float)font.getStringBounds(s[j], frc).getWidth();
float height = font.getLineMetrics(s[j], frc).getAscent();
float sx = PAD - width - 2;
float sy = (float)(PAD + j * yInc + height);
g2.drawString(s[j], sx, sy);
}
// middle
s = new String[] { "3", "2", "1" };
y = PAD + (h-2*PAD)/3;
for(int j = 0; j < s.length; j++)
{
float width = (float)font.getStringBounds(s[j], frc).getWidth();
float height = font.getLineMetrics(s[j], frc).getAscent();
float sx = PAD - width - 2;
float sy = (float)(y + j * yInc + height);
g2.drawString(s[j], sx, sy);
}
// bottom
s = new String[] { "8", "6", "4", "2" };
y = PAD + (h-2*PAD)*2/3;
yInc = vSpan/(s.length-1);
for(int j = 0; j < s.length; j++)
{
float width = (float)font.getStringBounds(s[j], frc).getWidth();
float height = font.getLineMetrics(s[j], frc).getAscent();
float sx = PAD - width - 2;
float sy = (float)(y + j * yInc + height);
g2.drawString(s[j], sx, sy);
}
// draw lines - bottom
vSpan = (h-2*PAD)/3;
g2.setPaint(Color.red);
double x1 = PAD, y1 = h-PAD, x2 = PAD + (w-2*PAD)/2, y2 = y1 - vSpan;
g2.draw(new Line2D.Double(x1, y1, x2, y2));
// middle
g2.setPaint(Color.green.darker());
y1 = h-PAD - 2*vSpan; x2 = w-PAD; y2 = y1 + 0.8 * vSpan;
g2.draw(new Line2D.Double(x1, y1, x2, y2));
// top
g2.setPaint(Color.blue);
y1 = PAD + vSpan/2; y2 = PAD + vSpan;
g2.draw(new Line2D.Double(x1, y1, x2, y2));
}
}
- 08-26-2012, 10:59 AM #7
Re: Java Project
Forum Rules -- especially the third paragraph
Guide For New Members
BB Code List - Java Programming Forum
dbWhy do they call it rush hour when nothing moves? - Robin Williams
-
Re: Java Project
I can't really tell. I suggest that you break down the large method into smaller methods with logical names to make your code easier to understand, and that you use code tags (please see Darryl's links) when you post the new code. Is this code you've created or code you've found?
- 08-26-2012, 05:43 PM #9
Re: Java Project
I can answer that.
How to draw a chart with multiple y-axises (Swing / AWT / SWT forum at JavaRanch)
dbWhy do they call it rush hour when nothing moves? - Robin Williams
-
Re: Java Project
Thanks Darryl.
Then Sam your job is to scrap that code entirely. Use the concepts found in it, for sure, but you need to write your code from scratch using these concepts if you're going to make headway at all. And please don't ask us to help debug code that is not yours.
- 08-26-2012, 08:38 PM #11
Member
- Join Date
- Aug 2012
- Posts
- 4
- Rep Power
- 0
Re: Java Project
Last edited by sam114; 08-26-2012 at 09:00 PM.
-
Re: Java Project
The goal of the assignment is for you to gain an understanding of Java and to use it in your code, and that's where I think you should start. The Swing tutorials have great resources to help you with this and they can be found here: Using Swing Components
- 09-02-2012, 05:23 AM #13
Similar Threads
-
Creating a project in eclipse from existing project
By Suraiya in forum New To JavaReplies: 1Last Post: 10-08-2011, 09:14 AM -
JAVA Project
By kappasig84 in forum New To JavaReplies: 6Last Post: 12-13-2009, 09:28 PM -
Java Project
By Smirre in forum New To JavaReplies: 16Last Post: 11-17-2008, 08:37 PM -
how to get the project in java.....
By thirumurugan.sethu in forum New To JavaReplies: 2Last Post: 10-07-2008, 07:37 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks