我基于你原來畫圖的方法,添加了事件觸發(fā)的命令b[j].setActionCommand("b" + j);否則你不能在事件響應(yīng)處理的方法中使用e.getActionCommand(),而且字符串的比較用equals方法比較好?,F(xiàn)在可以運行了,你可以看一下:
站在用戶的角度思考問題,與客戶深入溝通,找到東港網(wǎng)站設(shè)計與東港網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網(wǎng)站設(shè)計、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、空間域名、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋東港地區(qū)。
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); }
*/
}
應(yīng)該先調(diào)用父類的paint,即super.paint(g),不過最好的方法還是繼承出一個JPanel的子類,然后重載它的paint方法,然后把這個子類的實例增加到JFrame的容器里
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
//不規(guī)則圖形的繪制
public class IrregularShapeDemo extends JFrame {
GeneralPath gPath= new GeneralPath(); //GeneralPath對象實例
Point aPoint;
//構(gòu)造函數(shù)
public IrregularShapeDemo() {
super("不規(guī)則圖形的繪制"); //調(diào)用父類構(gòu)造函數(shù)
enableEvents(AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK); //允許事件
setSize(300, 200); //設(shè)置窗口尺寸
setVisible(true); //設(shè)置窗口可視
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //關(guān)閉窗口時退出程序
}
public void paint(Graphics g) { //重載窗口組件的paint()方法
Graphics2D g2D = (Graphics2D)g;//獲取圖形環(huán)境
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); //設(shè)置路徑點
}
}
protected void processMouseMotionEvent(MouseEvent e) { //鼠標運動事件處理
if(e.getID() == MouseEvent.MOUSE_DRAGGED) {
aPoint = e.getPoint(); //得到當前鼠標點
gPath.lineTo(aPoint.x, aPoint.y); //設(shè)置路徑
gPath.moveTo(aPoint.x, aPoint.y);
repaint(); //重繪組件
}
}
}
按照題目要求編寫的Java程序如下
注意 請使用你的真實姓名和班級替換Test類中
創(chuàng)建Student對象stu時用的"張三"和"20計算機應(yīng)用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+"同學(xué),你所在的班級是"+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計算機應(yīng)用01班",5);
stu.setStarNum(4);
stu.printStar();
stu.setScore();
stu.showInfo();
if(stu.getAvg()60){
System.out.println(stu.getName()+"是不合格學(xué)生");
}else{
System.out.println(stu.getName()+"是個合格學(xué)生");
}
}
}