java 找不到Graphics 变量g?初学者,求高人指导。
发布网友
发布时间:2022-04-26 01:26
我来回答
共2个回答
热心网友
时间:2022-06-19 11:38
有成员函数没传参数graphics g,下面是我修改的:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
public abstract class MyApplet1 extends Applet implements ActionListener
{
Label name;
Button btn_up,btn_down;
int iCount=0;
int iChange=1;
//-------------------------
public void init() //-------初始化
{
name=new Label("绘制图形");
btn_up=new Button("上一图");
btn_down=new Button("下一图");
//----------------------------
add(name);
add(btn_up);
add(btn_down);
btn_up.addActionListener(this);
btn_down.addActionListener(this);
}
public void actionPerfomed(ActionEvent e,Graphics g) //---------图像显示控制(利用Button)
{
if(e.getSource()==btn_down)
iChange=1;
if(e.getSource()==btn_up)
iChange=-1;
FiguresChange(g);
}
public void FiguresChange(Graphics g) //----------图形显示方法(利用Graphics)
{
if(iCount==0&iChange==-1) //*注意等号写法
iCount=0;
else
iCount=iCount+iChange;
//-----------------@下方出现错误@---------------
FiguresPaint(g); //----------调用画图方法
}
public void FiguresPaint(Graphics g)
{
//-----------------@上方出现错误@---------------
switch (iCount)
{
case 0: g.clearRect(0, 0, 400,350);
break;
case 1: g.drawString("绘制图像",180,40);
break;
case 2: g.drawLine(100,200,300,200);
break;
case 3: g.drawRect(100,100,200,200);
break;
case 4: g.fillRect(100,100,200,200);
break;
case 5: g.drawRoundRect(100,100,200,200,30,40);
break;
case 6: g.drawOval(100,100,200,200);
break;
case 7: g.fillOval(100,100,200,200);
break;
default: DrawMyFigures(g);
}
}
public void DrawMyFigures(Graphics g)
{
int[] xCoods={100,125,145,180,240,300};
int[] yCoods={100,120,150,200,250,300};
g.drawPolygon(xCoods,yCoods,6);
}
}
热心网友
时间:2022-06-19 11:39
修改如下:
Graphics g = getGraphics(); // 先获取父类的graphics实例
// 你这里的g,凭空冒出,谁也不知道它是啥,所以报错
FiguresPaint(g); //----------调用画图方法