中易网

Java类的定义

答案:2  悬赏:70  
解决时间 2021-01-13 19:44
Java类的定义
最佳答案
package java.lancs ;



import java.awt.* ;
import java.awt.event.* ;


class BasicShape
{
// name of the shape - RECTANGLE, OVAL, etc.
int shape ;
// dimensions of the shape
int x, y, w, h ;
// colour of the shape
Color colour ;

// constructor to initialise the variables to default values
public BasicShape()
{
shape = -1 ;
x = -1 ;
y = -1 ;
w = -1 ;
h = -1 ;
colour = Color.green ;
} // end of constructor method

// constructor to initialise the variables to specifier values
public BasicShape(int sh, int x1, int y1, int w1, int h1, Color col)
{
shape = sh ;
x = x1 ;
y = y1 ;
w = w1 ;
h = h1 ;
colour = col ;
} // end of constructor method
} // end of class BasicShape


class BasicCanvas extends Canvas
{
BasicGraphics parent ;

// constructor method
public BasicCanvas(BasicGraphics p)
{
parent = p ;
} // end of constructor method

// called when class is initialised to put window on the screen
// or when window needs to be redrawn
public void paint(Graphics g)
{
Dimension d = getSize() ;
int cx = d.width / 2,
cy = d.height /2 ;
g.setColor(Color.black) ;
g.drawRect(1, 1, d.width - 3, d.height - 3) ;
int yy = 25 ;
while (yy < d.height)
{
if (yy % 100 == 0)
{
g.drawLine(1, yy, 11, yy) ;
g.drawLine(d.width - 13, yy, d.width - 3, yy) ;
}
else
{
g.drawLine(1, yy, 6, yy) ;
g.drawLine(d.width - 8, yy, d.width - 3, yy) ;
}
yy += 25 ;
}
int xx = 25 ;
while (xx < d.width)
{
if (xx % 100 == 0)
{
g.drawLine(xx, 1, xx, 11) ;
g.drawLine(xx, d.height - 13, xx, d.height - 3) ;
}
else
{
g.drawLine(xx, 1, xx, 6) ;
g.drawLine(xx, d.height - 8, xx, d.height - 3) ;
}
xx += 25 ;
}

for (int i = 0 ; i < parent.noOfShapes ; i++)
{
g.setColor(parent.shapeList[i].colour) ;

if (parent.shapeList[i].shape == BasicGraphics.RECTANGLE)
{
g.drawRect(parent.shapeList[i].x, parent.shapeList[i].y,
parent.shapeList[i].w, parent.shapeList[i].h) ;
}
else if (parent.shapeList[i].shape == BasicGraphics.FILLED_RECTANGLE)
{
g.fillRect(parent.shapeList[i].x, parent.shapeList[i].y,
parent.shapeList[i].w, parent.shapeList[i].h) ;
}
else if (parent.shapeList[i].shape == BasicGraphics.OVAL)
{
g.drawOval(parent.shapeList[i].x, parent.shapeList[i].y,
parent.shapeList[i].w, parent.shapeList[i].h) ;
}
else if (parent.shapeList[i].shape == BasicGraphics.FILLED_OVAL)
{
g.fillOval(parent.shapeList[i].x, parent.shapeList[i].y,
parent.shapeList[i].w, parent.shapeList[i].h) ;
}
else if ((parent.shapeList[i].shape == BasicGraphics.TRIANGLE) ||
(parent.shapeList[i].shape == BasicGraphics.FILLED_TRIANGLE))
{
int x1 = parent.shapeList[i].x ;
int y1 = parent.shapeList[i].y ;
int w1 = parent.shapeList[i].w ;
int h1 = parent.shapeList[i].h ;

Polygon p = new Polygon() ;
p.addPoint(x1, y1 + h1) ;
p.addPoint(x1 + w1, y1 + h1) ;
p.addPoint(x1 + (w1 / 2), y1) ;
p.addPoint(x1, y1 + h1) ;

if (parent.shapeList[i].shape == BasicGraphics.TRIANGLE)
g.drawPolygon(p) ;
else
g.fillPolygon(p) ;
}
}
} // end of method paint

} // end of class BasicCanvas


public class BasicGraphics extends Frame implements ActionListener
{
// maximum width of window
private static final int MAX_WIDTH = 600 ;

// maximum height of window
private static final int MAX_HEIGHT = 400 ;


public static final int RECTANGLE = 1 ;


public static final int OVAL = 2 ;


public static final int TRIANGLE = 3 ;


public static final int FILLED_RECTANGLE = 4 ;


public static final int FILLED_OVAL = 5 ;


public static final int FILLED_TRIANGLE = 6 ;

BasicShape[] shapeList = new BasicShape[50];

int noOfShapes = 0;

private BasicShape newShape = new BasicShape();

private Button quit ;


public BasicGraphics()
{
setTitle("BasicGraphics Window") ;
setSize(MAX_WIDTH, MAX_HEIGHT + 50) ;

BasicCanvas c = new BasicCanvas(this) ;
add("Center", c) ;

Panel p = new Panel() ;
p.setLayout(new FlowLayout()) ;
quit = new Button("Quit") ;
p.add(quit) ;
quit.addActionListener(this) ;
add("South", p) ;
} // end of constructor method


public void actionPerformed(ActionEvent event)
{
dispose() ;
System.exit(0) ;
} // end of method actionPerformed


public void setShape(int shape)
{
if ((shape != RECTANGLE) && (shape != FILLED_RECTANGLE) &&
(shape != OVAL) && (shape != FILLED_OVAL) &&
(shape != TRIANGLE) && (shape != FILLED_TRIANGLE))
{
System.err.println("This is not a valid shape");
System.exit(1);
}
newShape.shape = shape ;
} // end of method setShape


public void setDimensions(int x, int y, int w, int h)
{
if (newShape.shape == -1)
{
System.err.println("You need to set the shape first");
System.exit(1);
}
if ((x < 5) || (y < 5) || (w < 5) || (h < 5) ||
(x + w > MAX_WIDTH - 5) || (y + h > MAX_HEIGHT - 5))
{
System.err.println("Invalid dimensions supplied") ;
System.exit(1);
}
newShape.x = x ;
newShape.y = y ;
newShape.w = w ;
newShape.h = h ;
} // end of method setDimensions


public void setColour(Color colour)
{
if (newShape.x == -1)
{
System.err.println("You need to set the dimensions first");
System.exit(1);
}
newShape.colour = colour ;
shapeList[noOfShapes] = new BasicShape(newShape.shape,
newShape.x, newShape.y,
newShape.w, newShape.h,
newShape.colour) ;
noOfShapes++ ;
newShape = new BasicShape() ;
} // end of method setColour


public void draw()
{
setVisible(true) ;
} // end of method draw

} // end of class BasicGraphics
全部回答
class a{
private l int ; //长
private w int ; //宽
public int getValue(int l,int w)
{
return l*w; //计算其面积
}
}
……晕不想写来。这个是作业题。帮你写了 太不道德了,
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
陶大宇 哎呀女朋友
高端智慧社区可以安装哪种访客注册系统?
台湾比美国还民主,为什么台湾精英都想移民美
元代早期延佑期民窑青花碗价格如何
哪个是画的
一个学校,男老师的平均年龄是30岁,女老师的平
杰尼亚的骑士香水怎么样
这个桃子是什么品种
简历上我想把在家学习的职业空白期写上去,我
win10 movie maker在哪
vivoy66i手机屏锁密码忘了怎么办
世界杯有几个国家可以参与,是怎么角逐进入的
思绪还是WwW仅停留在1122fp最初的样子,或者
ADC和DAC有什么区别?
我总是在担心失去谁 现在想想 有谁会担心失
推荐资讯
那部电视剧孙悟空武力全失
280分能上什么大学
辽宁市大连省到辽宁市辽宁省有多少小时的路程
综合油耗最低为1.5l100km是什么意思
开心得想哭是什么感觉
蒙自哪里卖平板电脑
wakeup地址好找么,我有些事要过去
王者荣耀ios有限时吗
母女俩你想上哪个
劳动法我代课十三年被辞退我该怎么办
vivox5qq邮箱登录不了怎么办
如何试探女情人喜不喜欢你
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?