请问如何用Graphics类画三角形?

解决方案 »

  1.   

    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.RenderingHints;
    import java.awt.geom.GeneralPath;import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;@SuppressWarnings("serial")
    public class TrianglePainter extends JPanel {
        private Triangle triangle;    public TrianglePainter() {
            triangle = new Triangle(new Point(20, 20), new Point(100, 100), new Point(10, 100));
        }    @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            triangle.draw(g2d);
            
            g2d.translate(150, 150);
            triangle.fill(g2d);
        }    private static void createGuiAndShow() {
            JFrame frame = new JFrame("三角形");
            frame.getContentPane().add(new TrianglePainter());        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(400, 400);
            frame.setAlwaysOnTop(true);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }    public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    createGuiAndShow();
                }
            });
        }
    }// 定义一个三角形类
    class Triangle {
        private Point p1;
        private Point p2;
        private Point p3;    private GeneralPath path;    // 使用三个点构建一个三角形
        public Triangle(Point p1, Point p2, Point p3) {
            this.p1 = p1;
            this.p2 = p2;
            this.p3 = p3;
            this.path = buildPath();
        }    // 绘制三角形边
        public void draw(Graphics2D g2d) {
            g2d.draw(path);
        }    // 填充三角形
        public void fill(Graphics2D g2d) {
            g2d.fill(path);
        }    // 创建三角形外形的路径
        private GeneralPath buildPath() {
            path = new GeneralPath();
            path.moveTo(p1.x, p1.y);
            path.lineTo(p2.x, p2.y);
            path.lineTo(p3.x, p3.y);
            path.closePath();        return path;
        }
    }
      

  2.   

    class PainterPanel extends JPanel implements MouseListener{
        int shape=-1; //图案类型
        Point[] point=new Point[2]; //记录鼠标拖动的起始点和终点
        //构造函数**********************************************
        public PainterPanel(){
               super(); //调用父类构造函数
               this.setBackground(Color.white); //设置背景颜色(JPanel成员) 其他成员对绘图没有实质性的帮助
            point[0]=new Point(-1,-1); //初始化变量
            point[1]=new Point(-1,-1);
               addMouseListener(this); //增加鼠标事件
        }
       //构造函数************************************************
        public void mouseReleased(MouseEvent e){ //鼠标释放事件
               point[1]=new Point(e.getX(),e.getY()); //设置终点位置
               repaint(); //重绘屏幕
        }    public void mouseEntered(MouseEvent e){}
        public void mouseExited(MouseEvent e){}
        public void mouseClicked(MouseEvent e){}    public void mousePressed(MouseEvent e){ //鼠标按下时事件
               point[0]=new Point(e.getX(),e.getY()); //设置起始点位置
        }    public void paint(Graphics g){
               super.paint(g);
               switch (shape){ //根据shape值绘制图形
                case 0:
                 g.drawLine(point[0].x,point[0].y,point[1].x,point[1].y); //绘线
                 break;            case 1:
                 int width=point[1].x-point[0].x;
                 int height=point[1].y-point[0].y;
                 g.drawOval(point[0].x,point[0].y,width,height); //绘椭圆
                 break;            case 2:
                 width=point[1].x-point[0].x;
                 height=point[1].y-point[0].y;
                 g.drawRect(point[0].x,point[0].y,width,height); //绘矩形
                 break;
               }
        }
        public void drawShape(int shape)
        {
               this.shape=shape;
        }
    }
    目前写的代码是这样 我不知道如何得到3个不同的点的坐标画3跳线
      

  3.   

    想出来了 加个鼠标单击事件 
    import java.awt.*;
    import java.awt.event.*;//包含MouseListener
    import javax.swing.*;
    import javax.swing.event.*;
    class PainterPanel extends JPanel implements MouseListener{
        int shape=-1; //图案类型
        Point[] point=new Point[3]; //记录鼠标拖动的起始点和终点
        //构造函数**********************************************
        public PainterPanel(){
               super(); //调用父类构造函数
               this.setBackground(Color.white); //设置背景颜色(JPanel成员) 其他成员对绘图没有实质性的帮助
            point[0]=new Point(-1,-1); //初始化变量
            point[1]=new Point(-1,-1);
               addMouseListener(this); //增加鼠标事件
        }
       //构造函数************************************************
        public void mouseReleased(MouseEvent e){ //鼠标释放事件
               point[1]=new Point(e.getX(),e.getY()); //设置终点位置
               repaint(); //重绘屏幕
        }    public void mouseEntered(MouseEvent e){}
        public void mouseExited(MouseEvent e){}    public void mouseClicked(MouseEvent e)
        { point[2]=new Point (e.getX(),e.getY());
        }    
        public void mousePressed(MouseEvent e)
        { //鼠标按下时事件
               point[0]=new Point(e.getX(),e.getY()); //设置起始点位置
        }    public void paint(Graphics g){
               super.paint(g);
               switch (shape){ //根据shape值绘制图形
                case 0:
                 g.drawLine(point[0].x,point[0].y,point[1].x,point[1].y); //绘线
                 break;            case 1:
                 int width=point[1].x-point[0].x;
                 int height=point[1].y-point[0].y;
                 g.drawOval(point[0].x,point[0].y,width,height); //绘椭圆
                 break;            case 2:
                 width=point[1].x-point[0].x;
                 height=point[1].y-point[0].y;
                 g.drawRect(point[0].x,point[0].y,width,height); //绘矩形
                 break;           case 3:            g.drawLine(point[0].x,point[0].y,point[1].x,point[1].y);
                g.drawLine(point[0].x,point[0].y,point[2].x,point[2].y);
                g.drawLine(point[2].x,point[2].y,point[1].x,point[1].y);
                break;
               }
        }
        public void drawShape(int shape)
        {
               this.shape=shape;
        }
    }
    public class PainterDemo extends JFrame{
        JToggleButton[] button=new JToggleButton[4]; //按钮组
        PainterPanel painter=new PainterPanel(); //绘图面板  上面构造了
        public PainterDemo(){
               super("Java随手画"); //调用父类构造函数
               String[] buttonName={"直线","椭圆","矩形"," 三角形"}; //按钮文字
               DrawShapeListener buttonListener=new DrawShapeListener(); //按钮事件
               JToolBar toolBar=new JToolBar(); //实例化工具栏
               ButtonGroup buttonGroup=new ButtonGroup(); //实例化按钮组           for (int i=0;i<button.length;i++){
                       button[i]=new JToggleButton(buttonName[i]); //实例化按钮
                       button[i].addActionListener(buttonListener); //增加按钮事件处理
                       buttonGroup.add(button[i]); //增加按钮到按钮组
                       toolBar.add(button[i]); //增加按钮到工具栏
               }           Container container=getContentPane(); //得到窗口容器
               container.add(toolBar,BorderLayout.NORTH); //增加组件到容器上
               container.add(painter,BorderLayout.CENTER);
               setSize(300,200); //设置窗口尺寸
               setVisible(true); //设置窗口为可视
               setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
        }
    //内部类
        class DrawShapeListener implements ActionListener{ //按钮事件处理
               public void actionPerformed(ActionEvent e){
                for (int i=0;i<button.length;i++){
                     if (e.getSource()==button[i]){ //判断来自于哪个按钮
                          painter.drawShape(i); //绘制图形
                     }
                }
               }
        }    public static void main(String[] args){
               new PainterDemo();
        }
    }