This is a simple graphics program in the Java Language This knol discusses writing a simple graphics program in the Java Language.
/**
*
* @author Beau
*/
public class SimpleApplet extends Applet {
int i,j;
/**
* Initialization method that will be called after the applet is loaded
* into the browser.
*/
@Override
public void paint(Graphics g) {
// TODO start asynchronous download of heavy resources
g.setColor(Color.red);
//g.drawString("A Simple Applet", 20, 20);
g.drawRect(20, 20, 200, 200);
g.fillRect(20, 20, 200, 200);
g.setColor(Color.green);
g.drawRect(120, 60, 200, 200);
g.fillRect(120, 60, 200, 200);
g.setColor(Color.blue);
g.drawRect(60, 80, 200, 200);
g.fillRect(60, 80, 200, 200);
g.setColor(Color.magenta);
g.drawOval(200, 40, 70, 100);
g.fillOval(200, 40, 70, 100);
}
The graphics package is very intuitive in the Java language. You can create simple to complex graphics using it. This program generates some simple graphical structures such as circles, rectangles, ovals and squares. The output is given below.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.awt.*;
import java.applet.*;
import java.awt.Graphics;
import java.applet.*;
import java.awt.Graphics;
/**
*
* @author Beau
*/
public class SimpleApplet extends Applet {
int i,j;
/**
* Initialization method that will be called after the applet is loaded
* into the browser.
*/
@Override
public void paint(Graphics g) {
// TODO start asynchronous download of heavy resources
g.setColor(Color.red);
//g.drawString("A Simple Applet", 20, 20);
g.drawRect(20, 20, 200, 200);
g.fillRect(20, 20, 200, 200);
g.setColor(Color.green);
g.drawRect(120, 60, 200, 200);
g.fillRect(120, 60, 200, 200);
g.setColor(Color.blue);
g.drawRect(60, 80, 200, 200);
g.fillRect(60, 80, 200, 200);
g.setColor(Color.magenta);
g.drawOval(200, 40, 70, 100);
g.fillOval(200, 40, 70, 100);
}
// TODO overwrite start(), stop() and destroy() methods
}
}
![]() |
| Output of the Above Program |

No comments:
Post a Comment