Skip to main content

Example of Canvas in AWT

Canvas

import java.applet.*;
import java.awt.*;
/*<applet code="CanvasDemo" width=500 height=200></applet>*/
class Canvas1 extends Canvas
{
public void paint(Graphics g)
{
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);
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()
{
Canvas1 c1=new Canvas1();
c1.setSize(401,501);
add(c1);
}
}

Comments

Popular posts from this blog

What is Servet ?

A servlet is a java class that extends an application hosted on a web server. Handles the HTTP request-response process (for our purposes) Often thought of as an applet that runs on a server. Provides a component base architecture for web development, using the Java Platform The foundation for Java Server Pages (JSP). Alternative to CGI scripting and platform specific server side applications.

Compact Strings -  Java 9 Feature

Motivation The current implementation of the String class stores characters in a char array, using two bytes (sixteen bits) for each character. Data gathered from many different applications indicates that strings are a major component of heap usage and, moreover, that most String objects contain only Latin-1 characters. Such characters require only one byte of storage, hence half of the space in the internal char arrays of such String objects is going unused Description We propose to change the internal representation of the String class from a UTF-16 char array to a byte array plus an encoding-flag field. The new String class will store characters encoded either as ISO-8859-1/Latin-1 (one byte per character), or as UTF-16 (two bytes per character), based upon the contents of the string. The encoding flag will indicate which encoding is used. String-related classes such as AbstractStringBuilder, StringBuilder, and StringBuffer will be updated to use the same representation, as wi...

46 to 50 Java Questions

46. What is Constructor? A constructor is a special method whose task is to initialize the object of its class. It is special because its name is the same as the class name. They do not have return types, not even void and therefore they cannot return values. They cannot be inherited, though a derived class can call the base class constructor. Constructor is invoked whenever an object of its associated class is created. 47. What is an Iterator ? The Iterator interface is used to step through the elements of a Collection. Iterators let you process each element of a Collection. Iterators are a generic way to go through all the elements of a Collection no matter how it is organized. Iterator is an Interface implemented a different way for every Collection. 48. What is the List interface? The List interface provides support for ordered collections of objects. Lists may contain duplicate elements. 49. What is memory leak? A memory leak is where an unreferenced object that will ne...