• 17.3


      1 import java.awt.BorderLayout;
      2 import java.awt.Color;
      3 import java.awt.Graphics;
      4 import java.awt.GridLayout;
      5 import java.awt.event.ActionEvent;
      6 import java.awt.event.ActionListener;
      7 
      8 import javax.swing.*;
      9 
     10 public class Test_17_3 extends JFrame{    
     11     private final int YSTART = 20;
     12     private final int VGAP = 2;
     13     private final int XGAP = 2;
     14     private Color myColor = Color.BLACK;
     15     
     16     ButtonGroup group = new ButtonGroup();
     17     JRadioButton jrb1 = new JRadioButton("red");
     18     JRadioButton jrb2 = new JRadioButton("blue");
     19     JRadioButton jrb3 = new JRadioButton("yellow");    
     20     
     21     trafficPanel tP = new trafficPanel();
     22     public Test_17_3(){    
     23         
     24         JPanel jgButtons = new JPanel();
     25         jgButtons.setLayout(new GridLayout(1,3));
     26         jgButtons.add(jrb1);
     27         jgButtons.add(jrb2);
     28         jgButtons.add(jrb3);
     29         
     30         group.add(jrb1);
     31         group.add(jrb2);
     32         group.add(jrb3);    
     33         
     34         setLayout(new BorderLayout());
     35         add(jgButtons,BorderLayout.SOUTH);
     36         add(tP,BorderLayout.CENTER);    
     37         
     38         jrb1.addActionListener(new ActionListener(){
     39 
     40             @Override
     41             public void actionPerformed(ActionEvent arg0) {
     42                 // TODO Auto-generated method stub
     43                 myColor = Color.RED;
     44                 tP.setColor = true;                
     45                 repaint();
     46             }
     47             
     48         });
     49         jrb2.addActionListener(new ActionListener(){
     50 
     51             @Override
     52             public void actionPerformed(ActionEvent arg0) {
     53                 // TODO Auto-generated method stub
     54                 myColor = Color.BLUE;
     55                 tP.setColor = true;                
     56                 repaint();
     57             }
     58             
     59         });
     60         jrb3.addActionListener(new ActionListener(){
     61 
     62             @Override
     63             public void actionPerformed(ActionEvent arg0) {
     64                 // TODO Auto-generated method stub
     65                 myColor = Color.YELLOW;
     66                 tP.setColor = true;                
     67                 repaint();
     68             }
     69             
     70         });
     71     }
     72     public static void main(String[] args) {
     73         // TODO Auto-generated method stub
     74         Test_17_3 frame = new Test_17_3();
     75         frame.setTitle("Test_17_3");
     76         frame.setSize(300,200);
     77         frame.setLocationRelativeTo(null);
     78         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     79         frame.setVisible(true);
     80     }
     81     
     82     
     83     class trafficPanel extends JPanel{
     84         public boolean setColor = false;
     85         
     86         protected void paintComponent(Graphics g){
     87             int xCenter = getWidth()/2;
     88             int yCenter = getHeight()/2;
     89             
     90             int radius = getHeight() /6 - 5;
     91             /** draw steady shape */
     92             //draw rect
     93             g.drawRect(xCenter -radius,YSTART, 2*radius,getHeight()-20 -VGAP);
     94             //draw three circles
     95             g.drawOval(xCenter - radius + XGAP, YSTART + VGAP, 2*radius -2*XGAP , 2*radius-2*XGAP);
     96             g.drawOval(xCenter - radius + XGAP, YSTART + 2*radius + 2*VGAP,2*radius -2*XGAP , 2*radius-2*XGAP);
     97             g.drawOval(xCenter - radius + XGAP, YSTART + 4*radius + 3*VGAP,2*radius -2*XGAP , 2*radius-2*XGAP );
     98             
     99             if(setColor)
    100             {                
    101                 if(myColor == Color.RED)        
    102                 {g.setColor(Color.RED); g.fillOval(xCenter - radius + XGAP, YSTART + VGAP, 2*radius -2*XGAP , 2*radius-2*XGAP);}
    103                 else if(myColor == Color.BLUE)
    104                 {g.setColor(Color.BLUE); g.fillOval(xCenter - radius + XGAP, YSTART + 2*radius + 2*VGAP,2*radius -2*XGAP , 2*radius-2*XGAP);} 
    105                 else
    106                 {g.setColor(Color.YELLOW);g.fillOval(xCenter - radius + XGAP, YSTART + 4*radius + 3*VGAP,2*radius -2*XGAP , 2*radius-2*XGAP );}                                
    107             }
    108         }
    109     }
    110 }
    Test_17_3.java
  • 相关阅读:
    学Python要避免哪些坑,如何巩固好基础
    Python爬虫:现学现用xpath爬取豆瓣音乐
    福州大学软件工程1816 | W班 第10次作业[软件工程实践总结]
    福州大学软件工程1816 | W班 第10次作业[个人作业——软件产品案例分析]
    福州大学软件工程1816 | W班 第8次作业[团队作业,随堂小测——校友录]
    福州大学软件工程1816 | W班 第6次作业WordCount成绩排名
    福州大学软件工程1816 | W班 第4次作业(团队展示)成绩排名
    福州大学软件工程1816 | W班 第2次作业成绩排名
    软件工程github使用小结
    2018北航软工教学培训小结
  • 原文地址:https://www.cnblogs.com/wanjiang/p/5712675.html
Copyright © 2020-2023  润新知