Don't use these tips without understanding what happens !
To put an Image...
bi = new BufferedImage[4];
String s[] = { "bld.jpg", "bld.jpg", "boat.gif", "boat.gif"};
for ( int i = 0; i < bi.length; i++ ) {
Image img = getImage(getURL("images/" + s[i]));
try {
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img, 0);
tracker.waitForID(0);
}
catch ( Exception e ) {}
int iw = img.getWidth(this);
int ih = img.getHeight(this);
bi[i] = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
Graphics2D big = bi[i].createGraphics();
big.drawImage(img,0,0,this);
}
To get the dimension of a bouding container
addNotify();
Insets insets = getInsets(); // from inside one container
then you get ...
insets.top
insets.bottom
insets.left
insets.right
Recursion
solveRecursively(Problem p) {
if (p is trivial) return(the obvious answer);
// else simplify the problem
Problem p1 = simplerProblem(p);
s1 = solveRecursively(p1);
// Turn the solution of simpler pb into solution of original problem
s = solutionOfOriginalProblem(s1);
return s1;
}
Careful
- Be always sure that simplerProblem will really bring you towards a trivial
problem
- Be sure to always test for the case of trivial problem that will end the
recursivity
To create a package...
To test the effectiveness of that procedure, you should try with a file that
contains at least 2
Classes
- Chose a Package name (Sun advice is to use your reversed Internet domain
name). E.g. : net.vuylsteker.test
- Put a Package statement in your file
package net.vuylsteker.test
- Check that your CLASSPATH environment variable is well set
It could be set in a .bashrc file for instance :
CLASSPATH="$CLASSPATH;Z:\ClassDir"
export CLASSPATH
- Compile with an option that will put your class in the right hierarchy
javac -d "Z:\ClassDir" HelloWord.java
- Test your package from another location, with the import statement
import net.vuylsteker.test.*
To let a user chose a file
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION) {
fichier = chooser.getSelectedFile();
System.out.println("You chose to open this file: " + fichier.getName());
System.out.println("You chose to open this file: " + fichier.getAbsolutePath());
}
To Do some Pick and Move
class ShowPanel extends JPanel implements MouseListener, MouseMotionListener {
ShowPanel(File f) {
addMouseMotionListener(this);
addMouseListener(this);
setBackground(Color.white);
../..
}
// Handles the event of the user pressing down the mouse button.
public void mousePressed(MouseEvent e) {
last_x = rect.x - e.getX();
last_y = rect.y - e.getY();
// Checks whether or not the cursor is inside of the rectangle
// while the user is pressing the mouse.
if ( rect.contains(e.getX(), e.getY()) ) {
updateLocation(e);
} else {
System.out.println("First position the cursor on the rectangle and then drag.");
pressOut = true;
}
}
// Handles the event of a user dragging the mouse while holding
// down the mouse button.
public void mouseDragged(MouseEvent e) {
if ( !pressOut ) {
updateLocation(e);
} else {
System.out.println("First position the cursor on the rectangle and then drag.");
}
}
// Handles the event of a user releasing the mouse button.
public void mouseReleased(MouseEvent e) {
// Checks whether or not the cursor is inside of the rectangle
// when the user releases the mouse button.
if ( rect.contains(e.getX(), e.getY()) ) {
updateLocation(e);
} else {
System.out.println("First position the cursor on the rectangle and then drag.");
pressOut = false;
}
}
// This method is required by MouseListener.
public void mouseMoved(MouseEvent e) {}
// These methods are required by MouseMotionListener.
public void mouseClicked(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
// Updates the coordinates representing the location of the current rectangle.
public void updateLocation(MouseEvent e) {
rect.setLocation(last_x + e.getX(), last_y + e.getY());
/*
* Updates the label to reflect the location of the
* current rectangle
* if checkRect returns true; otherwise, returns error message.
*/
if (checkRect()) {
System.out.println("Rectangle located at " + rect.getX() + ", " + rect.getY());
} else {
System.out.println("Please don't try to "+ " drag outside the area.");
}
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
// Draws and fills the newly positioned rectangle.
g2.setPaint(Color.red);
g.drawString("Bonjour... "+name, 20, 20);
g.drawImage(img,rect.x,rect.y,null);
}