• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Virtual Graphing Calculator

 
Justin Porter
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I need some ideas on how to start this. I want an applet that will allow me to write a function in the code (probably just cycle through it in a "for" loop) and output the results onto a graph, similar to that of a graphing calculator. All I need to do is plot the points as pixels, it doesn't need to be any more complicated than that.
Where I am stuck on this is on how to actually plot each pixel on the applet interface. I've done just regular java programming before this, I haven't worked on an applet yet, so mentioning basics and how things work is essential. Thanks ahead of time for any help you can give!
 
Joe Ess
Bartender
Posts: 9338
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The great thing about object-oriented design is that once you've learned how to manipulate one object, you automatically know how to manipulate all the related objects. Displaying pixels on an applet works pretty much the same as an application. What have you tried and what are you having trouble with?
 
Justin Porter
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For testing purposes, I was just attempting to draw 100 random pixels, so here is my code for that.

Then I have a JavaGraphUser that actually uses the class.
 
Ben Fischer
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For applets you don't create a panel, the applet is a panel. Also, the applet size is determined by the html that calls it. So the applet itself is pretty simple something like:

import java.awt.*;
import javax.swing.*;
import java.util.Random;

public class JavaGraphApp extends JApplet {
private final int Width=640;
private final int Height=480;
private int x=0,y=0;
Random rnd;

public void init() {
rnd=new Random();
}

public void paint (Graphics g) {
for (int i=0;i<100;i++) {
x=(int) (rnd.nextFloat()*Width);
y=(int) (rnd.nextFloat()*Height);
g.drawLine(x,y,x,y);
}
}
}

will do what you're looking for. Instead of defining the width and height probably you should use the getSize().width and getSize().height methods in case you specifiy different dimensions in the html.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic