JAVA -- weekly 11th lab -- software simulating a signal lamp -- code Reconstruction -- upgraded version

Source: Internet
Author: User

Test class
 
[Java]
/* (Start of program header annotation)
* Copyright and version Declaration of the program
* Copyright (c) 2011, a student from the computer College of Yantai University
* All rights reserved.
* File name: software that simulates a signal lamp
* Author: Lei hengxin
* Completion date: January 1, November 10, 2012
* Version No.: V1.0
* Description of tasks and Solutions
* Input Description: Add a drop-down list to the north of the JFrame. The drop-down list includes the "default no signal", "red light", "green light", and "yellow light" options. Www.2cto.com
* Input Description: Add a canvas in the center of the window. When you select an item from the drop-down list, the corresponding traffic signal is displayed on the canvas.
* Input Description: Program Composition: compile a subclass SignalCanvas of the Canvas, which is responsible for lighting.
* Input Description: Write a GUI class to select a drawing. Compile a program execution entry to test the software.
* Problem description:
* Program output:
* End the comment in the program Header
*/
Package Draw;
 
Public class Test {
 
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
Select_draw draw = new Select_draw ();
 
}
 
}

SignalCanvas class
 
[Java]
Package Draw;
 
Import java. awt. Canvas;
Import java. awt. Color;
Import java. awt. Graphics;
 
Public class SignalCanvas extends Canvas {
Int x, y, r;
Color color;
 
SignalCanvas (Color color ){
SetBackground (color );
}
 
Public void setX (){
This. x = (int) (Math. random () * 100) + 200;
}
 
Public void setY (){
This. y = (int) (Math. random () * 100) + 200;
}
 
Public void setR (){
This. r = (int) (Math. random () * 60) + 60;
}
 
Public void paint (Graphics g ){
G. setColor (this. color );
G. fillOval (x, y, 2 * r, 2 * r );
}
 
Public void setColor (Color color ){
This. color = color;
}
 
}

Select_draw class
 
[Java]
Package Draw;
 
Import java. awt .*;
Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener;
Import java. awt. event. ItemEvent;
Import java. awt. event. ItemListener;
 
Import javax. swing .*;
 
Public class Select_draw extends JFrame implements ItemListener {
SignalCanvas myCanvas;
Choice choice;
 
Select_draw (){
SetTitle ("Traffic Signal ");
Choice = new Choice ();
Choice. add ("no signal by default ");
Choice. add ("red light ");
Choice. add ("green light ");
Choice. add ("yellow light ");
Add (choice, BorderLayout. NORTH );
Choice. addItemListener (this );
MyCanvas = new SignalCanvas (Color. white );
Add (myCanvas, BorderLayout. CENTER );
SetSize (600,600 );
SetVisible (true );
}
 
Public void itemStateChanged (ItemEvent e ){
String name = choice. getSelectedItem ();
System. out. println (name );
If (name = "red light "){
MyCanvas. setColor (Color. red );
} Else if (name = "green light "){
MyCanvas. setColor (Color. blue );
} Else if (name = "yellow light "){
MyCanvas. setColor (Color. yellow );
} Else {
MyCanvas. setColor (Color. white );
}
MyCanvas. setX ();
MyCanvas. setY ();
MyCanvas. setR ();
MyCanvas. repaint ();
}
 
}

 
Running result:

 


 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.