This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

How to code optimization..  RSS feed

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Optimization" generally means improving performance, and always starts with making actual measurements using a profiling tool; of course, it only makes sense to do this if you're having a problem with the program's performance. Are you? If so, have you done some measurements to find out where the bottlenecks are?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would hope that a "Senior Java programmer" knows not to name a class with lowercase letters only... Not talking about optimization at all...
 
Arjun Palanichamy
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kirill..

Sorry. Just i give easy identification use. I will change that my future programming source...

Thanks.
ARjun Palanichamy.
Chennai - India.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!