Showing posts with label Simple Application Program. Show all posts
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class ToolBarDemo extends JPanel
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class ToolBarDemo extends JPanel
{
public static void main(String[] args)
public static void main(String[] args)
{
JFrame window = new JFrame("ToolBarDemo");
ToolBarDemo content = new ToolBarDemo();
window.setContentPane(content);
window.pack();
window.setResizable(false);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
window.setLocation( (screenSize.width - window.getWidth())/2,
(screenSize.height - window.getHeight())/2 );
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setVisible(true);
}
private class Display extends JPanel implements MouseListener, MouseMotionListener
JFrame window = new JFrame("ToolBarDemo");
ToolBarDemo content = new ToolBarDemo();
window.setContentPane(content);
window.pack();
window.setResizable(false);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
window.setLocation( (screenSize.width - window.getWidth())/2,
(screenSize.height - window.getHeight())/2 );
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setVisible(true);
}
private class Display extends JPanel implements MouseListener, MouseMotionListener
{
private BufferedImage OSC; // Off-screen canvas.
private Color currentColor = Color.RED; // Current drawing color.
private int prevX, prevY; // Previous mouse position, during mouse drags.
private BasicStroke stroke; // Stroke used for drawing.
Display() { // constructor.
addMouseListener(this);
addMouseMotionListener(this);
setPreferredSize(new Dimension(300,300));
stroke = new BasicStroke(3,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
}
void setCurrentColor(Color c) { // change current drawing color
currentColor = c;
}
void clear() { // clear the drawing area by filling it with white
if (OSC != null) {
Graphics g = OSC.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,getWidth(),getHeight());
g.dispose();
repaint();
}
}
public void paintComponent(Graphics g) { // just copies OSC to screen
checkImage();
g.drawImage(OSC,0,0,null);
}
void checkImage() { // create or resize OSC if necessary
if (OSC == null) {
// Create the OSC, with a size to match the size of the panel.
OSC = new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_RGB);
clear();
}
else if (OSC.getWidth() != getWidth() || OSC.getHeight() != getHeight()) {
BufferedImage newOSC;
newOSC = new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics g = newOSC.getGraphics();
g.drawImage(OSC,0,0,getWidth(),getHeight(),null);
g.dispose();
OSC = newOSC;
}
}
public void mousePressed(MouseEvent e) {
prevX = e.getX();
prevY = e.getY();
}
public void mouseDragged(MouseEvent e) {
Graphics2D g2 = (Graphics2D)OSC.getGraphics();
g2.setColor(currentColor);
g2.setStroke(stroke);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawLine(prevX,prevY,e.getX(),e.getY());
g2.dispose();
repaint();
prevX = e.getX();
prevY = e.getY();
}
public void mouseReleased(MouseEvent e) { }
public void mouseMoved(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
}
private Display display;
private BufferedImage OSC; // Off-screen canvas.
private Color currentColor = Color.RED; // Current drawing color.
private int prevX, prevY; // Previous mouse position, during mouse drags.
private BasicStroke stroke; // Stroke used for drawing.
Display() { // constructor.
addMouseListener(this);
addMouseMotionListener(this);
setPreferredSize(new Dimension(300,300));
stroke = new BasicStroke(3,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
}
void setCurrentColor(Color c) { // change current drawing color
currentColor = c;
}
void clear() { // clear the drawing area by filling it with white
if (OSC != null) {
Graphics g = OSC.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,getWidth(),getHeight());
g.dispose();
repaint();
}
}
public void paintComponent(Graphics g) { // just copies OSC to screen
checkImage();
g.drawImage(OSC,0,0,null);
}
void checkImage() { // create or resize OSC if necessary
if (OSC == null) {
// Create the OSC, with a size to match the size of the panel.
OSC = new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_RGB);
clear();
}
else if (OSC.getWidth() != getWidth() || OSC.getHeight() != getHeight()) {
BufferedImage newOSC;
newOSC = new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics g = newOSC.getGraphics();
g.drawImage(OSC,0,0,getWidth(),getHeight(),null);
g.dispose();
OSC = newOSC;
}
}
public void mousePressed(MouseEvent e) {
prevX = e.getX();
prevY = e.getY();
}
public void mouseDragged(MouseEvent e) {
Graphics2D g2 = (Graphics2D)OSC.getGraphics();
g2.setColor(currentColor);
g2.setStroke(stroke);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawLine(prevX,prevY,e.getX(),e.getY());
g2.dispose();
repaint();
prevX = e.getX();
prevY = e.getY();
}
public void mouseReleased(MouseEvent e) { }
public void mouseMoved(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
}
private Display display;
public ToolBarDemo() {
setLayout(new BorderLayout(2,2));
setBackground(Color.GRAY);
setBorder(BorderFactory.createLineBorder(Color.GRAY,2));
display = new Display();
add(display, BorderLayout.CENTER);
JToolBar toolbar = new JToolBar();
add(toolbar, BorderLayout.NORTH);
ButtonGroup group = new ButtonGroup();
toolbar.add( makeColorRadioButton(Color.RED,group,true) );
toolbar.add( makeColorRadioButton(Color.GREEN,group,false) );
toolbar.add( makeColorRadioButton(Color.BLUE,group,false) );
toolbar.addSeparator(new Dimension(20,20));
toolbar.add( makeClearButton() );
}
private JRadioButton makeColorRadioButton(final Color c, ButtonGroup grp, boolean selected) {
BufferedImage image = new BufferedImage(30,30,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0,0,30,30);
g.setColor(c);
g.fill3DRect(1, 1, 24, 24, true);
g.dispose();
Icon unselectedIcon = new ImageIcon(image);
/* Create an ImageIcon for the selected state of the button. */
image = new BufferedImage(30,30,BufferedImage.TYPE_INT_RGB);
g = image.getGraphics();
g.setColor(Color.DARK_GRAY);
g.fillRect(0,0,30,30);
g.setColor(c);
g.fill3DRect(3, 3, 24, 24, false);
g.dispose();
Icon selectedIcon = new ImageIcon(image);
/* Create and configure the button. */
JRadioButton button = new JRadioButton(unselectedIcon);
button.setSelectedIcon(selectedIcon);
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
// The action for this button sets the current drawing color
// in the display to c.
display.setCurrentColor(c);
}
});
grp.add(button);
if (selected)
button.setSelected(true);
return button;
} // end makeColorRadioButton
private JButton makeClearButton() {
BufferedImage image = new BufferedImage(24,24,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)image.getGraphics();
g2.setColor(Color.LIGHT_GRAY);
g2.fillRect(0,0,24,24);
g2.setStroke( new BasicStroke(3));
g2.setColor(Color.BLACK);
g2.drawLine(4,4,20,20);
g2.drawLine(4,20,20,4);
g2.dispose();
Icon clearIcon = new ImageIcon(image);
Action clearAction = new AbstractAction(null,clearIcon) {
public void actionPerformed(ActionEvent evt) {
display.clear();
}
};
clearAction.putValue(Action.SHORT_DESCRIPTION, "Clear the Display");
JButton button = new JButton(clearAction);
return button;
}
}
setLayout(new BorderLayout(2,2));
setBackground(Color.GRAY);
setBorder(BorderFactory.createLineBorder(Color.GRAY,2));
display = new Display();
add(display, BorderLayout.CENTER);
JToolBar toolbar = new JToolBar();
add(toolbar, BorderLayout.NORTH);
ButtonGroup group = new ButtonGroup();
toolbar.add( makeColorRadioButton(Color.RED,group,true) );
toolbar.add( makeColorRadioButton(Color.GREEN,group,false) );
toolbar.add( makeColorRadioButton(Color.BLUE,group,false) );
toolbar.addSeparator(new Dimension(20,20));
toolbar.add( makeClearButton() );
}
private JRadioButton makeColorRadioButton(final Color c, ButtonGroup grp, boolean selected) {
BufferedImage image = new BufferedImage(30,30,BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0,0,30,30);
g.setColor(c);
g.fill3DRect(1, 1, 24, 24, true);
g.dispose();
Icon unselectedIcon = new ImageIcon(image);
/* Create an ImageIcon for the selected state of the button. */
image = new BufferedImage(30,30,BufferedImage.TYPE_INT_RGB);
g = image.getGraphics();
g.setColor(Color.DARK_GRAY);
g.fillRect(0,0,30,30);
g.setColor(c);
g.fill3DRect(3, 3, 24, 24, false);
g.dispose();
Icon selectedIcon = new ImageIcon(image);
/* Create and configure the button. */
JRadioButton button = new JRadioButton(unselectedIcon);
button.setSelectedIcon(selectedIcon);
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
// The action for this button sets the current drawing color
// in the display to c.
display.setCurrentColor(c);
}
});
grp.add(button);
if (selected)
button.setSelected(true);
return button;
} // end makeColorRadioButton
private JButton makeClearButton() {
BufferedImage image = new BufferedImage(24,24,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D)image.getGraphics();
g2.setColor(Color.LIGHT_GRAY);
g2.fillRect(0,0,24,24);
g2.setStroke( new BasicStroke(3));
g2.setColor(Color.BLACK);
g2.drawLine(4,4,20,20);
g2.drawLine(4,20,20,4);
g2.dispose();
Icon clearIcon = new ImageIcon(image);
Action clearAction = new AbstractAction(null,clearIcon) {
public void actionPerformed(ActionEvent evt) {
display.clear();
}
};
clearAction.putValue(Action.SHORT_DESCRIPTION, "Clear the Display");
JButton button = new JButton(clearAction);
return button;
}
}
Output:
package vijay;
/**
*
* @author VIJAY
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SubKillerWithScore extends JPanel
/**
*
* @author VIJAY
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SubKillerWithScore extends JPanel
{
public static void main(String[] args)
public static void main(String[] args)
{
JFrame window = new JFrame("Sub Killer Game");
SubKillerWithScore content = new SubKillerWithScore();
window.setContentPane(content);
window.setSize(600, 480);
window.setLocation(100,100);
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setResizable(false); // User can't change the window's size.
window.setVisible(true);
}
//------------------------------------------------------------------------
private Timer timer; // Timer that drives the animation.
private int width, height;
private Boat boat; // The boat, bomb, and sub objects are defined
private Bomb bomb; // by nested classes Boat, Bomb, and Submarine,
private Submarine sub; // which are defined later in this class.
private int hits; // The number of times the user has hit the sub.
private int misses; // The number of times the user has missed the sub.
private Font infoFont = new Font("Monospaced", Font.PLAIN, 16);
// A font for displaying the numbers of hits and misses.
JFrame window = new JFrame("Sub Killer Game");
SubKillerWithScore content = new SubKillerWithScore();
window.setContentPane(content);
window.setSize(600, 480);
window.setLocation(100,100);
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setResizable(false); // User can't change the window's size.
window.setVisible(true);
}
//------------------------------------------------------------------------
private Timer timer; // Timer that drives the animation.
private int width, height;
private Boat boat; // The boat, bomb, and sub objects are defined
private Bomb bomb; // by nested classes Boat, Bomb, and Submarine,
private Submarine sub; // which are defined later in this class.
private int hits; // The number of times the user has hit the sub.
private int misses; // The number of times the user has missed the sub.
private Font infoFont = new Font("Monospaced", Font.PLAIN, 16);
// A font for displaying the numbers of hits and misses.
public SubKillerWithScore()
{
setBackground( new Color(0,200,0) );
ActionListener action = new ActionListener()
setBackground( new Color(0,200,0) );
ActionListener action = new ActionListener()
{
// Defines the action taken each time the timer fires.
public void actionPerformed(ActionEvent evt)
// Defines the action taken each time the timer fires.
public void actionPerformed(ActionEvent evt)
{
if (boat != null)
if (boat != null)
{
boat.updateForNewFrame();
bomb.updateForNewFrame();
sub.updateForNewFrame();
}
repaint();
}
};
timer = new Timer( 30, action ); // Fires every 30 milliseconds.
addMouseListener( new MouseAdapter()
boat.updateForNewFrame();
bomb.updateForNewFrame();
sub.updateForNewFrame();
}
repaint();
}
};
timer = new Timer( 30, action ); // Fires every 30 milliseconds.
addMouseListener( new MouseAdapter()
{
// The mouse listener simply requests focus when the user
// clicks the panel.
public void mousePressed(MouseEvent evt)
// The mouse listener simply requests focus when the user
// clicks the panel.
public void mousePressed(MouseEvent evt)
{
requestFocus();
}
} );
addFocusListener( new FocusListener()
requestFocus();
}
} );
addFocusListener( new FocusListener()
{
public void focusGained(FocusEvent evt)
public void focusGained(FocusEvent evt)
{
timer.start();
repaint();
}
public void focusLost(FocusEvent evt)
timer.start();
repaint();
}
public void focusLost(FocusEvent evt)
{
timer.stop();
repaint();
}
} );
addKeyListener( new KeyAdapter()
timer.stop();
repaint();
}
} );
addKeyListener( new KeyAdapter()
{
public void keyPressed(KeyEvent evt)
public void keyPressed(KeyEvent evt)
{
int code = evt.getKeyCode();
if (code == KeyEvent.VK_LEFT)
int code = evt.getKeyCode();
if (code == KeyEvent.VK_LEFT)
{
boat.centerX -= 15;
}
else if (code == KeyEvent.VK_RIGHT)
boat.centerX -= 15;
}
else if (code == KeyEvent.VK_RIGHT)
{
boat.centerX += 15;
}
else if (code == KeyEvent.VK_DOWN)
boat.centerX += 15;
}
else if (code == KeyEvent.VK_DOWN)
{
if ( bomb.isFalling == false )
bomb.isFalling = true;
}
}
} );
} // end constructor
public void paintComponent(Graphics g) {
super.paintComponent(g); // Fill panel with background color, green.
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (boat == null) {
width = getWidth();
height = getHeight();
boat = new Boat();
sub = new Submarine();
bomb = new Bomb();
}
if (hasFocus())
g.setColor(Color.CYAN);
else {
g.setColor(Color.BLACK);
g.drawString("CLICK TO ACTIVATE", 20, height - 10);
g.setColor(Color.GRAY);
}
g.drawRect(0,0,width-1,height-1); // Draw a 3-pixel border.
g.drawRect(1,1,width-3,height-3);
g.drawRect(2,2,width-5,height-5);
boat.draw(g);
sub.draw(g);
bomb.draw(g);
g.setFont(infoFont);
g.setColor(Color.BLACK);
g.drawString("Number of hits: " + hits, 15, 24);
g.drawString("Number of misses: " + misses, 15, 45);
} // end paintComponent()
if ( bomb.isFalling == false )
bomb.isFalling = true;
}
}
} );
} // end constructor
public void paintComponent(Graphics g) {
super.paintComponent(g); // Fill panel with background color, green.
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if (boat == null) {
width = getWidth();
height = getHeight();
boat = new Boat();
sub = new Submarine();
bomb = new Bomb();
}
if (hasFocus())
g.setColor(Color.CYAN);
else {
g.setColor(Color.BLACK);
g.drawString("CLICK TO ACTIVATE", 20, height - 10);
g.setColor(Color.GRAY);
}
g.drawRect(0,0,width-1,height-1); // Draw a 3-pixel border.
g.drawRect(1,1,width-3,height-3);
g.drawRect(2,2,width-5,height-5);
boat.draw(g);
sub.draw(g);
bomb.draw(g);
g.setFont(infoFont);
g.setColor(Color.BLACK);
g.drawString("Number of hits: " + hits, 15, 24);
g.drawString("Number of misses: " + misses, 15, 45);
} // end paintComponent()
private class Boat {
int centerX, centerY; // Current position of the center of the boat.
Boat() { // Constructor centers the boat horizontally, 80 pixels from top.
centerX = width/2;
centerY = 80;
}
void updateForNewFrame() { // Makes sure boat has not moved off screen.
if (centerX < 0)
centerX = 0;
else if (centerX > width)
centerX = width;
}
void draw(Graphics g) { // Draws the boat at its current location.
g.setColor(Color.BLUE);
g.fillRoundRect(centerX - 40, centerY - 20, 80, 40, 20, 20);
}
} // end nested class Boat
int centerX, centerY; // Current position of the center of the boat.
Boat() { // Constructor centers the boat horizontally, 80 pixels from top.
centerX = width/2;
centerY = 80;
}
void updateForNewFrame() { // Makes sure boat has not moved off screen.
if (centerX < 0)
centerX = 0;
else if (centerX > width)
centerX = width;
}
void draw(Graphics g) { // Draws the boat at its current location.
g.setColor(Color.BLUE);
g.fillRoundRect(centerX - 40, centerY - 20, 80, 40, 20, 20);
}
} // end nested class Boat
private class Bomb {
int centerX, centerY; // Current position of the center of the bomb.
boolean isFalling;
Bomb() { // Constructor creates a bomb that is initially attached to boat.
isFalling = false;
}
void updateForNewFrame() { // If bomb is falling, take appropriate action.
if (isFalling) {
if (centerY > height) {
isFalling = false;
misses++; // USER HAS MISSED THE SUB
}
else if (Math.abs(centerX - sub.centerX) <= 36 &&
Math.abs(centerY - sub.centerY) <= 21) {
sub.isExploding = true;
sub.explosionFrameNumber = 1;
isFalling = false; // Bomb reappears on the boat.
hits++; // USER HAS HIT THE SUB
}
else {
centerY += 10;
}
}
}
void draw(Graphics g) { // Draw the bomb.
if ( ! isFalling ) {
centerX = boat.centerX;
centerY = boat.centerY + 23;
}
g.setColor(Color.RED);
g.fillOval(centerX - 8, centerY - 8, 16, 16);
}
} // end nested class Bomb
private class Submarine {
int centerX, centerY; // Current position of the center of the sub.
boolean isMovingLeft; // Tells whether the sub is moving left or right
boolean isExploding; // Set to true when the sub is hit by the bomb.
int explosionFrameNumber;
Submarine() { // Create the sub at a random location 40 pixels from bottom.
centerX = (int)(width*Math.random());
centerY = height - 40;
isExploding = false;
isMovingLeft = (Math.random() < 0.5);
}
void updateForNewFrame() { // Move sub or increase explosionFrameNumber.
if (isExploding) {
explosionFrameNumber++;
if (explosionFrameNumber == 15) {
centerX = (int)(width*Math.random());
centerY = height - 40;
isExploding = false;
isMovingLeft = (Math.random() < 0.5);
}
}
else { // Move the sub.
if (Math.random() < 0.04) {
isMovingLeft = ! isMovingLeft;
}
if (isMovingLeft) {
centerX -= 5;
if (centerX <= 0) {
centerX = 0;
isMovingLeft = false;
}
}
else {
centerX += 5;
if (centerX > width) {
centerX = width;
isMovingLeft = true;
}
}
}
}
void draw(Graphics g) { // Draw sub and, if it is exploding, the explosion.
g.setColor(Color.BLACK);
g.fillOval(centerX - 30, centerY - 15, 60, 30);
if (isExploding) {
g.setColor(Color.YELLOW);
g.fillOval(centerX - 4*explosionFrameNumber,
centerY - 2*explosionFrameNumber,
8*explosionFrameNumber,
4*explosionFrameNumber);
g.setColor(Color.RED);
g.fillOval(centerX - 2*explosionFrameNumber,
centerY - explosionFrameNumber/2,
4*explosionFrameNumber,
explosionFrameNumber);
}
}
} // end nested class Submarine
} // end class SubKiller
Output:
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class PopupCalculatorApplet extends JApplet implements ActionListener
import java.awt.Button;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class PopupCalculatorApplet extends JApplet implements ActionListener
{
public void init()
public void init()
{
Button calcButton = new Button("Calculator");
calcButton.addActionListener(this);
Container contentPane = getContentPane();
contentPane.add(calcButton);
}
public void actionPerformed(ActionEvent evt)
Button calcButton = new Button("Calculator");
calcButton.addActionListener(this);
Container contentPane = getContentPane();
contentPane.add(calcButton);
}
public void actionPerformed(ActionEvent evt)
{
if (calc.isVisible())
calc.setVisible(false);
else
calc.show();
}
private JFrame calc = new CalculatorFrame();
}
class CalculatorPanel extends JPanel implements ActionListener
if (calc.isVisible())
calc.setVisible(false);
else
calc.show();
}
private JFrame calc = new CalculatorFrame();
}
class CalculatorPanel extends JPanel implements ActionListener
{
public CalculatorPanel()
public CalculatorPanel()
{
setLayout(new BorderLayout());
display = new JTextField("0");
display.setEditable(false);
add(display, "North");
JPanel p = new JPanel();
p.setLayout(new GridLayout(4, 4));
String buttons = "789/456*123-0.=+";
for (int i = 0; i < buttons.length(); i++)
addButton(p, buttons.substring(i, i + 1));
add(p, "Center");
}
private void addButton(Container c, String s)
setLayout(new BorderLayout());
display = new JTextField("0");
display.setEditable(false);
add(display, "North");
JPanel p = new JPanel();
p.setLayout(new GridLayout(4, 4));
String buttons = "789/456*123-0.=+";
for (int i = 0; i < buttons.length(); i++)
addButton(p, buttons.substring(i, i + 1));
add(p, "Center");
}
private void addButton(Container c, String s)
{
JButton b = new JButton(s);
c.add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
JButton b = new JButton(s);
c.add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
String s = evt.getActionCommand();
if ('0' <= s.charAt(0) && s.charAt(0) <= '9' || s.equals("."))
String s = evt.getActionCommand();
if ('0' <= s.charAt(0) && s.charAt(0) <= '9' || s.equals("."))
{
if (start)
display.setText(s);
else
display.setText(display.getText() + s);
start = false;
}
if (start)
display.setText(s);
else
display.setText(display.getText() + s);
start = false;
}
else
{
if (start)
if (start)
{
if (s.equals("-"))
if (s.equals("-"))
{
display.setText(s);
start = false;
} else
op = s;
}
display.setText(s);
start = false;
} else
op = s;
}
else
{
calculate(Double.parseDouble(display.getText()));
op = s;
start = true;
}
}
}
public void calculate(double n)
calculate(Double.parseDouble(display.getText()));
op = s;
start = true;
}
}
}
public void calculate(double n)
{
if (op.equals("+"))
arg += n;
else if (op.equals("-"))
arg -= n;
else if (op.equals("*"))
arg *= n;
else if (op.equals("/"))
arg /= n;
else if (op.equals("="))
arg = n;
display.setText("" + arg);
}
private JTextField display;
private double arg = 0;
private String op = "=";
private boolean start = true;
}
class CalculatorFrame extends JFrame
if (op.equals("+"))
arg += n;
else if (op.equals("-"))
arg -= n;
else if (op.equals("*"))
arg *= n;
else if (op.equals("/"))
arg /= n;
else if (op.equals("="))
arg = n;
display.setText("" + arg);
}
private JTextField display;
private double arg = 0;
private String op = "=";
private boolean start = true;
}
class CalculatorFrame extends JFrame
{
public CalculatorFrame()
public CalculatorFrame()
{
setTitle("Calculator");
setSize(200, 200);
Container contentPane = getContentPane();
contentPane.add(new CalculatorPanel());
}
}
setTitle("Calculator");
setSize(200, 200);
Container contentPane = getContentPane();
contentPane.add(new CalculatorPanel());
}
}
Output:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="Cal" width=300 height=300>
</applet>
*/
public class Cal extends Applet
implements ActionListener
{
String msg=" ";
int v1,v2,result;
TextField t1;
Button b[]=new Button[10];
Button add,sub,mul,div,clear,mod,EQ;
char OP;
public void init()
{
Color k=new Color(120,89,90);
setBackground(k);
t1=new TextField(10);
GridLayout gl=new GridLayout(4,5);
setLayout(gl);
for(int i=0;i<10;i++)
{
b[i]=new Button(""+i);
}
add=new Button("add");
sub=new Button("sub");
mul=new Button("mul");
div=new Button("div");
mod=new Button("mod");
clear=new Button("clear");
EQ=new Button("EQ");
t1.addActionListener(this);
add(t1);
for(int i=0;i<10;i++)
{
add(b[i]);
}
add(add);
add(sub);
add(mul);
add(div);
add(mod);
add(clear);
add(EQ);
for(int i=0;i<10;i++)
{
b[i].addActionListener(this);
}
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
mod.addActionListener(this);
clear.addActionListener(this);
EQ.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
char ch=str.charAt(0);
if ( Character.isDigit(ch))
t1.setText(t1.getText()+str);
else
if(str.equals("add"))
{
v1=Integer.parseInt(t1.getText());
OP='+';
t1.setText("");
}
else if(str.equals("sub"))
{
v1=Integer.parseInt(t1.getText());
OP='-';
t1.setText("");
}
else if(str.equals("mul"))
{
v1=Integer.parseInt(t1.getText());
OP='*';
t1.setText("");
}
else if(str.equals("div"))
{
v1=Integer.parseInt(t1.getText());
OP='/';
t1.setText("");
}
else if(str.equals("mod"))
{
v1=Integer.parseInt(t1.getText());
OP='%';
t1.setText("");
}
if(str.equals("EQ"))
{
v2=Integer.parseInt(t1.getText());
if(OP=='+')
result=v1+v2;
else if(OP=='-')
result=v1-v2;
else if(OP=='*')
result=v1*v2;
else if(OP=='/')
result=v1/v2;
else if(OP=='%')
result=v1%v2;
t1.setText(""+result);
}
if(str.equals("clear"))
{
t1.setText("");
}
}
}
import java.awt.event.*;
import java.applet.*;
/*
<applet code="Cal" width=300 height=300>
</applet>
*/
public class Cal extends Applet
implements ActionListener
{
String msg=" ";
int v1,v2,result;
TextField t1;
Button b[]=new Button[10];
Button add,sub,mul,div,clear,mod,EQ;
char OP;
public void init()
{
Color k=new Color(120,89,90);
setBackground(k);
t1=new TextField(10);
GridLayout gl=new GridLayout(4,5);
setLayout(gl);
for(int i=0;i<10;i++)
{
b[i]=new Button(""+i);
}
add=new Button("add");
sub=new Button("sub");
mul=new Button("mul");
div=new Button("div");
mod=new Button("mod");
clear=new Button("clear");
EQ=new Button("EQ");
t1.addActionListener(this);
add(t1);
for(int i=0;i<10;i++)
{
add(b[i]);
}
add(add);
add(sub);
add(mul);
add(div);
add(mod);
add(clear);
add(EQ);
for(int i=0;i<10;i++)
{
b[i].addActionListener(this);
}
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
mod.addActionListener(this);
clear.addActionListener(this);
EQ.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
char ch=str.charAt(0);
if ( Character.isDigit(ch))
t1.setText(t1.getText()+str);
else
if(str.equals("add"))
{
v1=Integer.parseInt(t1.getText());
OP='+';
t1.setText("");
}
else if(str.equals("sub"))
{
v1=Integer.parseInt(t1.getText());
OP='-';
t1.setText("");
}
else if(str.equals("mul"))
{
v1=Integer.parseInt(t1.getText());
OP='*';
t1.setText("");
}
else if(str.equals("div"))
{
v1=Integer.parseInt(t1.getText());
OP='/';
t1.setText("");
}
else if(str.equals("mod"))
{
v1=Integer.parseInt(t1.getText());
OP='%';
t1.setText("");
}
if(str.equals("EQ"))
{
v2=Integer.parseInt(t1.getText());
if(OP=='+')
result=v1+v2;
else if(OP=='-')
result=v1-v2;
else if(OP=='*')
result=v1*v2;
else if(OP=='/')
result=v1/v2;
else if(OP=='%')
result=v1%v2;
t1.setText(""+result);
}
if(str.equals("clear"))
{
t1.setText("");
}
}
}
Output:
This program demonstrates recursion by counting the number of squares in a "blob". The squares are arranged in a grid, and each position in the grid can be either empty or filled. A blob is defined to be a filled square and any square that can be reached from that square by moving horizontally or vertically to other filled squares. This program fills the grid randomly. If the user clicks on a filled square, all the squares in the blob that contains that square are colored red, and the number of squares in the blob is reported. The program can also count and report the number of blobs. When the user clicks a "New Blobs" button, the grid is randomly re-filled.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Blobs extends JPanel implements MouseListener, ActionListener
import java.awt.event.*;
import javax.swing.*;
public class Blobs extends JPanel implements MouseListener, ActionListener
{
public static void main(String[] args)
public static void main(String[] args)
{
JFrame window = new JFrame("Recursive Blob Counting");
window.setContentPane( new Blobs(454,400) );
window.pack();
window.setResizable(false);
window.setLocation(150,100);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
final static int SQUARE_SIZE = 9;
JFrame window = new JFrame("Recursive Blob Counting");
window.setContentPane( new Blobs(454,400) );
window.pack();
window.setResizable(false);
window.setLocation(150,100);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
final static int SQUARE_SIZE = 9;
JLabel message;
JComboBox<String> percentFill;
int rows;
int columns;
boolean[][] filled;
boolean[][] visited;
JComboBox<String> percentFill;
int rows;
int columns;
boolean[][] filled;
boolean[][] visited;
public Blobs(int width, int height) {
setLayout(null);
setBackground(new Color(220,220,255));
addMouseListener(this);
setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.BLUE));
setPreferredSize( new Dimension(width,height) );
rows = (height - 120) / SQUARE_SIZE;
columns = (width - 20) / SQUARE_SIZE;
filled = new boolean[rows][columns];
visited = new boolean[rows][columns];
for (int r = 0; r < rows; r++)
for (int c = 0; c < columns; c++)
filled[r][c] = (Math.random() < 0.3);
/* Create the components. */
message = new JLabel("Click a square to get the blob size.", JLabel.CENTER);
message.setForeground(Color.BLUE);
message.setFont(new Font("Helvetica",Font.PLAIN,14));
percentFill = new JComboBox<String>();
percentFill.addItem("10% fill");
percentFill.addItem("20% fill");
percentFill.addItem("30% fill");
percentFill.addItem("40% fill");
percentFill.addItem("50% fill");
percentFill.addItem("60% fill");
percentFill.addItem("70% fill");
percentFill.addItem("80% fill");
percentFill.addItem("90% fill");
percentFill.setBackground(Color.WHITE);
percentFill.setSelectedIndex(2);
JButton newButton = new JButton("New Blobs");
newButton.addActionListener(this);
newButton.setBackground(Color.LIGHT_GRAY);
JButton countButton = new JButton("Count the Blobs");
countButton.addActionListener(this);
countButton.setBackground(Color.LIGHT_GRAY);
/* Add the components to the panel and set their sizes and positions. */
add(message);
add(newButton);
add(percentFill);
add(countButton);
message.setBounds(15, height-100, width-30, 23);
countButton.setBounds(15, height-70, width-30, 28);
newButton.setBounds(15, height-35, (width-40)/2, 28);
percentFill.setBounds(width/2 + 5, height-35, (width-40)/2, 28);
} // end constructor
public void actionPerformed(ActionEvent evt) {
String cmd = evt.getActionCommand();
if (cmd.equals("New Blobs"))
fillGrid();
else if (cmd.equals("Count the Blobs"))
countBlobs();
}
private void fillGrid() {
double probability = (percentFill.getSelectedIndex() + 1) / 10.0;
for (int r = 0; r < rows; r++)
for (int c = 0; c < columns; c++) {
filled[r][c] = (Math.random() < probability);
visited[r][c] = false;
}
message.setText("Click a square to get the blob size.");
repaint();
}
private void countBlobs() {
int count = 0;
for (int r = 0; r < rows; r++)
for (int c = 0; c < columns; c++)
visited[r][c] = false;
for (int r = 0; r < rows; r++)
for (int c = 0; c < columns; c++) {
if (getBlobSize(r,c) > 0)
count++;
}
repaint();
message.setText("The number of blobs is " + count);
} // end countBlobs()
private int getBlobSize(int r, int c) {
if (r < 0 || r >= rows || c < 0 || c >= columns) {
return 0;
}
if (filled[r][c] == false || visited[r][c] == true) {
return 0;
}
visited[r][c] = true;
int size = 1;
size += getBlobSize(r-1,c);
size += getBlobSize(r+1,c);
size += getBlobSize(r,c-1);
size += getBlobSize(r,c+1);
return size;
} // end getBlobSize()
public void mousePressed(MouseEvent evt) {
int row = (evt.getY() - 10) / SQUARE_SIZE;
int col = (evt.getX() - 10) / SQUARE_SIZE;
if (row < 0 || row >= rows || col < 0 || col >= columns) {
message.setText("Please click on a square!");
return;
}
for (int r = 0; r < rows; r++)
for (int c = 0; c < columns; c++)
visited[r][c] = false; // Clear visited array before counting.
int size = getBlobSize(row,col);
if (size == 0)
message.setText("There is no blob at (" + row + "," + col + ").");
else if (size == 1)
message.setText("Blob at (" + row + "," + col + ") contains 1 square.");
else
message.setText("Blob at (" + row + "," + col + ") contains " + size + " squares.");
repaint();
}
public void mouseReleased(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(10, 10, columns*SQUARE_SIZE, rows*SQUARE_SIZE);
g.setColor(Color.BLACK);
for (int i = 0; i <= rows; i++)
g.drawLine(10, 10 + i*SQUARE_SIZE, columns*SQUARE_SIZE + 10, 10 + i*SQUARE_SIZE);
for (int i = 0; i <= columns; i++)
g.drawLine(10 + i*SQUARE_SIZE, 10, 10 + i*SQUARE_SIZE, rows*SQUARE_SIZE + 10);
for (int r = 0; r < rows; r++)
for (int c = 0; c < columns; c++) {
if (visited[r][c]) {
g.setColor(Color.RED);
g.fillRect(11 + c*SQUARE_SIZE, 11 + r*SQUARE_SIZE, SQUARE_SIZE - 1, SQUARE_SIZE - 1);
}
else if (filled[r][c]) {
g.setColor(Color.GRAY);
g.fillRect(11 + c*SQUARE_SIZE, 11 + r*SQUARE_SIZE, SQUARE_SIZE - 1, SQUARE_SIZE - 1);
}
}
} // end paintComponent();
} // end class Blobs
setLayout(null);
setBackground(new Color(220,220,255));
addMouseListener(this);
setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.BLUE));
setPreferredSize( new Dimension(width,height) );
rows = (height - 120) / SQUARE_SIZE;
columns = (width - 20) / SQUARE_SIZE;
filled = new boolean[rows][columns];
visited = new boolean[rows][columns];
for (int r = 0; r < rows; r++)
for (int c = 0; c < columns; c++)
filled[r][c] = (Math.random() < 0.3);
/* Create the components. */
message = new JLabel("Click a square to get the blob size.", JLabel.CENTER);
message.setForeground(Color.BLUE);
message.setFont(new Font("Helvetica",Font.PLAIN,14));
percentFill = new JComboBox<String>();
percentFill.addItem("10% fill");
percentFill.addItem("20% fill");
percentFill.addItem("30% fill");
percentFill.addItem("40% fill");
percentFill.addItem("50% fill");
percentFill.addItem("60% fill");
percentFill.addItem("70% fill");
percentFill.addItem("80% fill");
percentFill.addItem("90% fill");
percentFill.setBackground(Color.WHITE);
percentFill.setSelectedIndex(2);
JButton newButton = new JButton("New Blobs");
newButton.addActionListener(this);
newButton.setBackground(Color.LIGHT_GRAY);
JButton countButton = new JButton("Count the Blobs");
countButton.addActionListener(this);
countButton.setBackground(Color.LIGHT_GRAY);
/* Add the components to the panel and set their sizes and positions. */
add(message);
add(newButton);
add(percentFill);
add(countButton);
message.setBounds(15, height-100, width-30, 23);
countButton.setBounds(15, height-70, width-30, 28);
newButton.setBounds(15, height-35, (width-40)/2, 28);
percentFill.setBounds(width/2 + 5, height-35, (width-40)/2, 28);
} // end constructor
public void actionPerformed(ActionEvent evt) {
String cmd = evt.getActionCommand();
if (cmd.equals("New Blobs"))
fillGrid();
else if (cmd.equals("Count the Blobs"))
countBlobs();
}
private void fillGrid() {
double probability = (percentFill.getSelectedIndex() + 1) / 10.0;
for (int r = 0; r < rows; r++)
for (int c = 0; c < columns; c++) {
filled[r][c] = (Math.random() < probability);
visited[r][c] = false;
}
message.setText("Click a square to get the blob size.");
repaint();
}
private void countBlobs() {
int count = 0;
for (int r = 0; r < rows; r++)
for (int c = 0; c < columns; c++)
visited[r][c] = false;
for (int r = 0; r < rows; r++)
for (int c = 0; c < columns; c++) {
if (getBlobSize(r,c) > 0)
count++;
}
repaint();
message.setText("The number of blobs is " + count);
} // end countBlobs()
private int getBlobSize(int r, int c) {
if (r < 0 || r >= rows || c < 0 || c >= columns) {
return 0;
}
if (filled[r][c] == false || visited[r][c] == true) {
return 0;
}
visited[r][c] = true;
int size = 1;
size += getBlobSize(r-1,c);
size += getBlobSize(r+1,c);
size += getBlobSize(r,c-1);
size += getBlobSize(r,c+1);
return size;
} // end getBlobSize()
public void mousePressed(MouseEvent evt) {
int row = (evt.getY() - 10) / SQUARE_SIZE;
int col = (evt.getX() - 10) / SQUARE_SIZE;
if (row < 0 || row >= rows || col < 0 || col >= columns) {
message.setText("Please click on a square!");
return;
}
for (int r = 0; r < rows; r++)
for (int c = 0; c < columns; c++)
visited[r][c] = false; // Clear visited array before counting.
int size = getBlobSize(row,col);
if (size == 0)
message.setText("There is no blob at (" + row + "," + col + ").");
else if (size == 1)
message.setText("Blob at (" + row + "," + col + ") contains 1 square.");
else
message.setText("Blob at (" + row + "," + col + ") contains " + size + " squares.");
repaint();
}
public void mouseReleased(MouseEvent e) { }
public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(10, 10, columns*SQUARE_SIZE, rows*SQUARE_SIZE);
g.setColor(Color.BLACK);
for (int i = 0; i <= rows; i++)
g.drawLine(10, 10 + i*SQUARE_SIZE, columns*SQUARE_SIZE + 10, 10 + i*SQUARE_SIZE);
for (int i = 0; i <= columns; i++)
g.drawLine(10 + i*SQUARE_SIZE, 10, 10 + i*SQUARE_SIZE, rows*SQUARE_SIZE + 10);
for (int r = 0; r < rows; r++)
for (int c = 0; c < columns; c++) {
if (visited[r][c]) {
g.setColor(Color.RED);
g.fillRect(11 + c*SQUARE_SIZE, 11 + r*SQUARE_SIZE, SQUARE_SIZE - 1, SQUARE_SIZE - 1);
}
else if (filled[r][c]) {
g.setColor(Color.GRAY);
g.fillRect(11 + c*SQUARE_SIZE, 11 + r*SQUARE_SIZE, SQUARE_SIZE - 1, SQUARE_SIZE - 1);
}
}
} // end paintComponent();
} // end class Blobs
Output:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RandomCircles extends JPanel implements ActionListener
import java.awt.event.*;
import javax.swing.*;
public class RandomCircles extends JPanel implements ActionListener
{
public void drawFrame(Graphics g, int frameNumber, int width, int height)
public void drawFrame(Graphics g, int frameNumber, int width, int height)
{
int centerX,centerY;
int colorChoice;
int count;
for (count = 0; count < 500; count++)
int centerX,centerY;
int colorChoice;
int count;
for (count = 0; count < 500; count++)
{
colorChoice = (int)(3*Math.random());
switch (colorChoice)
colorChoice = (int)(3*Math.random());
switch (colorChoice)
{
case 0:
g.setColor(Color.RED);
break;
case 1:
g.setColor(Color.GREEN);
break;
case 2:
g.setColor(Color.BLUE);
break;
}
centerX = (int)(width*Math.random());
centerY = (int)(height*Math.random());
g.fillOval( centerX - 50, centerY - 50, 100, 100 );
g.setColor(Color.BLACK);
g.drawOval( centerX - 50, centerY - 50, 100, 100 );
}
}
public static void main(String[] args)
case 0:
g.setColor(Color.RED);
break;
case 1:
g.setColor(Color.GREEN);
break;
case 2:
g.setColor(Color.BLUE);
break;
}
centerX = (int)(width*Math.random());
centerY = (int)(height*Math.random());
g.fillOval( centerX - 50, centerY - 50, 100, 100 );
g.setColor(Color.BLACK);
g.drawOval( centerX - 50, centerY - 50, 100, 100 );
}
}
public static void main(String[] args)
{
JFrame window = new JFrame("Random Disks");
RandomCircles drawingArea = new RandomCircles();
drawingArea.setBackground(Color.WHITE);
window.setContentPane(drawingArea);
drawingArea.setPreferredSize(new Dimension(500,500));
window.pack();
window.setLocation(100,50);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
Timer frameTimer = new Timer(3000,drawingArea);
window.setVisible(true);
frameTimer.start();
}
private int frameNum;
public void actionPerformed(ActionEvent evt)
JFrame window = new JFrame("Random Disks");
RandomCircles drawingArea = new RandomCircles();
drawingArea.setBackground(Color.WHITE);
window.setContentPane(drawingArea);
drawingArea.setPreferredSize(new Dimension(500,500));
window.pack();
window.setLocation(100,50);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
Timer frameTimer = new Timer(3000,drawingArea);
window.setVisible(true);
frameTimer.start();
}
private int frameNum;
public void actionPerformed(ActionEvent evt)
{
frameNum++;
repaint();
}
protected void paintComponent(Graphics g)
frameNum++;
repaint();
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
drawFrame(g, frameNum, getWidth(), getHeight());
}
}
super.paintComponent(g);
drawFrame(g, frameNum, getWidth(), getHeight());
}
}
Output:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class InfiniteMotion extends JPanel implements ActionListener
import java.awt.event.*;
import javax.swing.*;
public class InfiniteMotion extends JPanel implements ActionListener
{
public void drawFrame(Graphics g, int frameNumber, int width, int height)
public void drawFrame(Graphics g, int frameNumber, int width, int height)
{
int inset;
int rectWidth, rectHeight;
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0,0,width,height);
g.setColor(Color.BLACK);
inset = frameNumber % 15;
rectWidth = width - 2*inset;
rectHeight = height - 2*inset;
while (rectWidth >= 0 && rectHeight >= 0)
int inset;
int rectWidth, rectHeight;
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0,0,width,height);
g.setColor(Color.BLACK);
inset = frameNumber % 15;
rectWidth = width - 2*inset;
rectHeight = height - 2*inset;
while (rectWidth >= 0 && rectHeight >= 0)
{
g.drawRect(inset, inset, rectWidth, rectHeight);
inset += 15;
rectWidth -= 30;
rectHeight -= 30;
}
}
public static void main(String[] args)
g.drawRect(inset, inset, rectWidth, rectHeight);
inset += 15;
rectWidth -= 30;
rectHeight -= 30;
}
}
public static void main(String[] args)
{
JFrame window = new JFrame("Infinite motion");
InfiniteMotion drawingArea = new InfiniteMotion();
drawingArea.setBackground(Color.WHITE);
window.setContentPane(drawingArea);
drawingArea.setPreferredSize(new Dimension(600,450));
window.pack();
window.setLocation(100,50);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(true);
Timer frameTimer = new Timer(20,drawingArea);
window.setVisible(true);
frameTimer.start();
}
private int frameNum;
public void actionPerformed(ActionEvent evt)
JFrame window = new JFrame("Infinite motion");
InfiniteMotion drawingArea = new InfiniteMotion();
drawingArea.setBackground(Color.WHITE);
window.setContentPane(drawingArea);
drawingArea.setPreferredSize(new Dimension(600,450));
window.pack();
window.setLocation(100,50);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(true);
Timer frameTimer = new Timer(20,drawingArea);
window.setVisible(true);
frameTimer.start();
}
private int frameNum;
public void actionPerformed(ActionEvent evt)
{
frameNum++;
repaint();
}
protected void paintComponent(Graphics g)
frameNum++;
repaint();
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
drawFrame(g, frameNum, getWidth(), getHeight());
}
}









