Hogwarts School Of Computer Science
From Huben's Wiki
Line 1: | Line 1: | ||
+ | [[Image:Welcome To Hogwarts.jpg]] | ||
+ | |||
''GLENDOWER: I can call spirits from the vasty deep.<br> | ''GLENDOWER: I can call spirits from the vasty deep.<br> | ||
''HOTSPUR: Why, so can I, or so can any man; But will they come when you do call for them?''<br> | ''HOTSPUR: Why, so can I, or so can any man; But will they come when you do call for them?''<br> | ||
Line 54: | Line 56: | ||
// Adjust the horizontal position of the banner: | // Adjust the horizontal position of the banner: | ||
− | xPos- | + | xPos = xPos - 1; |
if (xPos < -100) | if (xPos < -100) | ||
{ | { | ||
Line 81: | Line 83: | ||
* Start Dr. Java. | * Start Dr. Java. | ||
* Cut and paste the incantation Banner.java into Dr. Java. | * Cut and paste the incantation Banner.java into Dr. Java. | ||
− | * Use the file menu to save it to your H drive. Don't change the name from Banner. | + | * Use the file menu to save it to your H drive. Don't change the name from Banner. Dr. Java automatically knows what to call the file: you just tell it what folder. |
+ | * Use the Edit/Preferences menu to check the "Show All Line Numbers" box. Click apply, and close the preferences box. Go "oooo, ahhhhhhh!" | ||
* Click "compile", then click "run". | * Click "compile", then click "run". | ||
* Enjoy the results! | * Enjoy the results! | ||
Now we play with it. | Now we play with it. | ||
− | * Look for numbers, and try to | + | * Look for numbers, and try to change them to make it run differently. You will need to compile and run to see your changes. |
* What numbers do what? | * What numbers do what? | ||
* Bonus if you can find how to make it hop instead of smooth scrolling. | * Bonus if you can find how to make it hop instead of smooth scrolling. | ||
* Double bonus if you can do it the right way. | * Double bonus if you can do it the right way. | ||
+ | |||
+ | Here is another incantation: | ||
+ | |||
+ | <pre> | ||
+ | // This program shows simple graphics in a window. | ||
+ | |||
+ | import java.awt.*; | ||
+ | import javax.swing.*; | ||
+ | |||
+ | public class HelloGraphics extends JPanel | ||
+ | { | ||
+ | public void paintComponent(Graphics g) | ||
+ | { | ||
+ | super.paintComponent(g); // Call JPanel's paintComponent method | ||
+ | // to paint the background | ||
+ | g.setColor(Color.RED); | ||
+ | |||
+ | // Draw a 150 by 45 rectangle with the upper-left | ||
+ | // corner at x = 25, y = 40: | ||
+ | g.drawRect(20, 40, 150, 45); | ||
+ | |||
+ | g.setColor(Color.BLUE); | ||
+ | |||
+ | // Draw a string of text starting at x = 60, y = 25: | ||
+ | g.drawString("Hello, Graphics!", 55, 65); | ||
+ | } | ||
+ | |||
+ | public static void main(String[] args) | ||
+ | { | ||
+ | JFrame window = new JFrame("Graphics Demo"); | ||
+ | // Set this window's location and size: | ||
+ | // upper-left corner at 300, 300; width 200, height 100 | ||
+ | window.setBounds(300, 300, 200, 150); | ||
+ | window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
+ | |||
+ | HelloGraphics panel = new HelloGraphics(); | ||
+ | panel.setBackground(Color.WHITE); // the default color is light gray | ||
+ | Container c = window.getContentPane(); | ||
+ | c.add(panel); | ||
+ | |||
+ | window.setVisible(true); | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | This incantation has not followed the rules of the curly braces, but will still work. | ||
+ | |||
+ | * Cut and paste it into Dr. Java, and save it as HelloGraphics. | ||
+ | * Run it: it should work. | ||
+ | * Now indent it properly so that it looks like Banner.java above. | ||
+ | * Run it: it should still work. | ||
+ | * What does that tell us about spaces in these incantations? | ||
+ | * Why then do we indent according to curly braces? | ||
+ | |||
+ | Let's do it the easy way now! | ||
+ | |||
+ | * Cut and paste it again into Dr. Java, so there is no indentation. | ||
+ | * Select all the code. | ||
+ | * Press the Tab key. What magic has resulted? | ||
+ | * If you have time, you can play with this program too. |