Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; import java.text.*; import java.awt.geom.*; public class HelpWindow extends Frame implements ActionListener, ComponentListener, MouseListener, ItemListener, MouseMotionListener, KeyListener, AdjustmentListener, TextListener { Choice choice; TextArea mainTA; Image buffer; Graphics gg; Color acolor = new Color(145,145,145); final static int X=0; final static int Y=0; public HelpWindow(int initialSelection) { super("Robotran help"); setLayout(null); choice=new Choice(); choice.addItem("What this program is..."); choice.addItem("The translated Lejos program..."); choice.addItem("Robot structure"); choice.addItem("Control Structures"); choice.addItem("Loops"); choice.addItem("How to do stuff"); choice.addItem("Statements"); choice.addItem("Variables and Constants"); choice.addItem("Special variables"); choice.addItem("Changes to expressions"); choice.addItem("Comments"); choice.addItem("Turns"); choice.addItem("Button handlers"); choice.addItem("Included Code"); choice.addItem("Warning about bugs"); choice.setFont(new Font("SansSerif",Font.PLAIN,12)); choice.setBounds(X+20,Y+55,356,26); choice.setForeground(new Color(0,0,0)); choice.setBackground(new Color(255,255,255)); choice.addItemListener(this); add(choice); mainTA=new TextArea(5,40); mainTA.addTextListener(this); mainTA.setFont(new Font("Monospaced",Font.PLAIN,12)); mainTA.setBounds(X+8,Y+87,520,374); mainTA.setForeground(new Color(0,0,0)); mainTA.setBackground(new Color(255,255,255)); add(mainTA); switch (initialSelection) { case 0: help0(); break; case 1: help1(); break; case 2: help2(); break; } addMouseListener(this); addMouseMotionListener(this); addComponentListener(this); addKeyListener(this); setBackground(acolor); setVisible(true); setLocation(100,25); setSize(575,500); buffer = createImage(getSize().width, getSize().height); gg = buffer.getGraphics(); repaint(); addWindowListener( new WindowAdapter() { public void windowClosing (WindowEvent we) { dispose(); } } ); } public void actionPerformed(ActionEvent e) { } public void itemStateChanged(ItemEvent e) { if (e.getSource() == choice) { String s = choice.getSelectedItem(); mainTA.setText(""); if (s.equals("What this program is...")) help0(); else if (s.equals("The translated Lejos program...")) help1(); else if (s.equals("Robot structure")) help2(); else if (s.equals("Control Structures")) help3(); else if (s.equals("Loops")) help4(); else if (s.equals("How to do stuff")) help5(); else if (s.equals("Statements")) help6(); else if (s.equals("Variables and Constants")) help7(); else if (s.equals("Special variables")) help8(); else if (s.equals("Changes to expressions")) help9(); else if (s.equals("Comments")) help10(); else if (s.equals("Turns")) help11(); else if (s.equals("Button handlers")) help12(); else if (s.equals("Included Code")) help13(); else if (s.equals("Warning about bugs")) help14(); } } public void adjustmentValueChanged(AdjustmentEvent e) { } public void textValueChanged(TextEvent e) { } public void componentResized(ComponentEvent e) { int newwidth = getSize().width; int newheight = getSize().height; mainTA.setSize(newwidth-15, newheight-90); } public void componentHidden(ComponentEvent e) { } public void componentMoved(ComponentEvent e) { } public void componentShown(ComponentEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { requestFocus(); } public void mouseReleased(MouseEvent e) { } public void mouseClicked(MouseEvent e) { if(!e.isMetaDown()) if(e.getClickCount() == 2) doubleClick(e); } public void doubleClick (MouseEvent e) { } public void mouseMoved(MouseEvent e) { } public void mouseDragged(MouseEvent e) { } public void keyPressed(KeyEvent e){ } public void keyReleased(KeyEvent e){ } public void keyTyped(KeyEvent e){ } public void paint(Graphics g) { if (buffer == null || gg == null) return; gg.setColor(acolor); // Write to gg first g.drawImage(buffer,0,0,this); } public void update (Graphics g) { paint (g); } // The following code was generated from // the user's input lines. It really should // be indented 5 spaces but that might make // many of the lines wrap so we won't indent. private void help0(){ String s = ""; s+="This program translates from a simple language called Robolang into Lejos.\n"; s+="Robolang is a simple Palgo-like programming language.\n"; s+="\n"; s+="Lejos is a form of Java that is used to program the LEGO Mindstorms\n"; s+="robots. Like all forms of Java, it is a little intimidating to use\n"; s+="and the robot stuff makes it even harder.\n"; s+="\n"; s+="You use this tool to write your Robolang program, and then translate it\n"; s+="to Lejos. Save the Lejos program and then compile it separately.\n"; s+="Your instructor will tell you how to do this, but here's the basic\n"; s+="process.\n"; s+="\n"; s+="The Robolang program is written in the left text area, and the translated\n"; s+="program is placed in the right text area. You should not edit the right\n"; s+="text area unless you REALLY know what you are doing because this is a\n"; s+="generated Lejos program.\n"; s+="\n"; s+="First, you open up a CMD shell in Windows XP. (To do this, press the\n"; s+="START button, select \"run\", and type \"cmd\" in the space next to the\n"; s+="word \"Open:\".) \n"; s+="\n"; s+="You may need to cd to the right folder where your Lejos program resides.\n"; s+="Then type\n"; s+="\n"; s+=" lejosc YourProgram.java\n"; s+="\n"; s+="If there any errors, fix them in your Robolang program and retranslate.\n"; s+="\n"; s+="Once the program is compiled, plug your Infrared USB Tower into your\n"; s+="computer, put your robot directly in front of it, turn on the power on\n"; s+="the Robot (the On/Off button) and type\n"; s+="\n"; s+=" lejos YourProgram\n"; s+="\n"; s+="which downloads the program to the robot. Once you see 100% and the\n"; s+="download is successful (it may take several attempts...), you can\n"; s+="press the green RUN button on the robot. At this point, the program is\n"; s+="permanently stored in the robot and you don't need the big computer for\n"; s+="a while, until you want to change the program.\n"; mainTA.setText(s); } private void help1(){ String s = ""; s+="In the right text area, the translated code is placed.\n"; s+="You should not edit this code because it is very complicated, and\n"; s+="because it will be regenerated every time you translate the Robolang\n"; s+="program.\n"; s+="\n"; s+="However, if you DO know what you're doing, you can unlock the text area\n"; s+="and edit it. You should then save it to a file and work with that file\n"; s+="later.\n"; s+="\n"; s+="An alternative is to embed raw Java code within your Robolang program.\n"; s+="Do this by starting the code with /*#java and ending it with */. These\n"; s+="sequences should be on their own separate line and should be at the\n"; s+="beginning of the line.\n"; mainTA.setText(s); } private void help2(){ String s = ""; s+="The LEGO Mindstorms robot that this translated code works on assumes\n"; s+="several things:\n"; s+="\n"; s+=" 1. There are two bumpers on the front, hooked up to touch sensors.\n"; s+=" 2. The left sensor is hooked up to Sensor port S1 (the leftmost one).\n"; s+=" 3. The right sensor is hooked up to Sensor port S2 (the middle one).\n"; s+=" 4. The light sensor is hooked up to Sensor port S3 (the rightmost one.)\n"; s+=" 5. The left motor is hooked up to port A (the leftmost one).\n"; s+=" 6. The right motor is hooked up to port C (the rightmost one).\n"; s+=" 7. The middle motor portis hooked up to the pen motor.\n"; mainTA.setText(s); } private void help3(){ String s = ""; s+="Control structures are a way of piecing together simple, one-line statements.\n"; s+="Here are the Robolang control structures:\n"; s+="\n"; s+=" Loops (repeating statements)\n"; s+="\n"; s+=" while CONDITION\n"; s+=" // statements\n"; s+=" end\n"; s+="\n"; s+=" loop\n"; s+=" // statements\n"; s+=" end\n"; s+="\n"; s+=" repeat N times\n"; s+=" // statements\n"; s+=" end\n"; s+="\n"; s+=" for VAR = BEGIN# to END#\n"; s+=" // statements\n"; s+=" end\n"; s+="\n"; s+=" continue\n"; s+="\n"; s+=" continue until bump\n"; s+="\n"; s+=" Decisions (if statements)\n"; s+="\n"; s+=" if CONDITION\n"; s+=" // statements\n"; s+=" end\n"; s+=" \n"; s+=" if CONDITION\n"; s+=" // statements\n"; s+=" else\n"; s+=" // statements\n"; s+=" end\n"; s+="\n"; s+=" Button handlers\n"; s+="\n"; s+=" when XBUTTON is pressed\n"; s+=" // statements\n"; s+=" end\n"; s+="\n"; s+=" Subprograms and functions:\n"; s+="\n"; s+=" Robolang does not have subprograms or functions at this time\n"; s+="\n"; s+="----------------------------------\n"; s+="Key:\n"; s+=" CONDITION is a boolean expression\n"; s+="\n"; s+=" XBUTTON is either RUN, VIEW or PRGM (all caps)\n"; s+="\n"; s+=" VAR is a variable name\n"; s+="\n"; s+=" BEGIN# is a number or a variable or an expression that has a number value\n"; s+=" END# is a number like BEGIN#\n"; mainTA.setText(s); } private void help4(){ String s = ""; s+="A loop is a control structure that permits you to specify some statements\n"; s+="that will be over and over again. Every loop structure has to have some\n"; s+="way of ending the execution, or it will be an infinite loop and will tie up\n"; s+="the computer \"forever\" (until you turn it off).\n"; s+="\n"; s+="The statements inside the loop control structure are referred to as the \n"; s+="\"body\" of the loop.\n"; s+="\n"; s+="The \"break\" statement can be put inside the body of the loop anywhere.\n"; s+="Usually you will always put a break statement inside an if statement, or\n"; s+="else, it will always kill the loop at that point.\n"; s+="\n"; s+="Whenever a loop stops executing, the next statement after it will be executed.\n"; s+="\n"; s+="Here are the loop structures in Robolang:\n"; s+="\n"; s+=" loop -- generic loop, no condition to stop it\n"; s+=" while -- keep going while some condition is true,\n"; s+=" stop the loop when the condition becomes false\n"; s+=" repeat -- do the loop body for a specified number of times\n"; s+=" for -- do the loop body for a specified number of times,\n"; s+=" but assign this count to a variable\n"; s+="\n"; s+="repeat n times...end\n"; s+=" This is another loop structure that does the statements inside it for\n"; s+=" fixed number of times. In this example, it is the current value of\n"; s+=" variable n. This could be an expression or a constant, like\n"; s+="\n"; s+=" repeat 5 times\n"; s+="\n"; s+="for i=1 to 10...end\n"; s+=" This is another loop structure that runs through values from the\n"; s+=" beginning number to the ending number, setting variable i to this value\n"; s+=" value for each time through the loop. The statements inside the loop\n"; s+=" are executed first by setting variable i to 1. Then they are executed\n"; s+=" again, with i being 2, and so forth until i is 10. The loop's body (the\n"; s+=" statements inside the loop) are done one last time, with i being 10.\n"; s+=" Then it quits.\n"; s+="\n"; s+=" This loop is similar to the repeat 10 times except that the for loop\n"; s+=" gives you the opportunity to use a variable and find out what pass\n"; s+=" through the loop you are in. With repeat 10 times, you don't get this\n"; s+=" opportunity. It just does the statements inside the loop 10 times\n"; s+=" without giving you a variable.\n"; s+="\n"; s+=" You do not need to declare variable i ahead of time.\n"; s+=" The beginning and ending numbers can be variables, constants or\n"; s+=" expressions. For example, both the following are valid:\n"; s+="\n"; s+=" for i=j+1 to k\n"; s+=" for i=2 to m\n"; s+="\n"; s+=" WARNING! Do not change variable i inside the for loop! If you do, the\n"; s+=" loop will probably not do what you want it to, and will probably become\n"; s+=" an endless (infinite) loop and will never stop.\n"; mainTA.setText(s); } private void help5(){ String s = ""; s+=" Declaration: use the \"var\" keyword\n"; s+=" Assignment: use the \"let\" keyword\n"; s+=" Loop: use the \"while\" keyword, followed by \"end\"\n"; s+=" or use the \"loop\" keyword\" followed by \"end\"\n"; s+=" If: use the \"if\" and \"then\" keywords, followed by \"end\"\n"; s+=" When: use the \"when\" keyword, followed by \"end\"\n"; s+=" Output: use the \"display\" keyword\n"; s+=" Ending Program: use the \"exit\" keyword\n"; mainTA.setText(s); } private void help6(){ String s = ""; s+=" Special robot statements:\n"; s+=" go forward\n"; s+=" go forward 2 seconds\n"; s+=" motor X go forward\n"; s+=" motor X go forward N seconds\n"; s+=" motor X go backward\n"; s+=" motor X go backward N seconds\n"; s+=" continue\n"; s+=" continue until bump\n"; s+=" pause X seconds\n"; s+=" turn left N degrees \n"; s+=" turn right N degrees \n"; s+=" turn left N circles\n"; s+=" turn right N circles\n"; s+=" turn right sharp\n"; s+=" turn right sharp N degrees\n"; s+=" turn left sharp N circles\n"; s+="\n"; s+="----------------------------------\n"; s+="Key:\n"; s+=" X stands for A, B, C (all caps)\n"; s+=" N stands for a number\n"; mainTA.setText(s); } private void help7(){ String s = ""; s+="You should declare all variables in your program that you create,\n"; s+="except for the built-in variables defined by the sensors: S1, S2\n"; s+="and S3.\n"; s+="\n"; s+="Here's how. Just put \"var\" in front. This makes a variable that\n"; s+="is an integer (a whole number that can be positive or negative.)\n"; s+="\n"; s+=" var x\n"; s+="\n"; s+="Now you can assign it values:\n"; s+="\n"; s+=" let x = 2\n"; s+=" let x = x + 1\n"; s+=" let x = y * 47\n"; s+="\n"; s+="You can also declare a variable with an initial value:\n"; s+="\n"; s+=" var x = 2\n"; s+="\n"; s+="Or even an initial expression:\n"; s+="\n"; s+=" var angle = 360/numsides\n"; s+="\n"; s+="If you want a real number, you must say it is real. For example,\n"; s+="to create a variable named \"lightlevel\" that holds a real number:\n"; s+="\n"; s+=" var real lightlevel\n"; s+="\n"; s+="Real variables can hold really large and really small numbers, \n"; s+="positive or negative, and they can have decimal points in them.\n"; s+="-1, 2.6 and 4000000.28398 are all real numbers.\n"; s+="\n"; s+="You can also assign a real variable an initial value:\n"; s+="\n"; s+=" var real lightlevel = 2.6\n"; s+="\n"; s+="There are two other types: String and boolean\n"; s+="\n"; s+=" var string name\n"; s+=" var boolean muststop\n"; s+="\n"; s+="Strings hold sequences of characters, like\n"; s+=" \n"; s+=" let name = \"Anderson\"\n"; s+="\n"; s+="Booleans hold either true or false:\n"; s+="\n"; s+=" let muststop = true\n"; mainTA.setText(s); } private void help8(){ String s = ""; s+="There are three special variables: S1, S2, S3\n"; s+="\n"; s+="These are used for the three sensors that your robot may use. The meaning\n"; s+="of each is fixed:\n"; s+="\n"; s+=" S1 is the left touch sensor\n"; s+=" S2 is the right touch sensor\n"; s+=" S3 is the light sensor\n"; s+="\n"; s+="If you wish to reassign these, the easiest thing to do is to move the little\n"; s+="black connector squares to the sensor ports that you desire. For instance,\n"; s+="if you wanted to switch the meanings of S2 and S3, just switch the two black\n"; s+="squares. You cannot tell Robotran that S3 is a touch sensor; it is always\n"; s+="a light sensor.\n"; mainTA.setText(s); } private void help9(){ String s = ""; s+="Expressions are generally like Java, with two exceptions.\n"; s+="First, you don't need parentheses around the whole expression.\n"; s+="But if you need to override precedence, use parentheses inside the\n"; s+="expression.\n"; s+="\n"; s+="Second, there are three Boolean logic keywords that can be used in\n"; s+="Robolang (but not Java.) These are:\n"; s+="\n"; s+=" not\n"; s+=" and\n"; s+=" or\n"; s+="\n"; s+="Here's an example:\n"; s+="\n"; s+=" if a < b and not mustStop then\n"; s+="\n"; s+="This is translated into Java using Java's special operators.\n"; s+="But you can still use Java's special operators. These are ! for not,\n"; s+="&& for and, and || for or.\n"; mainTA.setText(s); } private void help10(){ String s = ""; s+="Robolang comments begin with //. \n"; s+="\n"; s+="// This is a comment.\n"; s+="\n"; s+="Another form can be used to deactivate a whole chunk of code. Start it\n"; s+="with /* and end with */.\n"; s+="\n"; s+="/*\n"; s+=" This is a block of comments.\n"; s+=" You can even put it around some code that you want to temporarily\n"; s+=" deactivate.\n"; s+="*/\n"; s+="\n"; s+="Block comments (the ones with /* and */) cannot be \"nested.\" That is, \n"; s+="You cannot have a block comment inside another block comment, because the\n"; s+="first */ ends the block comment.\n"; s+="\n"; s+="These two forms of comments are identical to Java's style of comments,\n"; s+="by the way.\n"; mainTA.setText(s); } private void help11(){ String s = ""; s+="There are many turning commands. Turns are caused by turning on one motor\n"; s+="and letting the other motor either be off or going backwards. To turn left,\n"; s+="the right motor is turned on to go forwards. To turn right, the left motor\n"; s+="is turned to go forwards.\n"; s+="\n"; s+="You can turn left or right by a given amount, measured either in degrees or\n"; s+="circles. 1 circle is 360 degrees. The default is 90 degrees, which will\n"; s+="cause a sharp turn right or left. To orient the robot so that it is facing\n"; s+="in the opposite direction, turn 180 degrees (which is 0.5 circles). \n"; s+="\n"; s+="Note!\n"; s+="turning to the opposite direction is not the same as going backwards,\n"; s+="though if you turn into the opposite direction and go forwards, the robot\n"; s+="moves in the same direction. However, the bumpers are only on the front.\n"; s+="If the robot is moving backwards, it won't detect bumps.\n"; s+="\n"; s+="The angle of turning is influenced by how long the motors remain turning. \n"; s+="This can be changed by the calibrate statement. This looks like\n"; s+="\n"; s+=" calibrate 1 circle = 10.0 seconds\n"; s+="\n"; s+="This will cause the robot's motors to remain in the same direction and motion\n"; s+="for 10 seconds and should cause it to complete 1 circle. You will have to\n"; s+="experiment with this, since it will vary slightly from robot to robot, and\n"; s+="may even change as your robot's batteries go down in power.\n"; s+="\n"; s+="The turning radius of the robot can be made to be smaller, hence giving a\n"; s+="tighter turn, if one motor is going forward and the other backward. For\n"; s+="instance, to turn left, the right motor (C) goes forward and the left motor (A)\n"; s+="goes backward. Normally, A would sit idle.\n"; s+="\n"; s+="The command to do this is\n"; s+="\n"; s+=" turn sharp right\n"; s+="\n"; s+="or\n"; s+="\n"; s+=" turn right sharp\n"; s+="\n"; s+="There are the usual degrees and circle modifiers:\n"; s+="\n"; s+=" turn sharp right 270 degrees\n"; s+=" turn sharp left 0.25 circles\n"; mainTA.setText(s); } private void help12(){ String s = ""; s+="Robolang permits you to write button handlers. They trap button presses\n"; s+="to some of the four buttons on the RCX brick:\n"; s+="\n"; s+="There are 4 buttons on the LEGO Mindstorms robot. Only 3 of them can\n"; s+="be programmed:\n"; s+="\n"; s+=" On-off cannot be programmed\n"; s+=" RUN starts the program running\n"; s+=" PRGM switches to a different lejos program\n"; s+=" VIEW used to view options in the display\n"; s+="\n"; s+="Notice that all of these buttons have preset meanings, but when your\n"; s+="Lejos program is running, you can redefine their actions by writing\n"; s+="button handlers.\n"; s+="\n"; s+="The structure is the following\n"; s+="\n"; s+=" when RUN button is pressed\n"; s+=" stop motor A\n"; s+=" let count = count + 1\n"; s+=" end\n"; s+="\n"; s+="Any button handler like this can contain other statements (but not\n"; s+="other button handlers.) Inside this code, you can do any statement\n"; s+="that controls the robot, such as stop motors, turn them on, etc.\n"; s+="\n"; s+="You can also declare new variables inside a button handler and use them.\n"; s+="However, these variables will not be visible outside the handler.\n"; s+="\n"; s+="If you want to communicate between the main part of your program with a\n"; s+="button handler, inside the button handler you should refer to a variable \n"; s+="that you declared in the main part. However, that variable must have\n"; s+="the keyword \"global\" in front of its declaration:\n"; s+="\n"; s+=" global var count = 0\n"; s+="\n"; s+="If you simply declare \"var count = 0\" then this variable would be usable\n"; s+="in the main program but invisible inside the button handler.\n"; s+="\n"; s+="You should put your button handlers after your main code. Code in the \n"; s+="button handlers is not executed until and unless the specific button is \n"; s+="pressed, and it can happen anytime, so there is no timing connection \n"; s+="between main code and the handler code.\n"; mainTA.setText(s); } private void help13(){ String s = ""; s+="You can include any Java code by putting it between two special lines.\n"; s+="The first is /*#java and the second is */. Both of these must start\n"; s+="at the beginning of the line. For example:\n"; s+="\n"; s+="/*#java\n"; s+=" String x = \"Pure Java\";\n"; s+=" System.out.println(x+y.substring(5));\n"; s+="*/\n"; s+="\n"; s+="Don't worry about understanding the code. Just be aware that you CAN\n"; s+="include Java code inside a Robolang program by doing this.\n"; mainTA.setText(s); } private void help14(){ String s = ""; s+="This program is not bug-free yet (will any program ever be???)\n"; s+="\n"; s+="Please inform Dr. Meyer of any bugs you find by emailing him:\n"; s+="\n"; s+=" meyer@canisius.edu\n"; s+="\n"; s+="Thank you!\n"; mainTA.setText(s); } // tester public static void main(String[] args) { new HelpWindow(0); } }