Monday, January 14, 2008

Random Notes 11

My PC monitor showed some signs of giving up a few days ago and I was concerned about having to shell out some more money for a new one. You could imagine my relief when it miraculously reverted to its former, non-flickering self two days ago.

Of course, I didn't have anything noteworthy to blog about, so I finished my repeat viewing of The Melancholy of Haruhi Suzumiya in chronological order. (The broadcast order was different from the chronological order as written in the light novels.)

The novelty of Haruhi for me is not the copious amounts of fan service, nor the smooth animation, or even the catchy opening and ending themes. Its story asks a what-if, that I am sure most of you have also asked at some point: "What if I am the center of the universe?"

To be sure, the whole series is not carried by Haruhi's strong, sometimes overbearing personality alone. On closer inspection, the other cast members may also be fulfilling their own potential answer to that question, by using their own attributes to keep Haruhi in check and maintain balance in the universe. Even Kyon, a "certified 100% human," can be considered an alternate center, as his unacknowledged importance to Haruhi more than makes up for his lack of extraordinary talent.

At some point in our life we ask that same question, and the subsequent reaction to the answer would shape our specific role in society. I remember having a discussion with two of my high school best friends, Butch and Nicholei, who were also my seatmates. It was a boring break period so we talked about who would be the most famous in the future.

I started the discussion by declaring that I would be President of the Philippines someday. Butch snickered, and went into a detailed thesis on how expensive it was to run for public office. He also highlighted the fact that I was not going to be a lawyer or a movie star, so that would be a disadvantage for me. As for him, the future lawyer, he would want to be appointed to the Supreme Court - often mentioned in the news in the most honorable of terms.

Nicholei, who was listening all these time, chastised me for being a Presidential aspirant without his approval, and Butch for doing all that work only to be mentioned in a newspaper. He would be a kingmaker. He would not be mentioned in newspapers, but his decisions on who would be good for the post would be heeded, and he would have all the influence in the country as a result. How parochial, I countered.

Looking at that discussion today, I am sure my two friends are not pretentious enough to try to live the dream right now; I am sure that they are waiting for the right time to execute their respective Grand Plan(s).

However there are many people who are not quite comfortable with their status as parts of a team, and thus act as if the solar system revolved around them. Not in a Haruhi-like I-have-to-act-like-this-or-my-life-would-be-insignificant way, but more of a I'm-right-because-I-am-better-than-you deal. It seems that the world will never run out of immature children, whose aim is to dominate or subjugate people, because they have to broadcast the fact that they are better in some way than others.

I am not sure why I always encounter these kinds of people... I used to be one of them, but now I have given up running the race of recognition and instead working with what I love most - computers. Seems that in any line of work, there will always be unreasonable, condescending, cocksure people who would not listen to one second of an intelligent opinion.

[There, there, Carl, you're ranting again. Let the people judge for themselves who's right this time.]

==

My Java Exercises:

14.6** (Alternating two messages) Write a program to rotate two messages "Java is fun" and "Java is powerful" displayed on a panel with a mouse click.

AlternatingTwoMessages.java

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

public class AlternatingTwoMessages extends JFrame {

public AlternatingTwoMessages() {
getContentPane().add(new DisplayCoordinatePanelFOUR());
}

/**Main method*/
public static void main(String[] args) {
// Create a frame
AlternatingTwoMessages frame = new AlternatingTwoMessages();
frame.setTitle("Alternating Two Messages");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

class DisplayCoordinatePanelFOUR extends JPanel implements MouseListener {

private static int clickCount = 0;
private boolean pressed = false;
private boolean released = false;

public DisplayCoordinatePanelFOUR() {
addMouseListener(this);
}

public void mousePressed(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mouseClicked(MouseEvent e) {
clickCount++;
repaint();
}

public void paintComponent(Graphics g) {
Font font = new Font("SegoeUI", Font.BOLD, 24);
super.paintComponent(g);
g.setFont(font);
g.setColor(Color.RED);
int width = getWidth();
int height = getHeight();
if (clickCount % 2 == 0) {
g.drawString("Java is powerful", width / 5, height / 2);
} else {
g.drawString("Java is fun", width / 3 - 8, height / 2);
}
}
}


14.7** (Entering and displaying a string) Write a program that receives a string from the keyboard and displays it on a panel. The Enter key signals the end of a string. Whenever a new string is entered, it is displayed on the panel.

DisplayEnteredString.java

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

public class DisplayEnteredString extends JFrame {

private DisplayCoordinatePanelFIVE keyboardPanel = new DisplayCoordinatePanelFIVE();

public DisplayEnteredString() {
setLayout(new GridLayout(2, 1, 3, 3));
getContentPane().add(keyboardPanel);
keyboardPanel.setFocusable(true);
}

/**Main method*/
public static void main(String[] args) {
// Create a frame

DisplayEnteredString frame = new DisplayEnteredString();
frame.setTitle("Display Entered String");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

class DisplayCoordinatePanelFIVE extends JPanel implements KeyListener {

private StringBuffer sb = new StringBuffer(1000);
// private StringBuffer sb2 = new StringBuffer(sb.length());
private boolean enterIsPressed = false;

public DisplayCoordinatePanelFIVE() {
addKeyListener(this);
}

public void keyPressed(KeyEvent e) {
}

public void keyReleased(KeyEvent e) {
int x = e.getKeyCode();
if (x == KeyEvent.VK_ENTER) {
enterIsPressed = true;
}
repaint();
}

public void keyTyped(KeyEvent e) {
sb.append(e.getKeyChar());
}

public void paintComponent(Graphics g) {
Font font = new Font("SegoeUI", Font.TRUETYPE_FONT, 12);
super.paintComponent(g);
g.setFont(font);
g.setColor(Color.BLUE);
int width = getWidth();
int height = getHeight();
// g.drawString("Pressing would show initial keyboard" , 10, height / 10);
// g.drawString(" input, or clear succeeding input.", 10, height / 10 + 20);
if (enterIsPressed) {
g.drawString(sb.toString(), width / 10, 3 * height / 4);
// sb2 = sb;
sb.delete(0, sb.length());
enterIsPressed = false;
}
// g.drawString(sb2.toString(), width / 10, 3 * height / 4);
}
}


14.15** (Racing car) Write a Java program that simulates car racing. The car moves from left to right. When it hits the right end, it restarts from the left and continues the same process. You can use a timer to control the animation. Redraw the car with new base coordinates (x, y).

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


public class RacingCar extends JFrame {

public RacingCar() {
add(new CarImage());
}

public static void main(String[] args) {
JFrame frame = new RacingCar();
frame.setTitle("Racing Car");
frame.setSize(300, 150);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

class CarImage extends JPanel {
protected int x = 0;
protected int y = 150;
protected int z = 300;
protected int c = 0;

public CarImage() {
Timer timer1 = new Timer(50, new ActionListener(){
public void actionPerformed(ActionEvent e) {
x += 10;
c ++;
repaint();
}
});
timer1.start();
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
// x = 0;
y = getHeight();
z = getWidth();
g.setColor(Color.WHITE);
g.fillRect(0, 0, z, y);
Polygon polygon = new Polygon();
polygon.addPoint(x + 10, y - 21);
polygon.addPoint(x + 20, y - 31);
polygon.addPoint(x + 30, y - 31);
polygon.addPoint(x + 40, y - 21);


if (x < z - 40) {
g.setColor(Color.BLACK);
g.fillOval(x + 10, y - 11, 10, 10);
g.fillOval(x + 30, y - 11, 10, 10);
g.setColor(Color.BLUE);
g.fillRect(x, y - 21, 50, 10);
g.setColor(Color.GRAY);
g.fillPolygon(polygon);
g.setColor(Color.RED);
}
else
x = 0;
if (c < z - 86)
g.drawString("Carl learns Java!", c, y - 51);
else
c = 0;

}

}
}

No comments: