您的位置:首页 > 编程语言 > Java开发

Java图形界面-画图程序

2015-08-06 17:53 351 查看


//主函数
package shapes;

public class MyPic {
public static void main(String[] args)
{
Picture pic = new Picture(420,300);
//Rectangle r1 = new Rectangle(100, 100, 100, 100);
//Triangle t1 = new Triangle(100, 100, 200, 100, 150, 50);
Line l1 = new Line(149,230,149,400);
Line l2 = new Line(151,230,151,400);
Circle c1 = new Circle(150,150,20);
Circle c2 = new Circle(150,150,40);
Circle c3 = new Circle(150,150,60);
Circle c4 = new Circle(150,150,80);
Src    b1 = new Src(217,-125,500,500,150,120);
//pic.add(r1);
//pic.add(t1);
pic.add(l1);
pic.add(l2);
pic.add(c1);
pic.add(c2);
pic.add(c3);
pic.add(c4);
pic.add(b1);
pic.draw();
}
}

//类:Circle
package shapes;

import java.awt.Graphics;

public class Circle extends Shape {
private int x;
private int y;
private int radius;

public Circle(int x, int y, int radius)
{
this.x = x;
this.y = y;
this.radius = radius;
}
@Override
public void draw(Graphics g) {
g.drawOval(x-radius, y-radius, radius*2, radius*2);
}
}

//类:Line
package shapes;

import java.awt.Graphics;

public class Line extends Shape {
private int x1;
private int y1;
private int x2;
private int y2;

public Line(int x1, int y1, int x2, int y2)
{
this.x1 = x1; this.y1 = y1;
this.x2 = x2; this.y2 = y2;
}

@Override
public void draw(Graphics g) {
g.drawLine(x1, y1, x2, y2);
}

}

//类:Triangle
package shapes;

import java.awt.Graphics;

public class Triangle extends Shape {
private int[] x = new int[3];
private int[] y = new int[3];

public Triangle(int x1, int y1, int x2, int y2, int x3, int y3)
{
x[0] = x1; x[1] = x2; x[2] = x3;
y[0] = y1; y[1] = y2; y[2] = y3;
}

@Override
public void draw(Graphics g) {
g.drawPolygon(x, y, x.length);
}
}

//类:Picture
package shapes;

import java.awt.Graphics;

public class Triangle extends Shape {
private int[] x = new int[3];
private int[] y = new int[3];

public Triangle(int x1, int y1, int x2, int y2, int x3, int y3)
{
x[0] = x1; x[1] = x2; x[2] = x3;
y[0] = y1; y[1] = y2; y[2] = y3;
}

@Override
public void draw(Graphics g) {
g.drawPolygon(x, y, x.length);
}

}

//类:Rectangle
package shapes;

import java.awt.Graphics;

public class Rectangle extends Shape {
private int x;
private int y;
private int width;
private int height;

public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}

@Override
public void draw(Graphics g) {
g.drawRect(x, y, width, height);
}

}

//类:Shape
package shapes;

import java.awt.Graphics;

public abstract class Shape {

public abstract void draw(Graphics g);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: