Skip to content

Instantly share code, notes, and snippets.

Avatar

BJ Dela Cruz bjpeterdelacruz

View GitHub Profile
View SampleJideApp.java
JFrame frame = new JFrame();
final JPanel panel = new JPanel();
// Create tree and add nodes.
DefaultMutableTreeNode root = new DefaultMutableTreeNode(new TestObject("Hello"));
root.add(new DefaultMutableTreeNode(new TestObject("World")));
root.add(new DefaultMutableTreeNode(new TestObject("Foo")));
root.add(new DefaultMutableTreeNode(new TestObject("Bar")));
final JTree tree = new JTree(root);
@bjpeterdelacruz
bjpeterdelacruz / DynamicClassLoadingExample.java
Last active December 11, 2015 09:59
How to dynamically load a class, de-serialize an object that was serialized using the loaded class, and ignore serialVersionUID in a unit test.
View DynamicClassLoadingExample.java
private URLClassLoader childClassLoader;
private Class<?> clazz;
@Before
public void init() throws Exception {
// put class file in same package together with this source file
URL url = getClass().getResource("MyClass.class").toURI().toURL();
childClassLoader = new URLClassLoader(new URL[] { url },
getClass().getClassLoader());
clazz = childClassLoader.loadClass("com.bpd.old.MyClass");
@bjpeterdelacruz
bjpeterdelacruz / sieve.java
Last active December 26, 2015 00:29
A Java program that will find all prime numbers between 0 and N.
View sieve.java
import java.util.ArrayList;
/**
* This program finds all of the prime numbers between 0 and a number supplied by the user,
* inclusive.
*
* @author BJ Peter DeLaCruz
* @version 1.1
*/
public class CountingPrimeNumbers {
View codingstandard1.java
ahead(centerY * 2); // Move to the bottom edge.
turnRight(90.0); // Face west.
ahead(centerX); // Move to the lower left corner.
turnRight(90.0); // Face north.
ahead(centerY * 2); // Move to the upper left corner.
View codingstandard2.java
counter = 0; // If robot loses radar contact with enemy after 10 turns,
tracker = 0; // try to find it again or pick another enemy by resetting
// tracker back to zero.
View codingstandard3.java
// Move to the bottom edge.
ahead(centerY * 2);
// Face west.
turnRight(90.0);
// Move to the lower left corner.
ahead(centerX);
// Face north.
turnRight(90.0);
// Move to the upper left corner.
ahead(centerY * 2);
@bjpeterdelacruz
bjpeterdelacruz / MyFileChooser.java
Created October 21, 2013 06:17
A custom file chooser that will change the state of the Open button when the user clicks on a file
View MyFileChooser.java
public class MyFileChooser extends JFileChooser {
public MyFileChooser() {
setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
addPropertyChangeListener(new MyListener());
}
private class MyListener implements PropertyChangeListener {
public void propertyChange(PropertyChangeEvent event) {
String property = FileChooser.SELECTED_FILE_CHANGED_PROPERTY;
@bjpeterdelacruz
bjpeterdelacruz / junit_ex1.java
Created November 28, 2013 23:21
JUnit Example #1
View junit_ex1.java
SomeObject obj = new SomeObject();
assertFalse(obj.hasA() || obj.hasB() || obj.hasC());
assertTrue(obj.hasD() && obj.hasE());
@bjpeterdelacruz
bjpeterdelacruz / junit_ex2.java
Created November 28, 2013 23:22
JUnit Example #2
View junit_ex2.java
SomeObject obj = new SomeObject();
assertTrue(!obj.hasA());
assertTrue(!obj.hasB());
assertTrue(!obj.hasC());
assertTrue(obj.hasD());
assertTrue(obj.hasE());
@bjpeterdelacruz
bjpeterdelacruz / junit_ex3.java
Created November 28, 2013 23:33
JUnit Example #3
View junit_ex3.java
SomeObject obj = new SomeObject();
// initial state
assertTrue(obj.hasA());
assertTrue(obj.hasB());
assertTrue(obj.hasC());
assertTrue(obj.hasD());
assertTrue(obj.hasE());
// perform some changes
assertTrue(!obj.hasA());