当前位置: 代码迷 >> Java相关 >> 求助: 如何绘制矩形
  详细解决方案

求助: 如何绘制矩形

热度:464   发布时间:2007-12-21 12:29:50.0
求助: 如何绘制矩形
编一个像WINDOWS绘图板画矩形的画法相同(无论鼠标往哪个方向绘制,每次鼠标释放的点都是矩形的一个顶点)
大大们帮个忙哈!!!小弟初学,望前辈指教啊!~!! 谢谢啦~
搜索更多相关的解决方案: 矩形  绘制  

----------------解决方案--------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DrawRect extends Frame{
    public static void main(String [] args){
        new DrawRect().init();    
    }
    public void init(){
        setTitle("矩形的绘制");
        setSize(300,300);
        setVisible(true);
        addMouseListener(new MouseAdapter()
        {
            int orgX;
            int orgY;
            int orgH;
            int orgL;
            double ordH;
            double ordL;
            public void mousePressed(MouseEvent e)
            {
                orgX=e.getX();
                orgY=e.getY();
            }
public void mouseReleased(MouseEvent e){
        ordH=Math.abs(orgX-e.getX());
        ordL=Math.abs(orgY-e.getY());
        orgH=(int)ordH;
        orgL=(int)ordL;
        Graphics g = getGraphics();
        g.setColor(Color.red);

        //while(e.getY()<orgY&&e.getX()>orgX)
            g.drawRect(orgX,orgY,orgH,orgL);
        //while(e.getY()<orgY&&e.getX()<orgX)
            g.drawRect(e.getX(),orgY,orgH,orgL);
        //while(e.getY()>orgY&&e.getX()>orgX)
            g.drawRect(orgX,e.getY(),orgH,orgL);
        //while(e.getY()>orgY&&e.getX()<orgX)
            g.drawRect(e.getX(),e.getY(),orgH,orgL);
            }
});
        addWindowListener(new WindowAdapter()
        {
        public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
    }
}

这样画出的矩形是个田字~~
请问高手们如何选择输出四种矩形绘制效果啊?
还有怎么添加重绘效果啊~~?
先谢谢各位高手了~
----------------解决方案--------------------------------------------------------
  相关解决方案