Monday, June 29, 2009

AbstractDemo

abstract class A3{
abstract void search();
//no body of statements in abstract method


void print(){
System.out.println("Non Abstract Method");
}
}

class B3 extends A3{
void search(){
System.out.println("Binary Search");
System.out.println("Linear Search");
}
}

class AbstractDemo{
public static void main(String[] a){
B3 b2=new B3();
b2.search();
b2.print();

A3 a2;
B3 b3=new B3();
a2=b3;
a2.print();
}
}

SimpleAboutDialog

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class SimpleAboutDialog extends JDialog {
public SimpleAboutDialog(JFrame parent) {
super(parent, "About Dialog", true);

Box b = Box.createVerticalBox();
b.add(Box.createGlue());
b.add(new JLabel("Java source code, product and article"));
b.add(new JLabel("By Java source and support"));
b.add(new JLabel("At www.java2s.com"));
b.add(Box.createGlue());
getContentPane().add(b, "Center");

JPanel p2 = new JPanel();
JButton ok = new JButton("Ok");
p2.add(ok);
getContentPane().add(p2, "South");

ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
setVisible(false);
}
});

setSize(250, 150);
}

public static void main(String[] args) {
JDialog f = new SimpleAboutDialog(new JFrame());
f.show();
}
}

FramewithComponents

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class FramewithComponents extends JFrame {
public FramewithComponents() {
super("JLayeredPane Demo");
setSize(256, 256);

JPanel content = new JPanel();
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
content.setOpaque(false);

JLabel label1 = new JLabel("Username:");
label1.setForeground(Color.white);
content.add(label1);

JTextField field = new JTextField(15);
content.add(field);

JLabel label2 = new JLabel("Password:");
label2.setForeground(Color.white);
content.add(label2);

JPasswordField fieldPass = new JPasswordField(15);
content.add(fieldPass);

getContentPane().setLayout(new FlowLayout());
getContentPane().add(content);
((JPanel) getContentPane()).setOpaque(false);

ImageIcon earth = new ImageIcon("1.jpg");
JLabel backlabel = new JLabel(earth);
getLayeredPane().add(backlabel, new Integer(Integer.MIN_VALUE));
backlabel.setBounds(0, 0, earth.getIconWidth(), earth.getIconHeight());

WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(l);

setVisible(true);
}

public static void main(String[] args) {
new FramewithComponents();
}
}

CenteredFrame

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

public class CenteredFrame extends JFrame {
public CenteredFrame() {
setTitle("CenteredFrame");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screenSize = tk.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
setSize(screenWidth / 2, screenHeight / 2);
setLocation(screenWidth / 4, screenHeight / 4);
}

public static void main(String[] args) {
JFrame frame = new CenteredFrame();
frame.show();
}
}

Errordialogs

import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Errordialogs extends JPanel {

public static void main(String[] a) {

JOptionPane jop = new JOptionPane();
JOptionPane.showMessageDialog(jop, "Press Ok to exit our " +
"from the error diaolag box");
JOptionPane.showConfirmDialog(null, "Click one",
"Click one", JOptionPane.YES_NO_OPTION);

}
}

SimpleScreenManager

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

/**
The SimpleScreenManager class manages initializing and
displaying full screen graphics modes.
*/
public class SimpleScreenManager {

private GraphicsDevice device;

/**
Creates a new SimpleScreenManager object.
*/
public SimpleScreenManager() {
GraphicsEnvironment environment =
GraphicsEnvironment.getLocalGraphicsEnvironment();
device = environment.getDefaultScreenDevice();
}


/**
Enters full screen mode and changes the display mode.
*/
public void setFullScreen(DisplayMode displayMode,
JFrame window)
{
window.setUndecorated(true);
window.setResizable(false);

device.setFullScreenWindow(window);
if (displayMode != null &&
device.isDisplayChangeSupported())
{
try {
device.setDisplayMode(displayMode);
}
catch (IllegalArgumentException ex) {
// ignore - illegal mode for this device
}
}
}


/**
Returns the window currently used in full screen mode.
*/
public Window getFullScreenWindow() {
return device.getFullScreenWindow();
}


/**
Restores the screen's display mode.
*/
public void restoreScreen() {
Window window = device.getFullScreenWindow();
if (window != null) {
window.dispose();
}
device.setFullScreenWindow(null);
}

}

FullScreenTest

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

public class FullScreenTest extends JFrame {

public static void main(String[] args) {

DisplayMode displayMode;

if (args.length == 3) {
displayMode = new DisplayMode(
Integer.parseInt(args[0]),
Integer.parseInt(args[1]),
Integer.parseInt(args[2]),
DisplayMode.REFRESH_RATE_UNKNOWN);
}
else {
displayMode = new DisplayMode(800, 600, 16,
DisplayMode.REFRESH_RATE_UNKNOWN);
}

FullScreenTest test = new FullScreenTest();
test.run(displayMode);
}

private static final long DEMO_TIME = 5000;


public void run(DisplayMode displayMode) {
setBackground(Color.blue);
setForeground(Color.white);
setFont(new Font("Dialog", Font.PLAIN, 24));

SimpleScreenManager screen = new SimpleScreenManager();
try {
screen.setFullScreen(displayMode, this);
try {
Thread.sleep(DEMO_TIME);
}
catch (InterruptedException ex) { }
}
finally {
screen.restoreScreen();
}
}


public void paint(Graphics g) {
g.drawString("Hello World!", 20, 50);
}
}


java FullScreenTest 400 500 100

ImagePanel

public class ImagePanel extends JPanel {
private Image img;

public ImagePanel(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null),
img.getHeight(null));
setSize(size);
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setLayout(null);
}

}