Search

Sunday, July 17, 2011

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.

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More