Logic and Integer Arithmetic : JVM Tool Interface « Development Class « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Development Class » JVM Tool InterfaceScreenshots 
Logic and Integer Arithmetic
   
/*
 * Copyright (c) 1996 Artima Software Company. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for EVALUATION purposes only
 * is hereby granted provided that this copyright notice
 * appears in all copies. "Evaluation purposes" are any uses which
 * do not constitute the sale, sharing, or redistribution of this
 * software with or to any other persons in any medium.
 *
 * ARTIMA SOFTWARE COMPANY MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT
 * THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. ARTIMA SOFTWARE COMPANY
 * SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
 * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 */

/*
 * ExposedInt.java
 *
 * This file contains all the code for the signed twos-complement int demo
 * applet, named Exposed Int, that accompanies the JavaWorld Under The Hood
 * article titled, "Logic and Integer Arithmetic".
 *
 * Bill Venners, September 1996
 *
 */

import java.awt.*;
import java.applet.*;

/**
 * An applet that interactively demonstrates the format and behavior
 * of signed twos-complement ints in Java.
 *
 @author      Bill Venners
 *
 */
public class ExposedInt extends Applet {

    private Label binaryField;
    private Label hexField;
    private Label decimalField;
    private int value;

    private GrayButton maximumButton = new GrayButton(ExposedIntStringTable.max);
    private GrayButton minimumButton = new GrayButton(ExposedIntStringTable.min);
    private GrayButton zeroButton = new GrayButton(ExposedIntStringTable.zero);

    public void init() {

        Panel buttonPanel = new PanelWithInsets(0000);
        buttonPanel.setLayout(new GridLayout(3255));
        buttonPanel.add(new GrayButton(ExposedIntStringTable.increment));
        buttonPanel.add(new GrayButton(ExposedIntStringTable.decrement));
        buttonPanel.add(minimumButton);
        buttonPanel.add(maximumButton);
        buttonPanel.add(zeroButton);
        buttonPanel.add(new GrayButton(ExposedIntStringTable.negate));
        zeroButton.disable();

        binaryField = new Label("00000000000000000000000000000000");
        hexField = new Label("00000000");
        decimalField = new Label("0");

        Font fieldFont = new Font("TimesRoman", Font.PLAIN, 12);
        binaryField.setFont(fieldFont);
        hexField.setFont(fieldFont);
        decimalField.setFont(fieldFont);

        Panel numberPanel = new Panel();
        numberPanel.setBackground(Color.white);
        numberPanel.setLayout(new GridLayout(31));
        Panel binaryPanel = new Panel();
        binaryPanel.setLayout(new BorderLayout());
        binaryPanel.add("Center", binaryField);
        numberPanel.add(binaryPanel);

        Panel hexPanel = new Panel();
        hexPanel.setLayout(new BorderLayout());
        hexPanel.add("Center", hexField);
        numberPanel.add(hexPanel);
        numberPanel.add(decimalField);

        Panel labelPanel = new Panel();
        labelPanel.setBackground(Color.white);
        labelPanel.setLayout(new GridLayout(31));
        Label label = new Label(ExposedIntStringTable.binary, Label.CENTER);
        Font labelFont = new Font("Helvetica", Font.ITALIC, 11);
        label.setFont(labelFont);
        labelPanel.add(label);
        label = new Label(ExposedIntStringTable.hex, Label.CENTER);
        label.setFont(labelFont);
        labelPanel.add(label);
        label = new Label(ExposedIntStringTable.decimal, Label.CENTER);
        label.setFont(labelFont);
        labelPanel.add(label);

        Panel dataPanel = new Panel();
        dataPanel.setLayout(new BorderLayout());
        dataPanel.add("West", labelPanel);
        dataPanel.add("Center", numberPanel);

        ColoredLabel title = new ColoredLabel(ExposedIntStringTable.title, Label.CENTER, Color.yellow);
        title.setFont(new Font("Helvetica", Font.BOLD, 12));

        setBackground(Color.red);
        setLayout(new BorderLayout(55));
        add("North", title);
        add("West", buttonPanel);
        add("Center", dataPanel);
    }

    public boolean action(Event evt, Object arg) {

        if (evt.target instanceof Button) {
            String bname = (Stringarg;
            if (bname.equals(ExposedIntStringTable.increment)) {

                ++value;
            }
            else if (bname.equals(ExposedIntStringTable.decrement)) {

                --value;
            }
            else if (bname.equals(ExposedIntStringTable.min)) {

                value = 0x80000000;
            }
            else if (bname.equals(ExposedIntStringTable.max)) {

                value = 0x7fffffff;
            }
            else if (bname.equals(ExposedIntStringTable.zero)) {

                value = 0;
            }
            else if (bname.equals(ExposedIntStringTable.negate)) {

                value *= -1;
            }
            UpdateNumberFields();
            enableDisableButton(maximumButton, Integer.MAX_VALUE);
            enableDisableButton(minimumButton, Integer.MIN_VALUE);
            enableDisableButton(zeroButton, 0);
        }
        return true;
    }

    void enableDisableButton(Button b, int val) {

        if (!b.isEnabled()) {
            if (value != val) {
                b.enable();
            }
        else if (value == val) {
            b.disable();
        }
    }

    void UpdateNumberFields() {
        decimalField.setText(Integer.toString(value));

        int v = value;
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < 8; ++i) {
            // Get lowest bit
            int remainder = v & 0xf;

            // Convert bit to a character and insert it into the beginning of the string
            switch (remainder) {
            case 0:
                buf.insert(0"0");
                break;
            case 1:
                buf.insert(0"1");
                break;
            case 2:
                buf.insert(0"2");
                break;
            case 3:
                buf.insert(0"3");
                break;
            case 4:
                buf.insert(0"4");
                break;
            case 5:
                buf.insert(0"5");
                break;
            case 6:
                buf.insert(0"6");
                break;
            case 7:
                buf.insert(0"7");
                break;
            case 8:
                buf.insert(0"8");
                break;
            case 9:
                buf.insert(0"9");
                break;
            case 10:
                buf.insert(0"a");
                break;
            case 11:
                buf.insert(0"b");
                break;
            case 12:
                buf.insert(0"c");
                break;
            case 13:
                buf.insert(0"d");
                break;
            case 14:
                buf.insert(0"e");
                break;
            case 15:
                buf.insert(0"f");
                break;
            }

            // Shift the int to the right one bit
            v >>>= 4;
        }
        hexField.setText(buf.toString());

        v = value;
        buf.setLength(0);
        for (int i = 0; i < 32; ++i) {
            // Get lowest bit
            int remainder = v & 0x1;

            // Convert bit to a character and insert it into the beginning of the string
            if (remainder == 0) {
                buf.insert(0"0");
            }
            else {
                buf.insert(0"1");
            }

            // Shift the int to the right one bit
            v >>>= 1;
        }
        binaryField.setText(buf.toString());
    }

    public Insets insets() {
        return new Insets(5555);
    }
}

class ExposedIntStringTable {

    public static final String title = "EXPOSED INT";
    public static final String binary = "binary";
    public static final String hex = "hex";
    public static final String decimal = "decimal";
    public static final String max = "Max";
    public static final String min = "Min";
    public static final String zero = "Zero";
    public static final String increment = "++";
    public static final String decrement = "--";
    public static final String negate = "*=(-1)";
}

// I used this class because I can't seem to set the background color of
// a label.  I only want a label, but I want the backgound to be gray.
class ColoredLabel extends Panel {

    private Label theLabel;

    ColoredLabel(String label, int alignment, Color color) {

        setLayout(new GridLayout(1,1));

        setBackground(color);

        theLabel = new Label(label, alignment);

        add(theLabel);
    }

    public void setLabelText(String s) {

        theLabel.setText(s);
    }

    public Insets insets() {
        return new Insets(0000);
    }
}

class GrayButton extends Button {

    GrayButton(String label) {

        super(label);
        setBackground(Color.lightGray);
    }
}

class PanelWithInsets extends Panel {

    private int top;
    private int left;
    private int bottom;
    private int right;

    PanelWithInsets(int t, int l, int b, int r) {
        top = t;
        left = l;
        bottom = b;
        right = r;
    }

    PanelWithInsets() {
        top = 5;
        left = 5;
        bottom = 5;
        right = 5;
    }

    public Insets insets() {
        return new Insets(top, left, bottom, right);
    }
}

   
    
    
  
Related examples in the same category
1. Set the memory available to the JVM
2. jconsole plugin
3. This agent library can be used to track threads that wait on monitors.
4. Check the version of the interface being used
5. Track method call and return counts
6. Inject code at method calls
7. Do some very basic bytecode insertion (BCI) of class files
8. Byte Code Instrumentation (BCI)
9. How to get an easy view of the heap in terms of total object count and space used
10. Track object allocations
11. This agent library can be used to track garbage collection events
12. JVM Tool Interface agent utilities
13. Java Platform Debugger Architecture
14. Memory related
15. System memory
16. Floating Point Arithmetic
17. JVM Simulator
18. Stack memory view
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.