Java图形界面-画图程序

通过程序画的  棒棒糖  图片

//主函数
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);

}


  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值