import java.awt.Graphics;
public class GraphApplet extends java.applet.Applet {
double f(double x) {
return (Math.cos(x/5) + Math.sin(x/7) + 2) * size().height / 4;
}
public void paint(Graphics g) {
for (int x = 0 ; x < size().width ; x++) {
g.drawLine(x, (int)f(x), x + 1, (int)f(x + 1));
}
}
}
and it produces an result of:
import java.applet.*;
import java.awt.*;
public class GraphAppletv2 extends java.applet.Applet {
public void init(){
Canvas funcShow = new DrawFuncv2(); // Canvas with the function
setLayout(new BorderLayout()); // one of a handfull of layouts
add("Center", funcShow);
}
}
File: DrawFuncv2.java
import java.applet.*;
import java.awt.*;
public class DrawFuncv2 extends Canvas {
double f(double x) {
return (Math.cos(x/5) + Math.sin(x/7) + 2) * size().height / 4;
}
public void paint(Graphics g) {
for (int x = 0 ; x < size().width ; x++) {
g.drawLine(x, (int)f(x), x + 1, (int)f(x + 1));
}
}
}
and it produces an result of:javac GraphAppletv2.java DrawFuncv2.java
import java.applet.*;
import java.awt.*;
public class GraphAppletv3 extends java.applet.Applet {
Label v1;
public void init(){
Canvas funcShow = new DrawFuncv3(); // Canvas with the function
Panel conPanel = new Panel(); // Panel to hold GUI elements
v1 = new Label("0"); // A new label, initialized to 0
// Set the conPanel layout and add two elements to it
conPanel.setLayout(new BorderLayout());
conPanel.add("Center", v1);
conPanel.add("South", new Scrollbar(Scrollbar.HORIZONTAL, 1, 0, 1, 10));
setLayout(new BorderLayout()); // one of a handfull of layouts
add("Center", funcShow);
add("South", conPanel);
}
public boolean handleEvent(Event evt) {
if (evt.target instanceof Scrollbar) {
int v=((Scrollbar)evt.target).getValue();
v1.setText(String.valueOf(v));
}
return true;
}
}
File: DrawFuncv3.java
import java.applet.*;
import java.awt.*;
public class DrawFuncv3 extends Canvas {
double f(double x) {
return (Math.cos(x/5) + Math.sin(x/7) + 2) * size().height / 4;
}
public void paint(Graphics g) {
for (int x = 0 ; x < size().width ; x++) {
g.drawLine(x, (int)f(x), x + 1, (int)f(x + 1));
}
}
}
and it produces an result of:
NOTE: There is an error in this approach (in the following applet), but I want to demonstrate the error, so if it seems to not be working, don't panic, read on. ;)
import java.applet.*;
import java.awt.*;
public class GraphAppletv4 extends java.applet.Applet {
Label v1;
public void init(){
Canvas funcShow = new DrawFuncv4(); // Canvas with the function
Panel conPanel = new Panel(); // Panel to hold GUI elements
v1 = new Label("0"); // A new label, initialized to 0
// Set the conPanel layout and add two elements to it
conPanel.setLayout(new BorderLayout());
conPanel.add("Center", v1);
conPanel.add("South", new Scrollbar(Scrollbar.HORIZONTAL, 1, 0, 1, 10));
setLayout(new BorderLayout()); // one of a handfull of layouts
add("Center", funcShow);
add("South", conPanel);
}
public boolean handleEvent(Event evt) {
if (evt.target instanceof Scrollbar) {
int v=((Scrollbar)evt.target).getValue();
DrawFuncv4.i=((Scrollbar)evt.target).getValue();
v1.setText(String.valueOf(v));
}
return true;
}
}
File: DrawFuncv4.java
import java.applet.*;
import java.awt.*;
public class DrawFuncv4 extends Canvas {
static int i;
double f(double x) {
return (Math.cos(x*i/5) + Math.sin(x*i/7) + 2) * size().height / 4;
}
public void paint(Graphics g) {
for (int x = 0 ; x < size().width ; x++) {
g.drawLine(x, (int)f(x), x + 1, (int)f(x + 1));
}
}
}
and it produces an result of:
import java.applet.*;
import java.awt.*;
public class GraphAppletv5 extends java.applet.Applet implements Runnable {
Label v1;
Thread runner;
DrawFuncv5 funcShow = new DrawFuncv5(); // Canvas with the function
public void start() {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}
public void stop() {
if (runner != null) {
runner.stop();
runner = null;
}
}
public void run() {
while (true) {
funcShow.reset();
try { Thread.sleep(1000); }
catch (InterruptedException e) { }
}
}
public void init(){
Panel conPanel = new Panel(); // Panel to hold GUI elements
v1 = new Label("0"); // A new label, initialized to 0
// Set the conPanel layout and add two elements to it
conPanel.setLayout(new BorderLayout());
conPanel.add("Center", v1);
conPanel.add("South", new Scrollbar(Scrollbar.HORIZONTAL, 1, 0, 1, 10));
setLayout(new BorderLayout()); // one of a handfull of layouts
add("Center", funcShow);
add("South", conPanel);
}
public boolean handleEvent(Event evt) {
if (evt.target instanceof Scrollbar) {
int v=((Scrollbar)evt.target).getValue();
DrawFuncv5.i=((Scrollbar)evt.target).getValue();
v1.setText(String.valueOf(v));
}
return true;
}
}
File: DrawFuncv5.java
import java.applet.*;
import java.awt.*;
public class DrawFuncv5 extends Canvas {
static int i;
public void reset() {
repaint();
}
double f(double x) {
return (Math.cos(x*i/5) + Math.sin(x*i/7) + 2) * size().height / 4;
}
public void paint(Graphics g) {
for (int x = 0 ; x < size().width ; x++) {
g.drawLine(x, (int)f(x), x + 1, (int)f(x + 1));
}
}
}
and it produces an result of: