zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    优化后的组合算法
    Android之——AsyncTask和Handler对照
    LeetCode(30) Substring with Concatenation of All Words
    HDU--1054--Strategic Game【最小点覆盖】
    最小生成树模板(poj3625)
    QT中使用高速排序
    走进APICloud的世界 (1)
    layer,一个可以让你想到即可做到的javascript弹窗(层)解决方案
    解决Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future:
    移动端H5的一些基本知识点总结
  • 原文地址:https://www.cnblogs.com/wanjiang/p/5712675.html
Copyright © 2011-2022 走看看