想编写一个能画直线,圆,矩形等简单二维图形的程序,却不知道怎么编
只是能响应鼠标和释放事件,获取起始点和终止点的坐标,但是不知道怎么才能画出来
Graphics类是抽象的,Canvas又不会用,请大家指教!急,在线等import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class MyGraphics extends JFrame{ protected JMenuBar mb;
protected JMenu shape, color, exit;
protected JMenuItem line, circle, rectangle;
protected JMenuItem red, yellow, blue;
protected JPanel p;
protected JMenuItem source;
int xStart, yStart;
int xEnd, yEnd;

public MyGraphics(){
super("画图");
setBounds(300, 300, 500,350);
Container c = getContentPane();
mb = new JMenuBar();
shape = new JMenu("Shape");
color = new JMenu("Color");
exit = new JMenu("Exit");
line = new JMenuItem("Line");
circle = new JMenuItem("Circle");
rectangle = new JMenuItem("Rectangle");
red = new JMenuItem("Red");
yellow = new JMenuItem("Yellow");
blue = new JMenuItem("Blue");
p = new JPanel();
p.setBackground(Color.gray);
c.add(p);
mb.add(shape);
mb.add(color);
mb.add(exit);
shape.add(line);
shape.add(circle);
shape.add(rectangle);
color.add(red);
color.add(yellow);
color.add(blue);

setJMenuBar(mb);
setVisible(true);
}




class MyMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
xStart = e.getX();
yStart = e.getY();
}

public void mouseReleased(MouseEvent e) {
xEnd = e.getX();
yEnd = e.getY();
//画直线
if(source == line) {

}
}
}


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


}

解决方案 »

  1.   

    看看这个,最好把JPanel和JFrame分为两个类,用MVC架构做:import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class MyGraphics extends JFrame { /**
     * 
     */
    private static final long serialVersionUID = 1L;
    protected JMenuBar mb;
    protected JMenu shape, color, exit;
    protected JMenuItem line, circle, rectangle;
    protected JMenuItem red, yellow, blue;
    protected MyPanel p;
    protected JMenuItem source;
    int xStart, yStart;
    int xEnd, yEnd; public MyGraphics() {
    super("画图");
    setBounds(300, 300, 500, 350);
    Container c = getContentPane();
    mb = new JMenuBar();
    shape = new JMenu("Shape");
    color = new JMenu("Color");
    exit = new JMenu("Exit");
    line = new JMenuItem("Line");
    circle = new JMenuItem("Circle");
    rectangle = new JMenuItem("Rectangle");
    red = new JMenuItem("Red");
    yellow = new JMenuItem("Yellow");
    blue = new JMenuItem("Blue");
    p = new MyPanel();
    p.setBackground(Color.gray);
    p.addMouseListener(new Handler());
    c.add(p);
    mb.add(shape);
    mb.add(color);
    mb.add(exit);
    shape.add(line);
    shape.add(circle);
    shape.add(rectangle);
    color.add(red);
    color.add(yellow);
    color.add(blue); setJMenuBar(mb);
    setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } class MyPanel extends JPanel { private static final long serialVersionUID = 1L; public void paint(Graphics g) {
    g.drawRect(xStart, yStart, xEnd, yEnd);
    } } class Handler extends MouseAdapter { public void mousePressed(MouseEvent e) {
    xStart = e.getX();
    yStart = e.getY();
    }
    public void mouseDragged(MouseEvent e) {
    xEnd = e.getX();
    yEnd = e.getY();
    p.repaint();
    } public void mouseReleased(MouseEvent e) {
    xEnd = e.getX();
    yEnd = e.getY();
    p.repaint();
    }
    } public static void main(String[] args) {
    new MyGraphics();
    }}
      

  2.   

    我的画图程序编好了,在家里的机子上可以用。但是在学校就不行了。学校的机子上,菜单项都被画布覆盖了,不知道什么原因。用的软件都是easyeclipse1.2.2
    请大家看看能不能运行,谢啦
    如果不行,能否看看什么原因import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;abstract class Shape
    {
       public abstract void draw(Graphics g, Color c);}
    class Line extends Shape
    {
       int x0,y0,x1,y1;
       public Line(int x0,int y0,int x1,int y1)
       {
          this.x0=x0;
          this.y0=y0;
          this.x1=x1;
          this.y1=y1;
       }
       public void draw(Graphics g, Color c)
       {
      g.setColor(c);
          g.drawLine(x0,y0,x1,y1);   }
    }
    class Circle extends Shape
    {
      int x,y,width,height;
      public Circle(int x,int y,int width,int height)
      {
         this.x=x;
         this.y=y;
         this.width=width;
         this.height=height;
      }
      public void draw(Graphics g, Color c)
      {
      g.setColor(c);
          g.drawOval(x,y,width,height);
      }
    }
    class Rectangle extends Shape
    {
      int x,y,height,width;
      public Rectangle(int x,int y,int width,int height)
      {
         this.x=x;
         this.y=y;
         this.width=width;
         this.height=height;  }
      public void draw(Graphics g, Color c)
      {
      g.setColor(c);
          g.drawRect(x,y,width,height);
      }
    }
    public class MyGraph extends JFrame implements ActionListener{ protected JMenuBar mb;
    protected JMenu shape, color, exit;
    protected JMenuItem line, circle, rectangle;
    protected JMenuItem red, yellow, blue;
    protected JPanel p;
    protected JMenuItem source;
    protected Color  curColor;
    int xStart, yStart;
    int xEnd, yEnd; Canvas canvas;
    MyCanvas can; public MyGraph(){
    super("画图");
    setBounds(300, 300, 500,350);
    Container c = getContentPane();
    mb = new JMenuBar();
    shape = new JMenu("Shape");
    color = new JMenu("Color");
    exit = new JMenu("Exit");
    line = new JMenuItem("Line");
    circle = new JMenuItem("Circle");
    rectangle = new JMenuItem("Rectangle");
    red = new JMenuItem("Red");
    yellow = new JMenuItem("Yellow");
    blue = new JMenuItem("Blue");
    p = new JPanel(); can = new MyCanvas(); p.setBackground(Color.gray); mb.add(shape);
    mb.add(color);
    mb.add(exit);
    shape.add(line);
    shape.add(circle);
    shape.add(rectangle);
    color.add(red);
    color.add(yellow);
    color.add(blue); c.add(can);
    line.addActionListener(this);
    circle.addActionListener(this);
    rectangle.addActionListener(this);
    red.addActionListener(this);
    yellow.addActionListener(this);
    blue.addActionListener(this); exit.addActionListener(this); addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    setJMenuBar(mb);
    setVisible(true);
    }
    public void actionPerformed(ActionEvent e) { if(e.getSource() == exit)
    System.exit(0);
    if(e.getSource() == line)
    can.setStatus(1);
    else
    if(e.getSource() == circle)
       can.setStatus(2);
    else
    if(e.getSource() == rectangle )
    can.setStatus(3);
    if(e.getSource() == red)
     can.setColor(Color.red);
    else
    if(e.getSource() == yellow)
    can.setColor(Color.yellow);
    else
    if(e.getSource() == blue)
    can.setColor(Color.blue); } /*class MyMouseListener extends MouseAdapter {
    public void mousePressed(MouseEvent e) {
    xStart = e.getX();
    yStart = e.getY();
    } public void mouseReleased(MouseEvent e) { xEnd = e.getX();
    yEnd = e.getY();
    //画直线
    if(source == line) {
     canvas.getGraphics().drawLine(xStart, yStart, xEnd, yEnd); }
    if(source == circle) {
    int minX, minY, width, height;
    minX = Math.min(xStart, xEnd);
    minY = Math.min(yStart, yEnd);
    width = Math.abs(xStart - xEnd);
    height = Math.abs(yStart - yEnd);
    canvas.getGraphics().drawOval(minX, minY, width, height);
    } if(source == rectangle ) {
    int minX, minY, width, height;
    minX = Math.min(xStart, xEnd);
    minY = Math.min(yStart, yEnd);
    width = Math.abs(xStart - xEnd);
    height = Math.abs(yStart - yEnd);
    canvas.getGraphics().drawRect(minX, minY, width, height);
    } canvas.setForeground(curColor);
    }
    } */
    public static void main(String[] args) {
    new MyGraph();
    }
    }
    class MyCanvas extends Canvas implements MouseMotionListener, MouseListener {
       Vector <Shape> graphics=new Vector <Shape>(); //存放多个图形对象
       Vector<Color> colors = new Vector<Color>();//存放每个图形的颜色
       public static final int  NO_DRAW=0,
                                DRAW_LINE=1,
                                DRAW_CIRCLE=2,
                                DRAW_RECTANGLE=3;    private int status=NO_DRAW;//作图状态
       private int x0,y0,x1,y1;    private Color curColor = Color.black;    int minX = Math.min(x0, x1);
       int minY = Math.min(y0, y1);
       int width = Math.abs(x1 - x0);
       int height = Math.abs(y1 - y0);
       public MyCanvas()
       {
          this.setBackground(Color.white);
          this.addMouseListener(this);
          this.addMouseMotionListener(this);
       }
       public void mousePressed(MouseEvent e)
       {
          x0=e.getX();
          y0=e.getY();
       }    public void mouseReleased(MouseEvent e)
       {       switch(status)
          {
             case 1:
              graphics.add(new Line(x0,y0,x1,y1));
                    
              break;
             case 2:
              graphics.add(new Circle(minX,minY,width,height));              break;
             case 3:
              graphics.add(new Rectangle(minX,minY,width,height));
              break;
          }
          colors.add(curColor);
           repaint();
       }
       public void mouseDragged(MouseEvent e)
       {
         x1=e.getX();
         y1=e.getY();
         repaint();
       }
       public void mouseMoved(MouseEvent e)
       {
       }
       public void mouseEntered(MouseEvent e)
       {
       }
       public void mouseExited(MouseEvent e)
       {
       }
       public void mouseClicked(MouseEvent e){    }
       public void setStatus(int status)
       {
         this.status=status;
       }
       public int getStatus()
       {
         return status;
       }
       public Color getColor() {
       return curColor;
       }
       public void setColor(Color c) {
       curColor = c;
       }
       public void paint(Graphics g)
       {
      // super.update(g);
        minX = Math.min(x0, x1);
        minY = Math.min(y0, y1);
        width = Math.abs(x1 - x0);
        height = Math.abs(y1 - y0);       for(int i=0;i<graphics.size();i++)
            ((Shape)graphics.get(i)).draw(g,(Color)colors.get(i));       switch(status)
          {
             case 1:
              g.setColor(curColor);
              g.drawLine(x0,y0,x1,y1);
              break;
             case 2:
              g.setColor(curColor);
              g.drawOval(minX,minY,width,height);break;
             case 3:
              g.setColor(curColor);
              g.drawRect(minX,minY,width,height);break;
          }
       }
    }
      

  3.   

    class MyCanvas extends Canvas 换成class MyCanvas extends JPanel
      

  4.   

    改成JPanel能画,但是都是重叠的那种,一画一大片。但我原来的那个也能画。为什么其他人就能运行,但是不能画?他们的菜单项被遮住咯
      

  5.   

    给你个提示吧
    鼠标没松开的时候,所有东西画在frame上
    鼠标松开红,将东西 画到panel上
    repaint一次,基本就ok了