Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
4.67/5 (3 votes)
See more:
I am going to work on a TDM-DeMux project, in which I have to plot spectrum from the generated signals. Even before that, I was asked to to plot spectrum for sine and cosine waves, first. Are there any libraries(or articles/tutorials) in Java that can help me in plotting spectrum.
Currently, I am working with the latest versions of JDK and Eclipse.

Thanks in advance!

(Updated)
Anyways, I have decided to create a graph first by myself. So I drew x&y axes, created a scale from an algorithm (given to me). The code is here:
public class Graph extends JPanel{
	int[] data = { 30, 60, 75, 90 };
	final int PAD = 200;
	
	protected void paintComponent(Graphics g) {
		
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D)g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		int h = getHeight();
		int w = getWidth();
		System.out.println("h = " + h + "\tw = " + w);
		g2.setColor(Color.GREEN);
		g2.drawLine(300, 0, 300, h+300);
		g2.drawLine(0, 300, w+300, 300);
                //this is the algorithm given to me
		double xScale = (w-2*PAD)/(data.length+1);
		double maxValue = 100.0;
		double yScale = (h-2*PAD)/maxValue;
                //------------------------------------//
		System.out.println("xScale= " + xScale + "\tyScale = " + yScale);
		//The origin location
		int x0 = 300;
		int y0 = h-300;
		System.out.println("x0 = " + x0 + "\ty0 = " + y0);
		g2.setPaint(Color.red);
		int x = x0 + data[0];
		int y = y0 + data[1];
		System.out.println("x = " + x);
		System.out.println("y = " + y);
		g2.drawOval(x, y, 30, 30);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame f = new JFrame("JGraph");
				f.setBackground(Color.BLACK);//background color is not changing
				f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				f.getContentPane().add(new Graph());
				f.setSize(600, 600);
				f.setLocation(100, 100);
				f.setVisible(true);
			}
		});	
	}

}


The problem is although x & y co-ordinates are positive relative to the origin. The circle which I drew is not
in the first quadrant. Why is this happening? Also I want to change the background color of my graph into black.
Thanks once again!
Posted
Updated 27-Aug-12 1:05am
v4

1 solution

you are placing a JPanel on your JFrame - therefor the JPanel is in front of the JFrame. So you need to change the JPanels color:

Java
// line 15ff
@Override
protected void paintComponent(Graphics g) {
    this.setBackground(Color.BLACK); //now black


the subtracted value (here 400, you used 300) is defining the position of the circle. You should relate it to PAD, which on a short look seems to be the definition of the square length in the diagram:
Java
// line 33/34
int y0 = h-400;
System.out.println("x0 = " + x0 + "\ty0 = " + y0);
 
Share this answer
 
Comments
stib_markc 27-Aug-12 7:46am    
Thank you very much that solved starting trouble I'm having. Is it possible to disable the maximize button and resizing of the UI, because if by accidentally I do it, then the values and the graph are distorted.
TorstenH. 27-Aug-12 8:11am    
f.setResizable(false); // do not allow resize of frame

is avoiding the change of size.
stib_markc 28-Aug-12 0:15am    
Thank you once again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900