Hi all...
I have done my JApplet Application,That run and perfectely.Then now i want code optimization. I give the here my application code.. Please can you help me any one...
My application Code....
========================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class appletshape extends JApplet {
JComboBox colorChoice, figureChoice; // Pop-up menus, defined as instance
public void init() {
setBackground(Color.gray);
getContentPane().setBackground(Color.gray);
Display canvas = new Display(); // The drawing area.
getContentPane().add(canvas, BorderLayout.CENTER);
JPanel buttonBar = new JPanel(); // A panel to hold the buttons.
buttonBar.setBackground(Color.gray);
getContentPane().add(buttonBar, BorderLayout.SOUTH);
JPanel choiceBar = new JPanel(); // A panel to hole the pop-up menus
choiceBar.setBackground(Color.gray);
getContentPane().add(choiceBar, BorderLayout.NORTH);
JButton fill = new JButton("Set Background"); // The first button.
fill.addActionListener(canvas);
buttonBar.add(fill);
JButton clear = new JButton("Clear"); // The second button.
clear.addActionListener(canvas);
buttonBar.add(clear);
colorChoice = new JComboBox(); // The pop-up menu of colors.
colorChoice.addItem("Black");
colorChoice.addItem("Red");
colorChoice.addItem("Green");
colorChoice.addItem("Blue");
colorChoice.addItem("Cyan");
colorChoice.addItem("Magenta");
colorChoice.addItem("Yellow");
colorChoice.addItem("White");
colorChoice.setBackground(Color.white);
choiceBar.add(colorChoice);
figureChoice = new JComboBox(); // The pop-up menu of shapes.
figureChoice.addItem("Curve");
figureChoice.addItem("Straight Line");
figureChoice.addItem("Rectangle");
figureChoice.addItem("Oval");
figureChoice.addItem("RoundRect");
figureChoice.addItem("Filled Rectangle");
figureChoice.addItem("Filled Oval");
figureChoice.addItem("Filled RoundRect");
figureChoice.setBackground(Color.white);
choiceBar.add(figureChoice);
}
public Insets getInsets() {
return new Insets(3, 3, 3, 3);
}
private class Display extends JPanel implements MouseListener,
MouseMotionListener, ActionListener {
private final static int BLACK = 0, RED = 1, // Some constants to
GREEN = 2, BLUE = 3, CYAN = 4, MAGENTA = 5, YELLOW = 6,
WHITE = 7;
private final static int CURVE = 0, LINE = 1, RECT = 2, // Some
OVAL = 3, ROUNDRECT = 4, FILLED_RECT = 5, FILLED_OVAL = 6,
FILLED_ROUNDRECT = 7;
Image OSI;
int widthOfOSI, heightOfOSI;
private int mouseX, mouseY;
private int prevX, prevY;
private int startX, startY;
private boolean dragging;
private int figure;
private Graphics dragGraphics;
private Color dragColor;
Display() {
addMouseListener(this);
addMouseMotionListener(this);
setBackground(Color.white);
}
private void drawFigure(Graphics g, int shape, int x1, int y1, int x2,
int y2) {
if (shape == LINE) {
g.drawLine(x1, y1, x2, y2);
return;
}
int x, y;
int w, h;
if (x1 >= x2) { // x2 is left edge
x = x2;
w = x1 - x2;
} else { // x1 is left edge
x = x1;
w = x2 - x1;
}
if (y1 >= y2) { // y2 is top edge
y = y2;
h = y1 - y2;
} else { // y1 is top edge.
y = y1;
h = y2 - y1;
}
switch (shape) { // Draw the appropriate figure.
case RECT:
g.drawRect(x, y, w, h);
break;
case OVAL:
g.drawOval(x, y, w, h);
break;
case ROUNDRECT:
g.drawRoundRect(x, y, w, h, 20, 20);
break;
case FILLED_RECT:
g.fillRect(x, y, w, h);
break;
case FILLED_OVAL:
g.fillOval(x, y, w, h);
break;
case FILLED_ROUNDRECT:
g.fillRoundRect(x, y, w, h, 20, 20);
break;
}
}
private void repaintRect(int x1, int y1, int x2, int y2) {
int x, y;
int w, h;
// figure
if (x2 >= x1) { // x1 is left edge
x = x1;
w = x2 - x1;
} else { // x2 is left edge
x = x2;
w = x1 - x2;
}
if (y2 >= y1) { // y1 is top edge
y = y1;
h = y2 - y1;
} else { // y2 is top edge.
y = y2;
h = y1 - y2;
}
repaint(x, y, w + 1, h + 1);
}
private void checkOSI() {
if (OSI == null || widthOfOSI != getSize().width
|| heightOfOSI != getSize().height) {
OSI = null;
OSI = createImage(getSize().width, getSize().height);
widthOfOSI = getSize().width;
heightOfOSI = getSize().height;
Graphics OSG = OSI.getGraphics(); // Graphics context for
OSG.setColor(getBackground());
OSG.fillRect(0, 0, widthOfOSI, heightOfOSI);
OSG.dispose();
}
}
public void paintComponent(Graphics g) {
checkOSI();
g.drawImage(OSI, 0, 0, this);
if (dragging && figure != CURVE) {
g.setColor(dragColor);
drawFigure(g, figure, startX, startY, mouseX, mouseY);
}
}
public void actionPerformed(ActionEvent evt) {
.
String command = evt.getActionCommand();
checkOSI();
if (command.equals("Set Background")) {
setBackground(getCurrentColor());
if (colorChoice.getSelectedIndex() == BLACK)
colorChoice.setSelectedIndex(WHITE);
else
colorChoice.setSelectedIndex(BLACK);
}
Graphics g = OSI.getGraphics();
g.setColor(getBackground());
g.fillRect(0, 0, getSize().width, getSize().height);
g.dispose();
repaint();
}private Color getCurrentColor() {
int currentColor = colorChoice.getSelectedIndex();
switch (currentColor) {
case BLACK:
return Color.black;
case RED:
return Color.red;
case GREEN:
return Color.green;
case BLUE:
return Color.blue;
case CYAN:
return Color.cyan;
case MAGENTA:
return Color.magenta;
case YELLOW:
return Color.yellow;
default:
return Color.white;
}
}
public void mousePressed(MouseEvent evt) {
if (dragging == true) // Ignore mouse presses that occur
return; // when user is already drawing a curve.
prevX = startX = evt.getX(); // Save mouse coordinates.
prevY = startY = evt.getY();
figure = figureChoice.getSelectedIndex();
dragColor = getCurrentColor();
dragGraphics = OSI.getGraphics();
dragGraphics.setColor(dragColor);
dragging = true; // Start drawing.
} // end mousePressed()
public void mouseReleased(MouseEvent evt) {
if (dragging == false)
return; // Nothing to do because the user isn't drawing.
dragging = false;
mouseX = evt.getX();
mouseY = evt.getY();
if (figure == CURVE) {
// A CURVE is drawn as a series of LINEs
drawFigure(dragGraphics, LINE, prevX, prevY, mouseX, mouseY);
repaintRect(prevX, prevY, mouseX, mouseY);
} else if (figure == LINE) {
repaintRect(startX, startY, prevX, prevY);
if (mouseX != startX || mouseY != startY) {
// Draw the line only if it has non-zero length.
drawFigure(dragGraphics, figure, startX, startY, mouseX,
mouseY);
repaintRect(startX, startY, mouseX, mouseY);
}
} else {
repaintRect(startX, startY, prevX, prevY);
if (mouseX != startX && mouseY != startY) {
drawFigure(dragGraphics, figure, startX, startY, mouseX,
mouseY);
repaintRect(startX, startY, mouseX, mouseY);
}
}
dragGraphics.dispose();
dragGraphics = null;
}
public void mouseDragged(MouseEvent evt) {
if (dragging == false)
return;
mouseX = evt.getX();
mouseY = evt.getY();
if (figure == CURVE) {
drawFigure(dragGraphics, LINE, prevX, prevY, mouseX, mouseY);
repaintRect(prevX, prevY, mouseX, mouseY);
} else {
repaintRect(startX, startY, prevX, prevY);
repaintRect(startX, startY, mouseX, mouseY);
}
prevX = mouseX;
prevY = mouseY;
}
public void mouseEntered(MouseEvent evt) {
}
public void mouseExited(MouseEvent evt) {
}
public void mouseClicked(MouseEvent evt) {
}
public void mouseMoved(MouseEvent evt) {
}
}
}
===================================================
Thanks in Advance
Arjun Palanichamy.
Senior
Java Programmer.
chennai.