我基于你原来画图的方法,添加了事件触发的命令b[j].setActionCommand("b" + j);否则你不能在事件响应处理的方法中使用e.getActionCommand(),而且字符串的比较用equals方法比较好。现在可以运行了,你可以看一下:
站在用户的角度思考问题,与客户深入沟通,找到东港网站设计与东港网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站设计、网站制作、企业官网、英文网站、手机端网站、网站推广、空间域名、网站空间、企业邮箱。业务覆盖东港地区。
import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class drawing extends Applet implements ActionListener {
Button b[] = new Button[5];
String fontname = "仿宋_GB2312";
int style = Font.PLAIN;
int size = 24;
int index = 0;
Font myfont;
public void init() {
setSize(700,700);
myfont = new Font(fontname, style, size);
b[0] = new Button("扇形");
b[1] = new Button("圆形");
b[2] = new Button("三角形");
b[3] = new Button("长方形");
b[4] = new Button("椭圆形");
for (int j = 0; j b.length; j++) {
b[j].setBounds(10, 10, 50, 20);
b[j].addActionListener(this);
b[j].setActionCommand("b" + j);
add(b[j]);
}
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("b0")) {
index = 0;
repaint();
}
if (e.getActionCommand().equals("b1")) {
index = 1;
repaint();
}
if (e.getActionCommand().equals("b2")) {
index = 2;
repaint();
}
if (e.getActionCommand().equals("b3")) {
index = 3;
repaint();
}
if (e.getActionCommand().equals("b4")) {
index = 4;
repaint();
}
}
public void paint(Graphics g) {
switch (index) {
case 0:
g.fillArc(0, 60, 80, 60, 30, 120);
break;
case 1:
g.drawOval( 300, 50, 60, 60);
break;
case 2:
Polygon filledPolygon = new Polygon();
filledPolygon.addPoint(380, 50);
filledPolygon.addPoint(380, 110);
filledPolygon.addPoint(450, 90);
g.drawPolygon(filledPolygon);
break;
case 3:
g.drawRect( 200, 50, 80, 60);
break;
case 4:
g.drawOval(100, 50, 80, 60);
break;
default:
g.fillArc(0, 60, 80, 60, 30, 120);
break;
}
}
/*
* public void paint(Graphics g) { g.fillArc( 0, 60, 80, 60, 30, 120);
* //绘制扇形 g.drawOval( 100, 50, 80, 60); g.drawRect( 200, 50, 80, 60);
* g.drawOval( 300, 50, 60, 60); Polygon filledPolygon=new Polygon();
* filledPolygon.addPoint(380,50); filledPolygon.addPoint(380,110);
* filledPolygon.addPoint(450,90); g.drawPolygon(filledPolygon); }
*/
}
应该先调用父类的paint,即super.paint(g),不过最好的方法还是继承出一个JPanel的子类,然后重载它的paint方法,然后把这个子类的实例增加到JFrame的容器里
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
//不规则图形的绘制
public class IrregularShapeDemo extends JFrame {
GeneralPath gPath= new GeneralPath(); //GeneralPath对象实例
Point aPoint;
//构造函数
public IrregularShapeDemo() {
super("不规则图形的绘制"); //调用父类构造函数
enableEvents(AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK); //允许事件
setSize(300, 200); //设置窗口尺寸
setVisible(true); //设置窗口可视
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}
public void paint(Graphics g) { //重载窗口组件的paint()方法
Graphics2D g2D = (Graphics2D)g;//获取图形环境
g2D.draw(gPath); //绘制路径
}
public static void main(String[] args) {
new IrregularShapeDemo();
}
protected void processMouseEvent(MouseEvent e) { //鼠标事件处理
if(e.getID() == MouseEvent.MOUSE_PRESSED) {
aPoint = e.getPoint(); //得到当前鼠标点
gPath = new GeneralPath(); //重新实例化GeneralPath对象
gPath.moveTo(aPoint.x,aPoint.y); //设置路径点
}
}
protected void processMouseMotionEvent(MouseEvent e) { //鼠标运动事件处理
if(e.getID() == MouseEvent.MOUSE_DRAGGED) {
aPoint = e.getPoint(); //得到当前鼠标点
gPath.lineTo(aPoint.x, aPoint.y); //设置路径
gPath.moveTo(aPoint.x, aPoint.y);
repaint(); //重绘组件
}
}
}
按照题目要求编写的Java程序如下
注意 请使用你的真实姓名和班级替换Test类中
创建Student对象stu时用的"张三"和"20计算机应用01班"
import java.util.Scanner;
class Student{
private String name,classname;
private int starnum,scorenum;
private int[] scores;
public void setStarNum(int n){
this.starnum=n;
}
public Student(String name,String classname,int scorenum){
this.name=name;
this.classname=classname;
this.scorenum=scorenum;
}
public String getName(){
return this.name;
}
public void printStar(){
for(int i=0;istarnum;i++){
for(int j=0;j2*i+1;j++){
System.out.print("*");
}
System.out.println();
}
}
public void setScore(){
Scanner sc=new Scanner(System.in);
scores=new int[scorenum];
System.out.print("请输入各科成绩:");
for(int i=0;iscorenum;i++){
scores[i]=sc.nextInt();
}
}
public void showInfo(){
System.out.print(name+"同学,你所在的班级是"+classname+",你各科考试成绩分别为:");
for(int i=0;iscorenum;i++){
if(i==scorenum-1)
System.out.print(scores[i]);
else
System.out.print(scores[i]+",");
}
System.out.println();
}
public float getAvg(){
float sum=0;
for(int i=0;iscorenum;i++){
sum=sum+scores[i];
}
return sum/scorenum;
}
}
public class Test{
public static void main(String[] args){
Student stu=new Student("张三","20计算机应用01班",5);
stu.setStarNum(4);
stu.printStar();
stu.setScore();
stu.showInfo();
if(stu.getAvg()60){
System.out.println(stu.getName()+"是不合格学生");
}else{
System.out.println(stu.getName()+"是个合格学生");
}
}
}