Math function graph 1 : Math Functions « Advanced Graphics « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Advanced Graphics » Math FunctionsScreenshots 
Math function graph 1
Math function graph 1

/*************************************************************************
*                                                                        *
*  This source code file, and compiled classes derived from it, can      *
*  be used and distributed without restriction, including for commercial *
*  use.  (Attribution is not required but is appreciated.)               * 
*                                                                        *
*   David J. Eck                                                         *
*   Department of Mathematics and Computer Science                       *
*   Hobart and William Smith Colleges                                    *
*   Geneva, New York 14456,   USA                                        *
*   Email: eck@hws.edu          WWW: http://math.hws.edu/eck/            *
*                                                                        *
*************************************************************************/


import java.awt.*;
import edu.hws.jcm.data.*;
import edu.hws.jcm.draw.*;
import edu.hws.jcm.awt.*;

public class GraphApplet3 extends java.applet.Applet {
      public static void main(String[] a){
         javax.swing.JFrame f = new javax.swing.JFrame();
         java.applet.Applet app = new GraphApplet3();
         app.init();
         
         f.getContentPane().add (app);

         f.pack();
         f.setSize (new Dimension (500500));
         f.setVisible(true);
      }  
   private DisplayCanvas canvas;
   
   public void stop() {
      canvas.releaseResources();
   }
   
   public void init() {
   
      Parser parser = new Parser();
      Variable x = new Variable("x");
      parser.add(x);

      canvas = new DisplayCanvas();
      canvas.setHandleMouseZooms(true);
      canvas.add(new Panner());
      
      CoordinateRect coords = canvas.getCoordinateRect();
      
      LimitControlPanel limits =
           new LimitControlPanelLimitControlPanel.SET_LIMITS | LimitControlPanel.RESTORE, false);
      limits.addCoords(canvas);
      
      ExpressionInput input = new ExpressionInput("sin(x)+2*cos(3*x)", parser);
      Function func = input.getFunction(x);
   
      Graph1D graph = new Graph1D(func);
      
      VariableInput xInput = new VariableInput();
      VariableSlider xSlider = new VariableSlidercoords.getValueObject(CoordinateRect.XMIN)
                                                      coords.getValueObject(CoordinateRect.XMAX) );
      
      Value yValue = new ValueMath(func,xSlider)// A Value object to represent the y-coord of the point.
      
         // Instead of using a crosshair to mark a point on the graph, it is marked
         //   with two gray lines and a small magenta oval.  These geometric objects
         //   are represented as objects belonging to the class DrawGeometric,
         //   which makes it possible to draw a variety of geometric figures on a
         //   DisplayCanvas.
      DrawGeometric vLine = new DrawGeometric(DrawGeometric.LINE_ABSOLUTE,xSlider,new Constant(0),xSlider,yValue);
      DrawGeometric hLine = new DrawGeometric(DrawGeometric.LINE_ABSOLUTE,new Constant(0),yValue,xSlider,yValue);
      DrawGeometric point = new DrawGeometric(DrawGeometric.OVAL_CENTERED,xSlider,yValue,3,3);
      vLine.setColor(Color.lightGray);
      hLine.setColor(Color.lightGray);
      point.setColor(Color.magenta);
      point.setFillColor(Color.magenta);

      
      DrawString info = new DrawString("x = #\nf(x) = #", DrawString.TOP_LEFT,
                                                             new Value[] { xSlider, yValue });
      info.setFontnew Font("SansSerif",Font.BOLD,12) );
      info.setColornew Color(0,100,0) );
      info.setOffset(10);

      ComputeButton graphIt = new ComputeButton("Graph It!");
      
      setLayout(new BorderLayout(3,3));
      setBackground(Color.lightGray);      

          // In this version of the applet, I have built the interface from
          //    regular Panels instead of JCMPanels.  This puts responsibility
          //    for a lot more setup in the hands of the programmer.  The gain
          //    is in efficiency.  Here, my object is to avoid recomputing the
          //    graph just because the user adjusts the slider.  To do this,
          //    I have to use two controllers, which listen for different user
          //    actions.  (Of course, computers are so fast now that the extra
          //    computation probably doesn't add a perceptible delay.  In this 
          //    case, the extra design work is probably not worth the trouble.)
      Panel top = new Panel();
      top.setLayout(new BorderLayout(3,3));
      Panel bottom = new Panel();
      bottom.setLayout(new BorderLayout(3,3));
      add(canvas, BorderLayout.CENTER);  // Add components directly to the applet.
      add(limits, BorderLayout.EAST);
      add(bottom, BorderLayout.SOUTH);
      add(top, BorderLayout.NORTH);

      top.add(input, BorderLayout.CENTER);
      top.add(new Label(" f(x) = "), BorderLayout.WEST);
      top.add(graphIt, BorderLayout.EAST);
      
      bottom.add(xSlider, BorderLayout.CENTER);
      bottom.add(xInput, BorderLayout.EAST);
      bottom.add(new Label("  x = "), BorderLayout.WEST);

      
      canvas.addnew Axes() );
      canvas.addhLine );
      canvas.addvLine );
      canvas.addpoint );
      canvas.addgraph );
      canvas.addinfo );
      canvas.addnew DrawBorder(Color.darkGray, 2) );
      
      Controller cc = new Controller();  // This controller will listen for changes
      xInput.setOnUserAction(cc);        //   In the VariableSlider or VariableInput,
      xSlider.setOnUserAction(cc);       //   As well as in the limits on the coordinates.
      coords.setOnChange(cc);

      cc.addnew Tie(xSlider,xInput) )// Ties the values of the slider and VariableInput.
      
      cc.addhLine );    // I have to tell the controller which objects need to be recomputed
      cc.addvLine );    //    when it sees some kind of change.  This includes all the 
      cc.addpoint );    //    objects that depend on the x-coordinate.  Note that is ALSO
      cc.addinfo );     //    includes xInput and xSlider, which need to be checked for 
      cc.addxInput );   //    changes in their values.  The value associated with a
      cc.addxSlider );  //    VariableSlider or VariableInput doesn't actually change
                          //    until a Controller checks it.  (All this is the part of the
                          //    setup that is done automatically when you build your
                          //    interface from JCMPanels.)
      
      Controller gc = new Controller();   // This controller will listen for changes
      input.setOnUserAction(gc);          //   in the function definition.
      graphIt.setOnUserAction(gc);
      
      gc.add(input);   // I have to add the ExpressionInput to a Controller, since the
                       //   function doesn't actually change unless the ExpressionInput
                       //   is checked by a Controller.
      gc.add(graph);   // The graph needs to be recomputed when the function changes.
      gc.add(cc);      // You can add one Controller to another.  Here, gc will call
                       //   on cc to do all its checks and computations, in addition to.
                       //   recomputing the graph.
      
      gc.setErrorReporter(canvas);      // Set error reporters for the Controller.
                                        //   This error reporter is also used by
                                        //   cc, which has been added as a subcontroller
                                        //   to gc.  So, it's not necessary to set a separate
                                        //   error reporter for cc

      limits.setErrorReporter(canvas);  // Error reporter for the LimitControlPanel.
      
   // end init()

// end class SimpleGraph3

           
       
jcm1-source.zip( 532 k)
Related examples in the same category
1. Math Function ChartMath Function Chart
2. Draw Your Own Contour FunctionDraw Your Own Contour Function
3. Draw Math Function Your OwnDraw Math Function Your Own
4. Draw Math Function In CoordinateDraw Math Function In Coordinate
5. ContouringContouring
6. Display the graph of a single function of one variableDisplay the graph of a single function of one variable
7. DerivativesDerivatives
8. Function CompositionFunction Composition
9. Draw the functionDraw the function
10. Draw math function on the coordinateDraw math function on the coordinate
11. Calculate the arithmetic functionCalculate the arithmetic function
12. Math function and barMath function and bar
13. Science Parser
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.