所以我在计算机科学课上需要一些帮助。我们创建了一个机器人,它需要具有他没有“握住我们的手”的功能。我的想法是让机器人在你“戳”他的眼睛时有一个显示“OW”的文本框。但是,我在输入文本时遇到了一些问题。我设法让眼睛改变颜色,但是当我点击眼睛时,我遇到了文本框没有显示的问题。谁能帮我弄清楚我的问题是什么?
PS忽略代码中未完成的披萨,仍在努力。
/*
* File Name: Test Robot
*
* This programs is going to attempt to make a robot using 3
* rectangles, 2 ovals, and 2 lines and a piece of pizza next to him.
*
*
* 8-28-13
*/
import acm.graphics.*;
import acm.program.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.TextComponent.*;
public class Robot extends GraphicsProgram {
GOval leftEye;
GOval rightEye;
GArc pizzaSlice;
GRect pizzaBottom;
public void init() {
//Start with large gray rectangle as base.
GRect base = new GRect(100,100,300,300);
base.setFilled(true);
base.setColor(Color.GRAY);
add(base);
//Second add two black ovals as eyes.
leftEye = new GOval(150,175,90,75);
leftEye.setFilled(true);
leftEye.setColor(Color.WHITE);
leftEye.setFillColor(Color.BLACK);
add(leftEye);
rightEye = new GOval(250,175,90,75);
rightEye.setFilled(true);
rightEye.setColor(Color.WHITE);
rightEye.setFillColor(Color.BLACK);
add(rightEye);
//Fourth add a white rectangle as the mouth.
GRect mouth = new GRect(175,325,150,50);
mouth.setFilled(true);
mouth.setColor(Color.WHITE);
add(mouth);
//Finally, add two black lines as antennae.
GLine Antennae1 = new GLine(250,100,100,50);
Antennae1.setColor(Color.BLACK);
add(Antennae1);
GLine Antennae2 = new GLine(250,100,400,50);
Antennae1.setColor(Color.BLACK);
add(Antennae2);
//pizza making time baby
pizzaSlice = new GArc(300,100,300,300,0,50);
pizzaSlice.setFilled(true);
pizzaSlice.setColor(Color.YELLOW);
pizzaSlice.setFillColor(Color.RED);
pizzaBottom = new GRect(300,100,600,400);
pizzaBottom.setFilled(true);
pizzaBottom.setColor(Color.YELLOW);
add(pizzaSlice);
//tells the program to pay attention to the mouse events
addMouseListeners();
}
//handles when mouse enters the window
public void mouseEntered(MouseEvent e) {
leftEye.setFillColor(Color.YELLOW);
rightEye.setFillColor(Color.YELLOW);
}
//handles when mouse exits the window
public void mouseExited(MouseEvent e) {
leftEye.setFillColor(Color.BLACK);
rightEye.setFillColor(Color.BLACK);
}
//handles the mouse event when mouse is pressed
public void mousePressed(GPoint pt) {
if (leftEye.contains(pt) || rightEye.contains(pt)){
leftEye.setFillColor(Color.GREEN);
rightEye.setFillColor(Color.GREEN);
TextArea OW = new TextArea("OW!", 100, 100,);
add(OW);
}
}
//handles the mouse event when the mouse is released
public void mouseReleased(GPoint pt) {
leftEye.setColor(Color.BLACK);
rightEye.setColor(Color.BLACK);
}
}