I am currently working on this graph "thingy" were I am told to produce 4 types of graphs.

  • y=x2
  • y=x3
  • y=x*sin(x)
  • y=x*cos(10*x)

Now basically, I have the GUI/Interface ready. I seem to have a problem linking the y=x*sin(x) button in the PlottingWindow class to the PlottingPanel where it is supposed to produce the line etc.

In short once the sine button is pressed the graph is supposed to be produced.

This is what I have till now.

I would appreciate if someone could guide me. Thanks!

Plotting Panel -

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;

import java.awt.*;
import java.awt.event.*;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.HeadlessException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class PlottingPanel extends JPanel
	private static final int PAD = 0;
	private static final int h = 0;
	private static final float w = 0;
	//number of points to draw, the more the accurate
	  int noOfPoints = 100000;;

	public void paint(Graphics gr)
		  //get graphics object
	      Graphics2D g = (Graphics2D)gr;
	      //our axis length is going to be from -200 to 200
	      //convert to coordinate system
	      //draw axes
	      //draw y axis
	      g.drawLine(0, -425, 0, 425);
	      g.drawLine(-425, 0, 425, 0);

Plotting Window -

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PlottingWindow extends JFrame implements ActionListener{
	JButton addButton = new JButton("y = x2");
    JButton x3Button = new JButton("y = x3");
    JButton sinButton = new JButton("y = x*sin(x)");
    JButton cosButton = new JButton("y = x*cos(10*x)");
    JButton saveButton = new JButton("Save");
    JButton exitButton = new JButton("Exit");
	public PlottingWindow()
		super("Plotting Panel Assignment");
		//Panel that holds the buttons 
		JPanel panel2 = new JPanel();
		panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
		panel2.setLayout(new BorderLayout());
        panel2.setLayout(new GridLayout(6,0));
		panel2.add(Box.createRigidArea(new Dimension(20, 20)));
		panel2.add(Box.createRigidArea(new Dimension(20, 20)));
		panel2.add(Box.createRigidArea(new Dimension(20, 20)));
		panel2.add(Box.createRigidArea(new Dimension(20, 20)));
		panel2.add(Box.createRigidArea(new Dimension(20, 20)));
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout(10, 10));
		panel.add(panel2, "West");
		PlottingPanel plottingPanel = new PlottingPanel();
		panel.add(plottingPanel, "Center");

	public void actionPerformed(ActionEvent event) {
		if (event.getSource() == exitButton) {

Recommended Answers

All 4 Replies

You would need to move your PlottingPanel variable out of the constructor and up to class level. You can add a method like setPlot() to PlottingPanel to indicate which function is to be drawn and your painting method will need reference whatever info you set to know which function to draw. Your button handlers will call plottingPanel.setPlot() to change the type.

On a side note, you should override paintComponent() instead of paint() in your panel class.

So basically all work needs to go into the Plotting Panel?

Yes, you need to add some method by which your buttons can set which plot should be shown. For the plotting itself, you could create a Plot interface with a draw(Graphics) method that would draw the plot onto the supplied Graphics context and create small plot classes for each of the function types.

If that's too complex, you could also just use an Enum for the plot type and have branches for each type in your paintComponent().

Thanks! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.