Source to BitShiftAp Applet, just a front end to the BitShift class
/* Marcus Green November 1999 BitShiftApp.java demonstration of
the process of the Java Bit shifting operands << >> >>. Part of
Marcus Greens Java Programmer Certification resources to be found at
http://www.software.u-net.com. mail@marcusgreen.co.uk. This file is the
interface component to be used with BitShift.java that does the actual
processing of the bits
*/
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class BitShiftAp extends Applet {
String ShiftType="";
int[] mybits = new int[32];
TextField txtInputValue =new TextField(32);
Panel ButtonPanel = new Panel();
Button LeftShift = new Button("<<");
Button RightShift = new Button(">>");
TextField txtPlaceCount = new TextField(4);
Panel pnlInputValue = new Panel();
Panel pnlBinaryVersion = new Panel();
GridLayout gridLayout1 = new GridLayout(4,2);
Label lblNumberToShift = new Label();
FlowLayout flowLayout1 = new FlowLayout();
Label lblBinaryVersion = new Label();
TextField txtBinBeforeShift = new TextField(33);
Panel pnlShiftedVersion = new Panel();
Label lblShiftedVersion = new Label();
TextField txtBinAfterShift = new TextField(33);
Label lblNumberOfPlaces = new Label();
Button btnUnsignedRight = new Button();
Panel pnlShiftedDecVersion = new Panel();
Label lblShiftedDecVersion = new Label("Shifted Decimal Version");
TextField txtDecAfterShift = new TextField(29);
FlowLayout flowLayout2 = new FlowLayout();
//Construct the applet
public BitShiftAp() {
}
//Initialize the applet
public void init() {
try {
jbInit();
//txtInputValue.setSelectionStart(30);
}
catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
setLayout(gridLayout1);
this.setLayout(gridLayout1);
this.setBackground(Color.pink);
//this.setSize(new Dimension(400, 295));
//txtInputValue.setSelectionStart(33);
RightShift.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
RightShift_mouseClicked(e);
}
});
LeftShift.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
LeftShift_mouseClicked(e);
}
});
pnlBinaryVersion.setLayout(flowLayout2);
gridLayout1.setRows(6);
lblNumberToShift.setText("Number to Shift");
lblBinaryVersion.setText("Binary Version");
lblShiftedVersion.setText("Shifted Version");
lblNumberOfPlaces.setText("Number of Places");
btnUnsignedRight.setLabel(">>>");
btnUnsignedRight.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
btnUnsignedRight_mouseClicked(e);
}
});
add(ButtonPanel);
ButtonPanel.add(btnUnsignedRight, null);
ButtonPanel.add(LeftShift);
ButtonPanel.add(RightShift);
ButtonPanel.add(lblNumberOfPlaces, null);
ButtonPanel.add(txtPlaceCount);
this.add(pnlInputValue);
pnlInputValue.add(lblNumberToShift, null);
pnlInputValue.add(txtInputValue, null);
add(pnlBinaryVersion);
pnlBinaryVersion.add(lblBinaryVersion, null);
pnlBinaryVersion.add(txtBinBeforeShift, null);
this.add(pnlShiftedVersion, null);
pnlShiftedVersion.add(lblShiftedVersion, null);
pnlShiftedVersion.add(txtBinAfterShift, null);
add(pnlShiftedDecVersion);
pnlShiftedDecVersion.add(lblShiftedDecVersion);
pnlShiftedDecVersion.add(txtDecAfterShift, null);
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
}
void RightShift_mouseClicked(MouseEvent e) {
String sPlaceCount;
sPlaceCount = txtPlaceCount.getText();
if(sPlaceCount.equals("")){
showStatus("Please enter the number of places to shift");
}else
{
doShift(">>");
}
}
public void doShift(String sShiftType){
String sBeforeShift="";
String sAfterShift="";
int iInputValue=0, iPlaceNo=0;
iInputValue= Integer.parseInt(txtInputValue.getText().trim());
int iPlaces= Integer.parseInt(txtPlaceCount.getText().trim());
BitShift bs = new BitShift();
txtBinBeforeShift.setText(bs.getBitString(iInputValue ));
txtBinAfterShift.setText((bs.doShift(iPlaces,iInputValue,sShiftType)));
txtDecAfterShift.setText(new Integer(bs.getShifted()).toString());
}
void LeftShift_mouseClicked(MouseEvent e) {
String sPlaceCount;
sPlaceCount = txtPlaceCount.getText();
if(sPlaceCount.equals("")){
showStatus("Please enter the number of places to shift");
}else
{
doShift("<<");
}
}
void btnUnsignedRight_mouseClicked(MouseEvent e) {
String sPlaceCount;
sPlaceCount = txtPlaceCount.getText();
if(sPlaceCount.equals("")){
showStatus("Please enter the number of places to shift");
}else
{
doShift(">>>");
}
}
}