JavaSE系列代码61:简单的绘图程序

Process refers to a “self-contained” running program with its own address space; thread is a single sequential control flow within the process. Based on the characteristics of the process, the computer is allowed to run two or more programs at the same time. In the multitasking environment based on thread, thread is the smallest processing unit.

import java.awt.*;
import java.awt.event.*;
public class Javase_61 extends Frame implements ActionListener
{
  static Javase_61 frm=new Javase_61 ();
  static Button bnt1 =new Button("画圆");
  static Button bnt2 =new Button("画椭圆");
  int circle=0;
  public static void main(String[] args)
  {
    frm.setTitle("简单绘图应用程序");
    frm.setSize(300,250);
    frm.setLayout(null);
    bnt1.setBounds(90, 215,50,25);
    bnt2.setBounds(160,215,50,25);
    frm.add(bnt1);   frm.add(bnt2);
    bnt1.addActionListener(frm);
    bnt2.addActionListener(frm);
    frm.setVisible(true);
  }
  public void actionPerformed(ActionEvent e)
  {
    Button bt=(Button)e.getSource();    //获取被按下的按钮
    if (bt==bnt1) circle=1;            //若按下的是“画圆”按钮
    else circle=2;                   //若按下的是“画椭圆”按钮
    Graphics g=getGraphics();         //获取窗口的绘图区
    paint(g);
  }
  public void paint(Graphics g)
  {
    g.setFont(new Font("楷体",Font.ITALIC,20));  //设置字体
    g.setColor(Color.red);     //设置颜色
    g.drawString("画圆或椭圆",120,50);    //以(120,50)为左下角显示字符串
    if (circle==1)  g.drawOval (100,90,70,70);         //画圆
    else if (circle==2) g.drawOval (80,60,70,120);       //画椭圆
  }
}
发布了73 篇原创文章 · 获赞 189 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/blog_programb/article/details/105566639