Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Wiki
Search Coderanch
Advance search
Google search
Register / Login
Bookmark Topic
Watch Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Wiki
Plot Sine Curve
posted 8 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
This code sample shows how to draw a sine curve. It is based on code
posted here
by Adrian Sosialuk and Jesper Young. Optionally, a line connecting the data points can be shown.
import java.awt.*; import javax.swing.*; /** * This class plots a sine curve between -2*PI and 2*PI. */ public class SinusoidApp { // whether or not to draw a connecting line between the dots static boolean drawConnectingLine = true; public static void main (String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Sinusoid s = new Sinusoid(); s.setPreferredSize(new Dimension(300, 200)); frame.getContentPane().add(s); frame.pack(); frame.setVisible(true); } }); } static class Sinusoid extends JPanel { int previousY = 0; double degToRad (int deg) { return ((2*Math.PI)/360.0) * deg; } int scale (int i, int width) { return (int) ((i/(double)width)*720.0); } public void paintComponent (Graphics g) { super.paintComponent(g); int width = getWidth(); int height = getHeight(); for (int i=0; i<width; i++) { int y = (int) Math.round((-Math.sin(degToRad(scale(i,width)))+1)*height/2.0); if (drawConnectingLine && i>0) { g.drawLine(i-1, previousY, i, y); } else { g.drawLine(i, y, i, y); } previousY = y; } } } }
CategoryCodeSamples
CodeBarn
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Plot Test
Creating Arrow Icon Help
Drawing a simple clock in java
Virtual Graphing Calculator
Turn Based Strategy Games
More...