用RadioButton做改变多边形图案(以五角星为例)颜色

第一步:设置界面

在窗体中设置三个radioButton组件,改变属性为各自颜色(红绿蓝)

添加一个panel1组件,用以显示多边形

可以添加一个lable组件,用以指示功能,文本设置为"选择绘制图形的颜色:"

第二步:设置属性

第三步:写代码

//红色多边形

private void radioButton1_Click(object sender, EventArgs e)

  {

Pen p1=new.Pen(Color.Red, 5);//创建笔

Graphics g1= panel1.CreateGraphics(); //创建画板

Brash a=new SolidBrush(Color.Red);//创建实体笔刷

//创建点

Point p1=new Point(0,76);

Point p2= new Point(80,76);

Point p3=new Point (106,0);

Point p4=new Point (130,76);

Point p5= new Point (210,76);

Point p6= new Point ((146,124);

Point p7= new Point (170,200);

Point p8=new Point (106,152);

Point p9=new Point (40,200);

Point p10=new Point (66, 124);

Point[]myPoints={p1,p2,p3,p4,p5,p6,p7,p8,p9,p10}

g.DrawPolygon(pen, myPoint);//用笔pen绘制多边

g1.FillPolgon(a,DrawPolgon);//填充多边形

//绿色多边形

private void radioButton2_Click(object sender, EventArgs e)

  {

Pen p1=new.Pen(Color.Green, 5);

Graphics g1= panel1.CreateGraphics(); 

Brash a=new SolidBrush(Color.Green);

Point p1=new Point(0,76);

Point p2= new Point(80,76);

Point p3=new Point (106,0);

Point p4=new Point (130,76);

Point p5= new Point (210,76);

Point p6= new Point ((146,124);

Point p7= new Point (170,200);

Point p8=new Point (106,152);

Point p9=new Point (40,200);

Point p10=new Point (66, 124);

Point[]myPoints={p1,p2,p3,p4,p5,p6,p7,p8,p9,p10}

g.DrawPolygon(pen, myPoint);

g1.FillPolgon(a,myPoints);

//蓝色多边形

private void radioButton2_Click(object sender, EventArgs e)

  {

Pen p1=new.Pen(Color.Blue, 5);

Graphics g1= panel1.CreateGraphics(); 

Brash a=new SolidBrush(Color.Blue);

Point p1=new Point(0,76);

Point p2= new Point(80,76);

Point p3=new Point (106,0);

Point p4=new Point (130,76);

Point p5= new Point (210,76);

Point p6= new Point ((146,124);

Point p7= new Point (170,200);

Point p8=new Point (106,152);

Point p9=new Point (40,200);

Point p10=new Point (66, 124);

Point[]myPoints={p1,p2,p3,p4,p5,p6,p7,p8,p9,p10}

g.DrawPolygon(pen, myPoint);

g1.FillPolgon(a,myPoints);

}}}

要实现在选中 `RadioButton` 时改变字体大小或字体颜色,可以使用以下方法: 1. 在布局文件中设置 `RadioButton` 的属性 `android:textColor` 或 `android:textSize`,并设置默认值。 2. 在 Java 代码中获取 `RadioButton` 对象,然后设置 `OnCheckedChangeListener` 监听器。 3. 在监听器的回调方法中,根据选中状态修改字体大小或字体颜色。 下面是一个示例代码: ```xml <RadioGroup android:id="@+id/radio_group" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 设置默认字体颜色和大小 --> <RadioButton android:id="@+id/radio_button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 1" android:textColor="@color/default_text_color" android:textSize="16sp" /> <RadioButton android:id="@+id/radio_button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 2" android:textColor="@color/default_text_color" android:textSize="16sp" /> </RadioGroup> ``` ```java public class MainActivity extends AppCompatActivity { private RadioButton radioButton1, radioButton2; private RadioGroup radioGroup; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); radioGroup = findViewById(R.id.radio_group); radioButton1 = findViewById(R.id.radio_button1); radioButton2 = findViewById(R.id.radio_button2); // 设置监听器 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // 判断选中的 RadioButton if (checkedId == R.id.radio_button1) { // 修改字体颜色和大小 radioButton1.setTextColor(Color.RED); radioButton1.setTextSize(20); // 恢复另一个 RadioButton 的默认样式 radioButton2.setTextColor(getResources().getColor(R.color.default_text_color)); radioButton2.setTextSize(16); } else if (checkedId == R.id.radio_button2) { radioButton2.setTextColor(Color.BLUE); radioButton2.setTextSize(20); radioButton1.setTextColor(getResources().getColor(R.color.default_text_color)); radioButton1.setTextSize(16); } } }); } } ``` 以上代码中,我们通过 `setOnCheckedChangeListener` 方法为 `RadioGroup` 添加了一个监听器,在选中状态改变时修改字体颜色和大小。注意,在修改字体颜色时需要使用 `Color` 类或 `getResources().getColor()` 方法获取颜色值。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值