Monday, December 19, 2005

Java Visible Screen

For Mac OS X you can use Toolkit.getScreenInsets() to setup a full size GUI without rendering into the menubar or the Dock.

An update with more detail:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
// this only works for one screen, if you need to access more than one
// you will need to loop through every GraphicsDevice
Insets in = Toolkit.getDefaultToolkit().getScreenInsets(gs[0].getConfigurations()[0]);
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();

left = in.left;
right = size.width - in.left - in.right;
top = in.top;
bottom = size.height - in.top - in.bottom;
setBounds(left, top, right, bottom);

Saturday, December 10, 2005

c++ Copy Constructor Conventions

A couple of very simple to follow rules for copy constructors.
Case A: An object has no dynamically allocated pointers. Let the default copy constructor do the work and don't bother writing one.

Case B: If said object does contain dynamic pointers, then you'll need to write your own copy constructor as follows.
MyObject(const MyObject& o) {
// Do some stuff for a true deep copy
// Make new pointers and stuff :)
}

Wednesday, December 07, 2005

c++ Type Conversions Rehash

Another popular way to write this function is by using templates.
template <typename T>
string toString(T& i) {
ostringstream oss;
oss << fixed << i;
return oss.str();
}
This should be put in a header file and needs the following:
sstream
iomanip
string

Tuesday, December 06, 2005

c++ Type Conversions

This will eventually come up in your programming days, so I thought I'd post it.

You need to convert a string input by the user to another data type (I'll use double here to demonstrate.)
//you'll need these includes
#include <string>
#include <sstream>

string s = "23.5"; // we need a string to work with
istringstream istr(s); // this is the stream we pump the string into (done by constructor)
double d; // a place to store the double we are asking for
istr >> d; // here we tell the sstream to pump our string out as a double

This can be easily formed into a function.
double toDouble(string& s) { // we only need a reference to the string since we are not going to modify it
istringstream istr(s);
double d;
istr >> d;
return d;
}

We can also reverse the process.
string toString(int& i) { // we only need a reference to the int since we are not going to modify it
ostringstream ostr;
ostr << fixed << i;
ostr << fixed << setw(8) << setprecision(3) << i;
// this second version adds some handy formatting
return ostr.str();
}

Friday, December 02, 2005

Math4J Library

Math4J: A Mathematics Library

This is new approach for math coding, at least for me. I plan to complete the function design and introduce a statistics design.

Math4J is a java library with an object-oriented design of some mathematical ideas: functions, primality, complex numbers, etc. It also consists of some utility methods and classes for mathematical constructions. This library is open source and currently only in alpha development.