可以使用Line2D或Line2D.Double/Float,这样可以生成一个Line对象
再使用Graphics对直线重绘就可以了

解决方案 »

  1.   

    cuij7718:
    具体该如何完成?
    能否贴出代码?
      

  2.   

    import java.awt.*;
    import java.awt.geom.*;
    import java.util.*;
    import java.awt.event.*;
    import javax.swing.*;public class Sketch
    {  
       public static void main(String[] args)
       {  
          SketchFrame frame = new SketchFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.show();
       }
    }/**
       A frame with a panel for sketching a figure
    */
    class SketchFrame extends JFrame
    {
       public SketchFrame()
       {
          setTitle("Sketch");
          setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);      // add panel to frame      SketchPanel panel = new SketchPanel();
          Container contentPane = getContentPane();
          contentPane.add(panel);
       }   public static final int DEFAULT_WIDTH = 300;
       public static final int DEFAULT_HEIGHT = 200;  
    }/**
       A panel for sketching with the keyboard.
    */
    class SketchPanel extends JPanel

       public SketchPanel()
       {  
          last = new Point2D.Double(100, 100);
          lines = new ArrayList();
          KeyHandler listener = new KeyHandler();
          addKeyListener(listener);
          setFocusable(true);
       }   /**
          Add a new line segment to the sketch.
          @param dx the movement in x direction
          @param dy the movement in y direction
       */
       public void add(int dx, int dy)
       {  
          // compute new end point
          Point2D end = new Point2D.Double(last.getX() + dx,
             last.getY() + dy);      // add line segment
          Line2D line = new Line2D.Double(last, end);
          lines.add(line);
          repaint();      // remember new end point
          last = end;
       }   public void paintComponent(Graphics g)
       {  
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D)g;      // draw all lines
          for (int i = 0; i < lines.size(); i++)
             g2.draw((Line2D)lines.get(i));
       }   private Point2D last;
       private ArrayList lines;   private static final int SMALL_INCREMENT = 1;
       private static final int LARGE_INCREMENT = 5;   private class KeyHandler implements KeyListener
       {  
          public void keyPressed(KeyEvent event)
          {  
             int keyCode = event.getKeyCode();         // set distance
             int d;
             if (event.isShiftDown())
                d = LARGE_INCREMENT;
             else
                d = SMALL_INCREMENT;         // add line segment
             if (keyCode == KeyEvent.VK_LEFT) add(-d, 0);
             else if (keyCode == KeyEvent.VK_RIGHT) add(d, 0);
             else if (keyCode == KeyEvent.VK_UP) add(0, -d);
             else if (keyCode == KeyEvent.VK_DOWN) add(0, d);
          }      public void keyReleased(KeyEvent event) {}      public void keyTyped(KeyEvent event)
          {  
             char keyChar = event.getKeyChar();         // set distance
             int d;
             if (Character.isUpperCase(keyChar))
             {  
                d = LARGE_INCREMENT;
                keyChar = Character.toLowerCase(keyChar);
             }
             else
                d = SMALL_INCREMENT;         // add line segment
             if (keyChar == 'h') add(-d, 0);
             else if (keyChar == 'l') add(d, 0);
             else if (keyChar == 'k') add(0, -d);
             else if (keyChar == 'j') add(0, d);
          }
       }
    }
      

  3.   

    可以直接在JFrame上画图,但是不好,可以在JFrame上加入一个JPanel,然后在JPanel上画图;
    把你要画的图形的坐标值存放在数组中,然后调用paintComponent(Graphics g)方法,
    在paintComponent()方法中,调用drawLine().
    一段一段直到画完整个曲线。
      

  4.   

    caiyi0903:
    你的代码画出的都是直角的折线,
    能不能画出弧线出来?
      

  5.   

    JFrame本身具有核Applet类相同的方法Paint
    public void paint(Graphics g)
    {
    }
    通过就可绘出
    java.awt.geom.*
    中的line2D、GeneralPath也可。
      

  6.   

    如果是弧线得用GeneralPath类完成
    用法基本上一样,只是多一个弧度问题(可以用默认的参数而不去考虑弧度)
      

  7.   

    利用类
    QuadCurve2D (二次曲线)

    CubicCurve2D (三次曲线)
    可以绘制曲线,这两个类在包 java.awt.geom中
    下面是我做的例子:import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;public class CurveDemo extends JFrame
     {  
        //曲线参数
        Point2D.Double startQ=new Point2D.Double(50,75);
        Point2D.Double endQ=new Point2D.Double(150,75);
        Point2D.Double control=new Point2D.Double(80,25);    Point2D.Double startC=new Point2D.Double(50,150);
        Point2D.Double endC=new Point2D.Double(150,150);
        Point2D.Double controlStart=new Point2D.Double(80,100);
        Point2D.Double controlEnd=new Point2D.Double(160,100);

        QuadCurve2D.Double quadCurve;  //二次曲线
        CubicCurve2D.Double cubicCurve;//三次曲线
        public CurveDemo()
        {
          super("Curve Demo");
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          
          quadCurve=new QuadCurve2D.Double(
       startQ.x,startQ.y,control.x,control.y,endQ.x,endQ.y);
          cubicCurve=new CubicCurve2D.Double(
       startC.x,startC.y,controlStart.x,controlStart.y,
       controlEnd.x,controlEnd.y,endC.x,endC.y);
      
          setSize(300,300);
          show();
         }
     
         public void paint(Graphics g)
         {
          Graphics2D g2D=(Graphics2D)g;
          g2D.setPaint(Color.BLUE);
          g2D.draw(quadCurve);
          g2D.draw(cubicCurve);
         }

         public static void main(String[] args)
         {
           CurveDemo curve=new CurveDemo();
          }
    }
      

  8.   

    import java.awt.*;
    import java.awt.geom.*;public class test
    {
      public test(Point start, Point next)
      {
        curve=new GeneralPath();
        curve.moveTo(start.x, start.y);
        curve.lineTo(next.x, next.y);
      }  // 添加另一端
      public void modify(Point start, Point next) {
        curve.lineTo(next.x,
                     next.y);
      }  public java.awt.Rectangle getBounds() {
        return curve.getBounds();
      }  public void draw(Graphics2D g2D) {
        g2D.draw(curve); // 重绘弧线
      }  private GeneralPath curve;
    }试一试
      

  9.   

    如果要显示刚才例子中曲线的控制点(控制弯曲度和方向),程序稍微复杂些,如下://绘制曲线import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;public class CurveDemo2 extends JFrame
     {  
             //曲线参数
             Point2D.Double startQ=new Point2D.Double(50,75);
    Point2D.Double endQ=new Point2D.Double(150,75);
    Point2D.Double control=new Point2D.Double(80,25);

    Point2D.Double startC=new Point2D.Double(50,150);
    Point2D.Double endC=new Point2D.Double(150,150);
    Point2D.Double controlStart=new Point2D.Double(80,100);
    Point2D.Double controlEnd=new Point2D.Double(160,100);

    QuadCurve2D.Double quadCurve;  //二次曲线
    CubicCurve2D.Double cubicCurve;//三次曲线     public CurveDemo2()
         {
          super("Curve Demo");
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          
          quadCurve=new QuadCurve2D.Double(
       startQ.x,startQ.y,control.x,control.y,endQ.x,endQ.y);
          cubicCurve=new CubicCurve2D.Double(
       startC.x,startC.y,controlStart.x,controlStart.y,
       controlEnd.x,controlEnd.y,endC.x,endC.y);
      
          setSize(300,300);
          show();
         }
     
     //添加内部类来显示曲线的控制点和切线
     class Marker
     {
      Ellipse2D.Double circle;
      Point2D.Double center;
      static final double radius=3;
      
      public Marker(Point2D.Double control)
      {
       center=control;
       circle=new Ellipse2D.Double(control.x-radius,control.y-radius,
           2.0*radius,2.0*radius);
      }
      
      public void draw(Graphics2D g2D)
      {
       g2D.draw(circle);
      }
      
      Point2D.Double getCenter()
      {
       return center;
      }
     }
     
     //设定控制点
     Marker ctrlQuad=new Marker(control);
     Marker ctrlCubic1=new Marker(controlStart);
     Marker ctrlCubic2=new Marker(controlEnd);
     
     public void paint(Graphics g)
     {
      Graphics2D g2D=(Graphics2D)g;
      g2D.setPaint(Color.BLUE);
      g2D.draw(quadCurve);
      g2D.draw(cubicCurve);
     
      //下面显示控制点和切线
      g2D.setPaint(Color.RED);
      ctrlQuad.draw(g2D);
      ctrlCubic1.draw(g2D);
      ctrlCubic2.draw(g2D);
      
      Line2D.Double tangent=new Line2D.Double(startQ,ctrlQuad.getCenter());
      g2D.draw(tangent);
      tangent=new Line2D.Double(endQ,ctrlQuad.getCenter());
      g2D.draw(tangent); 
      tangent=new Line2D.Double(startC,ctrlCubic1.getCenter());
      g2D.draw(tangent); 
      tangent=new Line2D.Double(endC,ctrlCubic2.getCenter());
      g2D.draw(tangent);  
     }

         public static void main(String[] args)
         {
      CurveDemo2 curve=new CurveDemo2();
     }
    }
      

  10.   

    我已经搞定了。
    本意是做出圆角的button来。
    现在已经搞定了。
    现把这个button的源码贴出来以饷各位帮助过我的兄弟们。
      

  11.   

    NJButton.java代码package UIControl.NJButton;import javax.swing.JButton;
    import javax.swing.BorderFactory;
    import java.awt.event.*;
    import java.awt.*;
    import java.awt.geom.*;
    import UIControl.UIProperties.UIProperties;
    import javax.swing.plaf.basic.BasicButtonUI;public class NJButton extends JButton
    {
        private static final int m_ArcLen = 8;
        public NJButton()
        {
            try
            {
                jbInit();
                setContentAreaFilled(false);
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }    private void jbInit() throws Exception
        {
            this.setBorder(BorderFactory.createEtchedBorder());//设置Button的Border风格
            this.addMouseListener(new NJButton_this_mouseAdapter(this));//鼠标移入事件
            this.setBackground(UIProperties.cInnerButton); //设置Button的背景颜色
            this.setForeground(UIProperties.cForeButton); //设置Button的前景颜色
            this.setFont(UIProperties.fMiddle);//设置Button的字体大小
        }    protected void paintComponent(Graphics g)
        {
            boolean blRaise = true;
    //        if (getModel().isArmed())
    //        {
    //            blRaise = false;
    //        }
            g.setColor(this.getBackground());
            g.fill3DRect(0, 0, getSize().width - 1,
                       getSize().height - 1,blRaise);        //这个调用会画一个标签和焦点矩形。
            super.paintComponent(g);
        }
        //画按钮的边界
        protected void paintBorder(Graphics g)
        {
            g.setColor(new Color(128,128,255));
            drawRect(0, 0, getSize().width - 1,
                     getSize().height - 1, g);
        }    public void drawRect(int x, int y, int width, int height, Graphics g)
        {
            if ( (width < 0) || (height < 0))
            {
                return;
            }        if (height == 0 || width == 0)
            {
                g.drawLine(x, y, x + width, y + height);
            }
            else
            {
                g.drawArc(x ,y ,m_ArcLen,m_ArcLen,90,90);
                g.drawLine(x + (m_ArcLen/2), y, x + width - (m_ArcLen/2), y);
                g.drawArc(x + width - m_ArcLen,y ,m_ArcLen,m_ArcLen,0,90);
                g.drawLine(x + width, y + (m_ArcLen/2), x + width, y + height - (m_ArcLen/2));
                g.drawArc(x + width - m_ArcLen,y + height - m_ArcLen ,m_ArcLen,m_ArcLen,270,90);
                g.drawLine(x + width - (m_ArcLen/2), y + height, x + (m_ArcLen/2), y + height );
                g.drawArc(x ,y + height - m_ArcLen ,m_ArcLen,m_ArcLen,180,90);
                g.drawLine(x, y + height - (m_ArcLen/2), x, y + (m_ArcLen/2));
            }
        }    // 侦测点击事件
        Shape shape;
        public boolean contains(int x, int y)
        {// 如果按钮改变大小,产生一个新的形状对象。
            if (shape == null ||
                !shape.getBounds().equals(getBounds()))
            {
                shape = new Ellipse2D.Float(0, 0,
                                            getWidth(), getHeight());
            }
            return shape.contains(x, y);
        }
        /**
         * 鼠标移入事件方法
         * @param e    鼠标事件
         */
        void this_mouseEntered(MouseEvent e)
        {
            this.setCursor(new Cursor(Cursor.HAND_CURSOR)); //设置鼠标形状为手型
            this.setBackground(new Color(248, 249, 252));
        }    /**
         * 鼠标移出事件方法
         * @param e    鼠标事件
         */
        void this_mouseExited(MouseEvent e)
        {
            this.setBackground(UIProperties.cInnerButton);
        }
    }class NJButton_this_mouseAdapter extends java.awt.event.MouseAdapter
    {
        NJButton adaptee;    NJButton_this_mouseAdapter(NJButton adaptee)
        {
            this.adaptee = adaptee;
        }    public void mouseEntered(MouseEvent e)
        {
            adaptee.this_mouseEntered(e);
        }
        public void mouseExited(MouseEvent e)
        {
            adaptee.this_mouseExited(e);
        }
    }UIProperties.java代码package UIControl.UIProperties;import java.awt.Color;
    import java.awt.Font;public class UIProperties
    {
        /**
         * 颜色属性
         */
        public static final Color cBackground = new Color(224,224,224);//一般背景颜色
        public static final Color cInnerFrame = new Color(237,237,237);//内置框颜色
        public static final Color cInnerFrame2 = new Color(237,245,245);//内置框颜色2
        public static final Color cInnerButton = new Color(237,241,248);//内置按钮颜色
        public static final Color cOuterButton = new Color(198,196,232);//外部按钮及滚动条颜色
        public static final Color cScrollFrame = new Color(125,125,255);//滚动条边框颜色
        public static final Color cForeNormal = new Color(10,49,107);//一般前景颜色
        public static final Color cForeButton = Color.BLACK;//按钮前景颜色    /**
         * 字体属性
         */
        public static final Font fSmall = new Font("Dialog", 0, 12);//小字体
        public static final Font fMiddle = new Font("Dialog", 0, 14);//中等字体
        public static final Font fLarge = new Font("Dialog", 0, 16);//大字体    public UIProperties()
        {
        }
    }
      

  12.   

    上面兄弟们的方法我都没用,我用的是
    g.drawArc(int x, int y, int width, int height,
     int startAngle, int arcAngle);
    方法搞定的。不过还是要感谢大家,散分了。:)