Question

I'm currently having some problems regarding my homework.

Here's the Exercise:

(Plot the sine and cosine functions) Write a program that plots the sine function in red and the cosine function in blue.

hint: The Unicode for Pi is \u03c0. To display -2Pi, use g.drawString("-2\u03c0", x, y). For a trigonometric function like sin(x), x is in radians. Use the following loop to add the points to a polygon p

for (int x = -170; x <= 170; x++) {
    p.addPoint(x + 200, 100 - (int)(50 * Math.sin((x / 100.0) * 2 * Math.PI)));

-2Pi is at (100, 100), the center of the axis is at (200, 100), and 2Pi is at (300, 100) Use the drawPolyline method in the Graphics class to connect the points.

Okay, so the sin function I have is a little different from the one in the exercise but it works so it shouldn't be a problem. The cosine function on the other hand, I'm having trouble finding the code for it so I don't have that in my program.

What I also need to do is place -Pi and Pi on the graph on their respectable places.

So, here's the code.

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Polygon;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Exercise13_12 extends JFrame {

public Exercise13_12() {
    setLayout(new BorderLayout());
    add(new DrawSine(), BorderLayout.CENTER);
}

public static void main(String[] args) {
    Exercise13_12 frame = new Exercise13_12();
    frame.setSize(400, 300);
    frame.setTitle("Exercise13_12");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

class DrawSine extends JPanel {

    double f(double x) {
        return Math.sin(x);
    }

    double g(double y) {
        return Math.cos(y);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.drawLine(10, 100, 380, 100);
        g.drawLine(200, 30, 200, 190);

        g.drawLine(380, 100, 370, 90);
        g.drawLine(380, 100, 370, 110);
        g.drawLine(200, 30, 190, 40);
        g.drawLine(200, 30, 210, 40);

        g.drawString("X", 360, 80);
        g.drawString("Y", 220, 40);

        Polygon p = new Polygon();

        for (int x = -170; x <= 170; x++) {
            p.addPoint(x + 200, 100 - (int) (50 * f((x / 100.0) * 2
                    * Math.PI)));

        }

        g.drawPolyline(p.xpoints, p.ypoints, p.npoints);
        g.drawString("-2\u03c0", 95, 115);
        g.drawString("2\u03c0", 305, 115);
        g.drawString("0", 200, 115);
    }
 }
}

If anyone have the time to help me out I would be very grateful.

Was it helpful?

Solution

Try this:

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Exercise13_12 extends JFrame {

public Exercise13_12() {
    setLayout(new BorderLayout());
    add(new DrawSine(), BorderLayout.CENTER);
}

public static void main(String[] args) {
    Exercise13_12 frame = new Exercise13_12();
    frame.setSize(400, 300);
    frame.setTitle("Exercise13_12");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

class DrawSine extends JPanel {

    double f(double x) {
        return Math.sin(x);
    }

    double gCos(double y) {
        return Math.cos(y);
    }

    protected void paintComponent(Graphics g) 
    {
        super.paintComponent(g);

        g.drawLine(10, 100, 380, 100);
        g.drawLine(200, 30, 200, 190);

        g.drawLine(380, 100, 370, 90);
        g.drawLine(380, 100, 370, 110);
        g.drawLine(200, 30, 190, 40);
        g.drawLine(200, 30, 210, 40);

        g.drawString("X", 360, 80);
        g.drawString("Y", 220, 40);

        Polygon p = new Polygon();
        Polygon p2 = new Polygon();

       for (int x = -170; x <= 170; x++) {
            p.addPoint(x + 200, 100 - (int) (50 * f((x / 100.0) * 2
                    * Math.PI)));

        }

        for (int x = -170; x <= 170; x++) {
            p2.addPoint(x + 200, 100 - (int) (50 * gCos((x / 100.0) * 2
                    * Math.PI)));

        }

        g.setColor(Color.red);
        g.drawPolyline(p.xpoints, p.ypoints, p.npoints);
        g.drawString("-2\u03c0", 95, 115);
        g.drawString("2\u03c0", 305, 115);
        g.drawString("0", 200, 115);

        g.setColor(Color.blue);
        g.drawPolyline(p2.xpoints, p2.ypoints, p2.npoints);

    }
 }
}

enter image description here

Basically it's the same code all over, but you need a new polygon to draw it. And then I set the color using the setColor() function of the Graphics.

OTHER TIPS

You can add this to your paintComponent method:

        //Draw pi and -pi
        g.drawString("-\u03c0", 147, 100);
        g.drawString("\u03c0", 253, 100);   

        //Create a new polygon
        Polygon p2 = new Polygon();

        //Add the points of the cosine
        for (int x = -170; x <= 170; x++) {
            p2.addPoint(x + 200, 100 - (int) (50 * g((x / 100.0) * 2
                    * Math.PI)));
        }
        //Draw the function
        g.drawPolyline(p2.xpoints, p2.ypoints, p2.npoints);

With that you can have the results that you need.

Okay so now that the program is complete i ended up with this.

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Exercise13_12 extends JFrame {

public Exercise13_12() {
setLayout(new BorderLayout());
add(new DrawSine(), BorderLayout.CENTER);
}

public static void main(String[] args) {
Exercise13_12 frame = new Exercise13_12();
frame.setSize(400, 300);
frame.setTitle("Exercise13_12");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);

}

class DrawSine extends JPanel {

double f(double x) {
    return Math.sin(x);
}

double gCos(double y) {
    return Math.cos(y);
}

protected void paintComponent(Graphics g) 
{
    super.paintComponent(g);

    g.drawLine(10, 100, 380, 100);
    g.drawLine(200, 30, 200, 190);

    g.drawLine(380, 100, 370, 90);
    g.drawLine(380, 100, 370, 110);
    g.drawLine(200, 30, 190, 40);
    g.drawLine(200, 30, 210, 40);

    g.drawString("X", 360, 80);
    g.drawString("Y", 220, 40);

    Polygon p = new Polygon();
    Polygon p2 = new Polygon();

   for (int x = -170; x <= 170; x++) {
        p.addPoint(x + 200, 100 - (int) (50 * f((x / 100.0) * 2
                * Math.PI)));

    }

    for (int x = -170; x <= 170; x++) {
        p2.addPoint(x + 200, 100 - (int) (50 * gCos((x / 100.0) * 2
                * Math.PI)));

    }

    g.setColor(Color.red);
    g.drawPolyline(p.xpoints, p.ypoints, p.npoints);
    g.drawString("-2\u03c0", 95, 115);
    g.drawString("-\u03c0", 147, 115);
    g.drawString("\u03c0", 253, 115);  
    g.drawString("2\u03c0", 305, 115);
    g.drawString("0", 200, 115);

    g.setColor(Color.blue);
    g.drawPolyline(p2.xpoints, p2.ypoints, p2.npoints);

  }
 }
}

For anyone that might have the same problem as me later on.

And thanks guys for helping me out, always grateful.

check it out .....

public class GuiBasicstest {    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic herei
        guiBasics gb = new guiBasics();
        JFrame jf = new JFrame();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setSize(500,500);
        jf.add(gb);            
        jf.setVisible(true);            
    }        
}    

................................................................................
package guibasics;    
import java.awt.Graphics;
import javax.swing.JPanel;

/**
 *
 * @author ALI
 */
public class guiBasics extends JPanel{
    //Graphics g = null;

    public void paintComponent(Graphics g){

        //super.paintComponent(g);
        for(double i = 0; i<200;i++){
            double y= Math.sin(i);
            draw(i,y,g);
        }
    }
    private void draw(double x , double y,Graphics g ){ 
         double x1, y1;
         x+=10;
         y+=10;
         x*=10;
         y*=30;

       x1=x+1;
       y1=y+1;
       Double d = new Double(x);
       int a = d.intValue();
       int b = (new Double(y)).intValue();
       int c = (new Double(x1)).intValue();
       int e = (new Double(y1)).intValue();
       g.drawLine(a, b, c, e);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top