0

I am a beginner in Java Swing . I was trying to draw the graph of sin(x) in applet . I am surprised why applet always draws my axis only not my graph? here is my code :

import java.applet.Applet;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class NewApplet extends Applet
{

    public void init()
    {
        // TODO start asynchronous download of heavy resources
    }
    public void paint(Graphics g)
    {
    Graphics2D g2 = (Graphics2D) g;
    //get dimension
    Dimension appletSize = this.getSize();
    // make the applet empty at start
    g2.setBackground(Color.white);
    g2.setColor(Color.white);
    g2.fillRect(0, 0, appletSize.width, appletSize.height);
    //now draw all you need
    g2.setColor(Color.red);
    //x axis
    g2.drawLine(0, appletSize.height/2, appletSize.width, appletSize.height/2);
    //y axis
    g2.drawLine(appletSize.width/2, 0, appletSize.width/2, appletSize.height);
    //function
    g2.setColor(Color.black);
    for(int k=0; k<=180; k++)
    {
        g2.drawString(".", (float) Math.toRadians(k), (float) Math.sin(Math.toRadians(k)));
    }
}
}

I am plotting 180 points of sin(x) with String "." , but My applet didn't show a single plot.Why?

[ Mention if there any good practice I should follow or suggest me any good library (JAR) for drawing graphs ]

000
  • 405
  • 5
  • 20
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). .. – Andrew Thompson Apr 09 '16 at 12:32
  • .. 3) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Apr 09 '16 at 12:32
  • Do you suggest me to use swing? – 000 Apr 09 '16 at 12:34
  • I suggested many things.. The point is being drawn, but the y values are all less than 1, while the x values range up to around 3 (pixels). It is in the upper left of the applet, where I'd have expected it to draw. – Andrew Thompson Apr 09 '16 at 12:40
  • 1
    I have understood , my graph was too small , thanks – 000 Apr 09 '16 at 12:45

1 Answers1

1

It actually draws a graph.

Since values sine are between -1 and +1, and the radian values are between0 +π and -π, so the graph is drawn at those coordinates.

If you change this line:

g2.drawString(".", (float) Math.toRadians(k), (float) Math.sin(Math.toRadians(k)));

to this line:

g2.drawString(".", (appletSize.height/2)+(float) Math.toRadians(k)*50, (appletSize.width/2)+(float) Math.sin(Math.toRadians(k))*50);

You will see half the graph (since you have written the code for half the graph.)

Here:

  • I add (appletSize.height/2) and (appletSize.width/2) to the values, to bring them to the center of the screen (on the graph)
  • I multiply them by 50 to make the graph large, else, the graph is too small to understand.
dryairship
  • 6,022
  • 4
  • 28
  • 54
  • *"I add (appletSize.height/2) to the values, to bring them to the center of the screen (on the graph)"* An `AffineTransform` would work nicely here, for the axes (? plural of axis) moving AND scaling the plot. – Andrew Thompson Apr 09 '16 at 12:43
  • @AndrewThompson Actually I am not very much into applet programming, so I don't know about those classes. – dryairship Apr 09 '16 at 12:44
  • *"not very much into applet programming"* An `AffineTransform` is part of Java-2D as might be seen in an applet or (frame based) application (or a command line app. that draws to an image) or using Swing ***or*** AWT. – Andrew Thompson Apr 09 '16 at 12:47
  • 1
    @AndrewThompson Looking at its documentation right now. Thanks for introducing it to me. – dryairship Apr 09 '16 at 12:49
  • You might also find some examples among [these answers of mine](http://stackoverflow.com/search?tab=votes&q=user%3a418556%20affinetransform).. – Andrew Thompson Apr 09 '16 at 12:59
  • @AndrewThompson Thanks for that! – dryairship Apr 09 '16 at 13:01