Search

Sunday, July 17, 2011

Drawing Rectangle with Java


Draw Rectangle

import java.awt.Graphics;

import javax.swing.JComponent;
import javax.swing.JFrame;

class MyCanvas extends JComponent {

public void paint(Graphics g) {
g.drawRect (10, 10, 200, 200);
}
}

public class DrawRect {
public static void main(String[] a) {
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBounds(30, 30, 300, 300);
window.getContentPane().add(new MyCanvas());
window.setVisible(true);
}
}

source : http://www.java2s.com

Drawing a square in Java

O’Reilly’s Head First Java is a good book, but it doesn’t do enough hand holding for my tastes. I need more repetition before something sinks in. For example, on page 364, basically it says, “It’s easy to add a square to a screen, just make your own widget!” Then it shows you how to make a widget:

import java.awt.*;
import javax.swing.*;

//OK, that part I understand.

class MyDrawPanel extends JPanel {

//JPanel is a widget that can be added to a frame just like the button in a previous example in the book.

public void paintComponent(Graphics g) {

//Here’s what I’m guessing this means. We’re overriding the paintComponent method in JPanel, and we’re passing it a Graphics method as an argument. Where this comes from, I don’t know, somewhere in java.awt.* or javax.swing.*

g.setColor(Color.orange);
g.fillRect(20,50,100,100);

}

}

So that will compile as javac MyDrawPanel.java.

But, what the book doesn’t explain in big red crayon so an idiot like me can understand is that you have to create another class to make the window that contains the frame that you’ll stick this widget into. The authors assume that if they explain something once, hell, you ought to be able to figure it all out by now, and the lack of explanation in cases like this makes me wish I could take a course in Java in a real school where I could ask my stupid questions to a real live brainy (but patient) person and get answers.

To get the above code to do anything useful, you have to create this other class too, as a separate file:

import javax.swing.*;
import java.awt.*;

public class SimpleGuiRectangle {

//You can call this class anything you want. It’s not part of the Java library, you’re creating it using bits and pieces from awt and swing.

public static void main (String[] args) {


//You know what, either they never explain what that (String[] args) means or I was sleeping when I read it. It’s so that you can call arguments when you run the class like “java SimpleGuiRectangle -v -h –chuckyoufarley”

MyDrawPanel rectangle;

// You’re setting up the variable that will contain an instance of the MyDrawPanel class you made up there.

SimpleGuiRectangle gui = new SimpleGuiRectangle();

// This is a recursive thing? Inside the class you’re making, you’re creatubg a instance of it. Confuses the heck out of me still.

JFrame frame = new JFrame();

// The JFrame is the outer part of a Java window. The actual guts are in the contentPane that’s inside the JFrame.

rectangle = new MyDrawPanel();

//OK, this is an instance of that class you copied from page 364 and complied. Now you’re actually going to do something useful with it.

frame.getContentPane().add(rectangle);

//What this means (I think) is getContentPane is a method in the JFrame class. It has a method called “add” which takes an object and adds it to the pane. So instead of adding a bigass button like they showed in the book, you’re adding an instance of thar rectangle class you made.

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//You have to put that in so you’ll be able to actually close the damn window

frame.setSize(300,300);
frame.setVisible(true);

}

}

So you compile “javac SimpleGuiRectangle.java” then run “java SimpleGuiRectangle and for all your trouble you get a 300 x 300 window with an orange square in it. Seems like a lot of frikkin’ work to do something so simple, and it makes me wonder why Java is described as being supposedly so quick and easy. Or maybe I’m just a really slow learner.

Java Digital Clock

This is a basic digital clock in Java that works off your operating system time, it works in a multithreaded environment and have coded it to put my own background in.

Code: Java

import java.awt.*;
import javax.swing.*;
import java.util.*;

class Clock extends JFrame implements Runnable
{
Thread runner; //declare global objects
Font clockFont;

public Clock()
{
super("Java clock");
setSize( 350, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
color="#808080" style=" font-style: italic;">//create window

clockFont = new Font("Serif", Font.BOLD, 40); //create font instance

Container contentArea = getContentPane();
ClockPanel timeDisplay = new ClockPanel();


contentArea.add(timeDisplay); //add components
setContentPane(contentArea);
start(); //start thread running

}
source : http://www.go4expert.com

Saturday, July 9, 2011

Using thumbnails

When you select a folder with bitmaps in Windows Explorer, it'll indicate all bitmap files using the same old icon. Did you know that you can make Windows display a thumbnail of those bitmaps instead?
  • Run "Registry Editor" (run RegEdit.exe)
  • Select "HKEY_CLASSES_ROOT\.bmp" and note the "default" value for it ("Paint.Picture" for example)

  • Now select "HKEY_CLASSES_ROOT\Paint.Picture" ("Paint.Picture" being the default value as explained above)
  • Double click on "default"
  • Type "%1" without the quotes and press ENTER
  • Restart Windows

Now when you view directories with bitmap files, you'll actually see a small (32x32 by default) representation of those pictures.

Rename Rycyle Bin

Did you ever wanted to rename the "Recycle Bin," and found out that you can't rename it like other files and folders?
  1. Run "Registry Editor"
    Select "Start | Run," type REGEDIT.EXE and press ENTER.
  2. Select "HKEY_CLASSES_ROOT\CLSID"
  3. Then select "{645FF040-5081-101B-9F08-00AA002F954E}"

  1. Double click on "(Default)"
  2. You'll see the current name of your "Recycle Bin." Simple change it to whatever you want and close the Registry Editor.
  3. Click on the "Desktop" and press F5 to refresh shortcuts.

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More