Saturday, July 7, 2007

Prime Number

/* Following method will check whether a is a prime or not */
public static boolean isPrime(int a) {
boolean prime = true;

for (int counter = 2; counter < a; counter++)
if (a % counter == 0)
prime = false;

return (prime);
}
/* Following method will return highest prime number closest to parameter n */
public static int getPrime(int n) {
int bigPrime = 0;
for (int counter = 2; counter <= n; counter++)
if (isPrime(counter))
bigPrime = counter > bigPrime ? counter : bigPrime;
return bigPrime;
}

Friday, July 6, 2007

JButton with non-rectangular shape

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class TriangleButton extends JButton {
public TriangleButton(String label) {
super(label);

// These statements enlarge the button so that it
// becomes a circle rather than an oval.
Dimension size = getPreferredSize();
size.width = size.height = Math.max(size.width,
size.height);
setPreferredSize(size);

// This call causes the JButton not to paint
// the background.
// This allows us to paint a round background.
setContentAreaFilled(false);
}

// Paint the round background and label.
protected void paintComponent(Graphics g) {
if (getModel().isArmed()) {
// You might want to make the highlight color
// a property of the RoundButton class.
g.setColor(Color.lightGray);
} else {
g.setColor(getBackground());
}

int x3Points[] = {getSize().width/2, 0, getSize().width};
int y3Points[] = {0, getSize().height, getSize().height};
g.fillPolygon(x3Points, y3Points, x3Points.length);

// This call will paint the label and the
// focus rectangle.
super.paintComponent(g);
}

// Paint the border of the button using a simple stroke.
protected void paintBorder(Graphics g) {
g.setColor(getForeground());
int x3Points[] = {getSize().width/2, 0, getSize().width};
int y3Points[] = {0, getSize().height, getSize().height};
g.drawPolygon(x3Points, y3Points, x3Points.length);
}

// Hit detection.
Polygon polygon;
public boolean contains(int x, int y) {
// If the button has changed size,
// make a new shape object.
if (polygon == null ||
!polygon.getBounds().equals(getBounds())) {
int x3Points[] = {getSize().width/2, 0, getSize().width};
int y3Points[] = {0, getSize().height, getSize().height};
polygon = new Polygon(x3Points,y3Points,3);
}
return polygon.contains(x, y);
}

// Test routine.
public static void main(String[] args) {
// Create a button with the label "Jackpot".
JButton button = new TriangleButton("Jackpot");
button.setBackground(Color.green);

// Create a frame in which to show the button.
JFrame frame = new JFrame();
frame.getContentPane().setBackground(Color.yellow);
frame.getContentPane().add(button);
frame.getContentPane().setLayout(new FlowLayout());
frame.setSize(150, 150);
frame.setVisible(true);
}
}

Wednesday, July 4, 2007

MVC architecture


Model-view-controller (MVC) is an architectural pattern used in software engineering. In complex computer applications that present a large amount of data to the user, a developer often wishes to separate data (model) and user interface (view) concerns, so that changes to the user interface will not affect data handling, and that the data can be reorganized without changing the user interface. The model-view-controller solves this problem by decoupling data access and business logic from data presentation and user interaction, by introducing an intermediate component: the controller.

It is common to split an application into separate layers: presentation (UI), domain, and data access. In MVC the presentation layer is further separated into view and controller. MVC encompasses more of the architecture of an application than is typical for a design pattern.

Model
The domain-specific representation of the information on which the application operates. It is a common misconception that the model is another name for the domain layer. Domain logic adds meaning to raw data (e.g., calculating if today is the user's birthday, or the totals, taxes and shipping charges for shopping cart items).
Many applications use a persistent storage mechanism (such as a database) to store data. MVC does not specifically mention the data access layer because it is understood to be underneath or encapsulated by the Model.
View
Renders the model into a form suitable for interaction, typically a user interface element.
Controller
Processes and responds to events, typically user actions, and may invoke changes on the model.

MVC is often seen in web applications, where the view is the actual HTML page, and the controller is the code which gathers dynamic data and generates the content within the HTML. Finally the model is represented by the actual content, usually stored in a database or XML files.

Though MVC comes in different flavors, control flow generally works as follows:

1. The user interacts with the user interface in some way (e.g., presses a button).
2. A controller handles the input event from the user interface, often via a registered handler or callback.
3. The controller accesses the model, possibly updating it in a way appropriate to the user's action (e.g., controller updates user's shopping cart).[1]
4. A view uses the model to generate an appropriate user interface (e.g., the view produces a screen listing the shopping cart contents). The view gets its own data from the model. The model has no direct knowledge of the view.
5. The user interface waits for further user interactions, which begins the cycle anew.