import java.applet.*;
import java.awt.*;
import java.awt.event.*;
abstract class shape { //抽象基类
int x,y;
int width,high;
}
class Circle extends shape //画椭圆
{
public Circle(){}
public Circle(int x1,int y1,int x2,int y2) //x1,y1,表圆心,x2表width,y3表height
{
x=x1;
y=y1;
width=x2-x1;
high=y2-y1;
}
public void paint(Graphics g)
{
g.fillOval(x,y,width,high); //x1,y1,表圆心,x2表width,y3表height
}
}class Rectangle extends shape
{
public Rectangle(){}
public Rectangle(int x1,int y1,int x2,int y2) //x1,y1,表左上角,x2表width,y3表height
{
x=x1;
y=y1;
width=x2-x1;
high=y2-y1;
}
public void paint(Graphics g)
{
g.fillRect(x,y,width,high); //x1,y1,表左上角,x2表width,y3表height
}
}
public class Usetest extends Applet implements ActionListener
{
Button bt1,bt2;
Label prompt;
TextField tex1,tex2,tex3,tex4,tex5,tex6,tex7,tex8;
int flag=0;
public void init() //初始化
{
TextField tex1=new TextField(5);
TextField tex2=new TextField(5);
TextField tex3=new TextField(5);
TextField tex4=new TextField(5);
TextField tex5=new TextField(5);
TextField tex6=new TextField(5);
TextField tex7=new TextField(5);
TextField tex8=new TextField(5);
Button bt1=new Button("command1");
Button bt2=new Button("command2");
add(new Label("椭圆 x1,y1,x2,y2"));
add(tex1);
add(tex2);
add(tex3);
add(tex4);
add(bt1);
add(new Label("矩形 x1,y1,x2,y2"));
add(tex5);
add(tex6);
add(tex7);
add(tex8);
add(bt2);
bt1.addActionListener(this); //此处监听按钮
bt2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==bt1)
{
Circle circle=new Circle(Integer.parseInt(tex1.getText()),Integer.parseInt(tex2.getText()),Integer.parseInt(tex3.getText()),Integer.parseInt(tex4.getText()));
circle.paint(Graphics g); //继承此处有错误,不知道要怎么改
}
if(e.getSource()==bt2)
{
Rectangle rectangle=new Rectangle(Integer.parseInt(tex5.getText()),Integer.parseInt(tex6.getText()),Integer.parseInt(tex7.getText()),Integer.parseInt(tex8.getText()));
rectangle.paint(Graphics g); //继承此处有错误,不知道要怎么改
}
}
}
import java.awt.*;
import java.awt.event.*;
abstract class shape { //抽象基类
int x,y;
int width,high;
}
class Circle extends shape //画椭圆
{
public Circle(){}
public Circle(int x1,int y1,int x2,int y2) //x1,y1,表圆心,x2表width,y3表height
{
x=x1;
y=y1;
width=x2-x1;
high=y2-y1;
}
public void paint(Graphics g)
{
g.fillOval(x,y,width,high); //x1,y1,表圆心,x2表width,y3表height
}
}class Rectangle extends shape
{
public Rectangle(){}
public Rectangle(int x1,int y1,int x2,int y2) //x1,y1,表左上角,x2表width,y3表height
{
x=x1;
y=y1;
width=x2-x1;
high=y2-y1;
}
public void paint(Graphics g)
{
g.fillRect(x,y,width,high); //x1,y1,表左上角,x2表width,y3表height
}
}
public class Usetest extends Applet implements ActionListener
{
Button bt1,bt2;
Label prompt;
TextField tex1,tex2,tex3,tex4,tex5,tex6,tex7,tex8;
int flag=0;
public void init() //初始化
{
TextField tex1=new TextField(5);
TextField tex2=new TextField(5);
TextField tex3=new TextField(5);
TextField tex4=new TextField(5);
TextField tex5=new TextField(5);
TextField tex6=new TextField(5);
TextField tex7=new TextField(5);
TextField tex8=new TextField(5);
Button bt1=new Button("command1");
Button bt2=new Button("command2");
add(new Label("椭圆 x1,y1,x2,y2"));
add(tex1);
add(tex2);
add(tex3);
add(tex4);
add(bt1);
add(new Label("矩形 x1,y1,x2,y2"));
add(tex5);
add(tex6);
add(tex7);
add(tex8);
add(bt2);
bt1.addActionListener(this); //此处监听按钮
bt2.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==bt1)
{
Circle circle=new Circle(Integer.parseInt(tex1.getText()),Integer.parseInt(tex2.getText()),Integer.parseInt(tex3.getText()),Integer.parseInt(tex4.getText()));
circle.paint(Graphics g); //继承此处有错误,不知道要怎么改
}
if(e.getSource()==bt2)
{
Rectangle rectangle=new Rectangle(Integer.parseInt(tex5.getText()),Integer.parseInt(tex6.getText()),Integer.parseInt(tex7.getText()),Integer.parseInt(tex8.getText()));
rectangle.paint(Graphics g); //继承此处有错误,不知道要怎么改
}
}
}
import java.awt.*;
import java.awt.event.*;abstract class shape { // 抽象基类
int x, y; int width, high;
}class Circle extends shape // 画椭圆
{
public Circle() {
} public Circle(int x1, int y1, int x2, int y2) // x1,y1,表圆心,x2表width,y3表height
{
x = x1;
y = y1;
width = x2 - x1;
high = y2 - y1;
} public void paint(Graphics g) {
g.fillOval(x, y, width, high); // x1,y1,表圆心,x2表width,y3表height
}
}class Rectangle extends shape {
public Rectangle() {
} public Rectangle(int x1, int y1, int x2, int y2) // x1,y1,表左上角,x2表width,y3表height
{
x = x1;
y = y1;
width = x2 - x1;
high = y2 - y1;
} public void paint(Graphics g) {
g.fillRect(x, y, width, high); // x1,y1,表左上角,x2表width,y3表height
}
}public class UseTest extends Applet implements ActionListener {
Button bt1, bt2; Label prompt; TextField tex1, tex2, tex3, tex4, tex5, tex6, tex7, tex8; int flag = 0;
Circle circle;
Rectangle rectangle;
public void init() // 初始化
{
TextField tex1 = new TextField(5);
TextField tex2 = new TextField(5);
TextField tex3 = new TextField(5);
TextField tex4 = new TextField(5);
TextField tex5 = new TextField(5);
TextField tex6 = new TextField(5);
TextField tex7 = new TextField(5);
TextField tex8 = new TextField(5);
Button bt1 = new Button("画椭圆");
Button bt2 = new Button("画矩形");
add(new Label("椭圆 x1,y1,x2,y2"));
add(tex1);
add(tex2);
add(tex3);
add(tex4);
add(bt1);
add(new Label("矩形 x1,y1,x2,y2"));
add(tex5);
add(tex6);
add(tex7);
add(tex8);
add(bt2);
bt1.addActionListener(this); // 此处监听按钮
bt2.addActionListener(this);
setSize(600,600);
} public void actionPerformed(ActionEvent e){
if(e.getSource()==bt1){
circle=new Circle(Integer.parseInt(tex1.getText()),Integer.parseInt(tex2.getText()),Integer.parseInt(tex3.getText()),Integer.parseInt(tex4.getText()));
repaint();
}
if(e.getSource()==bt2){
rectangle=new Rectangle(Integer.parseInt(tex5.getText()),Integer.parseInt(tex6.getText()),Integer.parseInt(tex7.getText()),Integer.parseInt(tex8.getText()));
repaint();
}
}
public void paint(Graphics g){
if(circle!=null)
circle.paint(g);
if(rectangle!=null)
rectangle.paint(g);
}
}
import java.awt.*;
import java.awt.event.*;abstract class shape {
int x, y; int width, high;
}class Circle extends shape {
public Circle() {
} public Circle(int x1, int y1, int x2, int y2) {
x = x1;
y = y1;
width = x2 - x1;
high = y2 - y1;
} public void paint(Graphics g) {
g.drawOval(x, y, width, high);
}
}class Rectangle extends shape {
public Rectangle() {
} public Rectangle(int x1, int y1, int x2, int y2) {
x = x1;
y = y1;
width = x2 - x1;
high = y2 - y1;
} public void paint(Graphics g) {
g.drawRect(x, y, width, high);
}
}public class UseTest extends Applet implements ActionListener {
Button bt1, bt2; Label prompt; TextField tex1, tex2, tex3, tex4, tex5, tex6, tex7, tex8; int flag = 0; Circle circle; Rectangle rectangle; public void init() {
tex1 = new TextField(5);
tex2 = new TextField(5);
tex3 = new TextField(5);
tex4 = new TextField(5);
tex5 = new TextField(5);
tex6 = new TextField(5);
tex7 = new TextField(5);
tex8 = new TextField(5);
bt1 = new Button("画椭圆");
bt2 = new Button("画矩形");
add(new Label("椭圆 x1,y1,x2,y2"));
add(tex1);
add(tex2);
add(tex3);
add(tex4);
add(bt1);
add(new Label("矩形 x1,y1,x2,y2"));
add(tex5);
add(tex6);
add(tex7);
add(tex8);
add(bt2);
bt1.addActionListener(this);
bt2.addActionListener(this);
setSize(600, 600);
} public void actionPerformed(ActionEvent e) {
if (e.getSource() == bt1) {
circle = new Circle(Integer.parseInt(tex1.getText()), Integer
.parseInt(tex2.getText()),
Integer.parseInt(tex3.getText()), Integer.parseInt(tex4
.getText()));
repaint();
}
if (e.getSource() == bt2) {
rectangle = new Rectangle(Integer.parseInt(tex5.getText()), Integer
.parseInt(tex6.getText()),
Integer.parseInt(tex7.getText()), Integer.parseInt(tex8
.getText()));
repaint();
}
} public void paint(Graphics g) {
g.setColor(Color.BLUE);
if (circle != null)
circle.paint(g);
if (rectangle != null)
rectangle.paint(g);
}
}