MyCanvas


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: MyCanvas

  1. #1
    Join Date
    Aug 2005
    Posts
    14

    MyCanvas

    the compiler is complaining about the file name. i want my applet to create a canvas and draw on it. i want two cycles of sine to be displayed. i want the waveform to be centered at the origin


    import java.applet.*;
    import java.awt.*;

    /*
    <applet code="CanvasDemo" width = 501 height = 200>
    </applet>
    */

    class MyCanvas extends Canvas {

    public void paint (Graphics g) {

    // Draw axes
    g.setColor(Color.lightGray);
    Dimension d = getSize();
    g.drawRect(0, 0, d.width - 1, d.height - 1);
    g.drawLine(0, d.height/2, d.width, d.height/2);
    g.drawLine(d.width/2, 0, d.width/2,d.height - 1);

    // Draw waveform
    g.setColor(Color.blue);
    double dx = 4 * Math.PI / d.width;
    double x = -2 * Math.PI;

    int h = d.height;
    for(int i = 0; i < d.width - 1; i++) {
    int y1 = (int) ((h - h * Math.sin(x)) / 2);
    int y2 = (int) ((h - h * Math.sin(x + dx)) / 2);
    g.drawLine(i, y1, i + 1, y2);
    x += dx;
    }
    }
    }

    public class CanvasDemo extends Applet {

    public void init() {
    MyCanvas myCanvas = new MyCanvas();
    myCanvas.setSize(401, 150);
    add(myCanvas);
    }
    }

  2. #2
    Join Date
    Aug 2003
    Posts
    313
    If the compiler is complaining about the file name, what is the file name? It looks like it should be CanvasDemo.java.
    ~evlich

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center