java绘图代码

功能要求:实现简单绘图功能,具体要求:画直线,画空心或实心椭圆、画空心或实心矩形、画多边形(图形的位置和大小可在程序中设定)。界面要求:用图形界面实现。... 功能要求:实现简单绘图功能,具体要求:画直线,画空心或实心椭圆、画空心或实心矩形、画多边形(图形的位置和大小可在程序中设定)。
界面要求:用图形界面实现。
展开
 我来答
hardneedl
推荐于2018-03-13 · TA获得超过921个赞
知道小有建树答主
回答量:1353
采纳率:57%
帮助的人:605万
展开全部

画笔定义

import java.awt.*;

/**

 * @author Hardneedl

 */

interface Brush {

    void doPaint(Graphics g);

}

画笔工厂

import java.awt.*;

/**

 * @author Hardneedl

 */

class BrushFactory {

    static final int LINE_BRUSH = 0;

    static final int ELLIPSE_BBRUSH = 1;

    static final int RECTANGLE_BRUSH = 2;

    static final int POLYGON_BRUSH = 3;

    static final int ELLIPSE_FILL_BRUSH = 4;

    static final int RECTANGLE_FILL_BRUSH = 5;

    static final Brush NO = new NONE();

    static final Brush LINE = new LineBrush();

    static final Brush ELLIPSE = new EllipseBrush();

    static final Brush ELLIPSE_FILL = new EllipseFillBrush();

    static final Brush RECTANGLE = new RectangleBrush();

    static final Brush RECTANGLE_FILL = new RectangleFillBrush();

    static final Brush POLYGON = new PolygonBrush();

    Brush getBrush(int brushIndex){

        switch(brushIndex){

            case LINE_BRUSH:return LINE;

            case ELLIPSE_BBRUSH:return ELLIPSE;

            case RECTANGLE_BRUSH:return RECTANGLE;

            case RECTANGLE_FILL_BRUSH:return RECTANGLE_FILL;

            case POLYGON_BRUSH:return POLYGON;

            case ELLIPSE_FILL_BRUSH:return ELLIPSE_FILL;

            default:return NO;

        }

    }

    private static final class LineBrush implements Brush {

        public void doPaint(Graphics g) {

            Graphics gg  =g.create();

            gg.setColor(Color.BLACK);

            gg.drawLine(70,70, 200,200);

            gg.dispose();

        }

    }

    private static final class EllipseBrush implements Brush {

        public void doPaint(Graphics g) {

            Graphics gg  =g.create();

            gg.setColor(Color.PINK);

            gg.drawOval(100,100,200,50);

            gg.dispose();

        }

    }

    private static final class EllipseFillBrush implements Brush {

        public void doPaint(Graphics g) {

            Graphics gg  =g.create();

            gg.setColor(Color.PINK);

            gg.fillOval(100,100,200,50);

            gg.dispose();

        }

    }

    private static final class PolygonBrush implements Brush {

        public void doPaint(Graphics g) {

            Graphics gg  =g.create();

            gg.setColor(Color.RED);

            gg.drawPolygon(new int[]{48,50,244,483,310}, new int[]{36,192,281,302,77}, 5);

            gg.dispose();

        }

    }

    private static final class RectangleBrush implements Brush {

        public void doPaint(Graphics g) {

            Graphics gg  =g.create();

            gg.setColor(Color.BLUE);

            gg.drawRect(70,70,100,100);

            gg.dispose();

        }

    }

    private static final class RectangleFillBrush implements Brush {

        public void doPaint(Graphics g) {

            Graphics gg  =g.create();

            gg.setColor(Color.BLUE);

            gg.fillRect(70,70,100,100);

            gg.dispose();

        }

    }

    private static final class NONE implements Brush {

        public void doPaint(Graphics g) {

            Graphics gg  =g.create();

            gg.setColor(Color.RED);

            gg.drawString("No brush selected!",20,30);

            gg.dispose();

        }

    }

}

图形界面

import javax.swing.*;

import javax.swing.border.*;

import java.awt.*;

import java.awt.event.*;

/**

 * @author Hardneedl

 */

final class Draw extends JFrame {

    public String getTitle() {return "frame title";}

    static private final Dimension size = new Dimension(600, 400);

    public Dimension getPreferredSize() {return size;}

    public Dimension getMaximumSize() {return size;}

    public Dimension getMinimumSize() {return size;}

    public Dimension getSize() {return size;}

    private static final Action lineAction = new LineAction();

    private static final Action rectangleAction=new RectangleAction();

    private static final Action rectangleFillAction=new RectangleFillAction();

    private static final Action ellipseAction=new EllipseAction();

    private static final Action ellipseFillAction=new EllipseFillAction();

    private static final Action polygonAction = new PolygonAction();

    private static final BrushFactory brushFactory = new BrushFactory();

    private static Brush brush;

    private final static JComponent canvas = new JComponent(){

        protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            if (brush!=null){

                brush.doPaint(g);

            }

        }

        public Border getBorder() {return BorderFactory.createLineBorder(Color.BLACK,2);}

    };

    Draw() throws HeadlessException {

        init();

        attachListeners();

        doLay();

    }

    private void init() {

        JMenuBar menuBar = new JMenuBar();

        menuBar.add(new JMenu(lineAction));

        JMenu elp = new JMenu("椭圆");

        elp.add(ellipseAction);

        elp.add(ellipseFillAction);

        menuBar.add(elp);

        JMenu rct = new JMenu("矩形");

        rct.add(rectangleAction);

        rct.add(rectangleFillAction);

        menuBar.add(rct);

        menuBar.add(new JMenu(polygonAction));

        setJMenuBar(menuBar);

    }

    private void attachListeners() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    private void doLay() {

        Container container = getContentPane();

        container.add(canvas,BorderLayout.CENTER);

        JPanel buttonsPane = new JPanel(new GridLayout(2,3));

        buttonsPane.add(new JButton(lineAction));

        buttonsPane.add(new JButton(ellipseAction));

        buttonsPane.add(new JButton(rectangleAction));

        buttonsPane.add(new JButton(polygonAction));

        buttonsPane.add(Box.createHorizontalBox());

        buttonsPane.add(new JButton(ellipseFillAction));

        buttonsPane.add(new JButton(rectangleFillAction));

        container.add(buttonsPane,BorderLayout.SOUTH);

        pack();

        setVisible(true);

    }

    private static final class RectangleAction extends AbstractAction {

        private RectangleAction() {super("空心矩形");}

        public void actionPerformed(ActionEvent e) {

            brush = brushFactory.getBrush(BrushFactory.RECTANGLE_BRUSH);

            canvas.repaint();

        }

    }

    private static final class RectangleFillAction extends AbstractAction {

        private RectangleFillAction() {super("实心矩形");}

        public void actionPerformed(ActionEvent e) {

            brush = brushFactory.getBrush(BrushFactory.RECTANGLE_FILL_BRUSH);

            canvas.repaint();

        }

    }

    private static final class EllipseFillAction extends AbstractAction {

        private EllipseFillAction() {super("实心椭圆");}

        public void actionPerformed(ActionEvent e) {

            brush = brushFactory.getBrush(BrushFactory.ELLIPSE_FILL_BRUSH);

            canvas.repaint();

        }

    }

    private static final class EllipseAction extends AbstractAction {

        private EllipseAction() {super("空心椭圆");}

        public void actionPerformed(ActionEvent e) {

            brush = brushFactory.getBrush(BrushFactory.ELLIPSE_BBRUSH);

            canvas.repaint();

        }

    }

    private static final class PolygonAction extends AbstractAction {

        private PolygonAction() {super("多边形");}

        public void actionPerformed(ActionEvent e) {

            brush = brushFactory.getBrush(BrushFactory.POLYGON_BRUSH);

            canvas.repaint();

        }

    }

    private static final class LineAction extends AbstractAction {

        private LineAction() {super("直线");}

        public void actionPerformed(ActionEvent e) {

            brush = brushFactory.getBrush(BrushFactory.LINE_BRUSH);

            canvas.repaint();

        }

    }

    public static void main(String[] args) {new Draw();}

}

本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式