日期:2014-05-20  浏览次数:20586 次

java画图的问题
共有四个文件
文件一:MoveBaseComponents.java
Java code

import java.awt.*;
import java.awt.event.*;

public class MoveBaseComponents extends Canvas {
    static private int x; // 中间变量之横坐标
    static private int y; // 中间变量之纵坐标
    static private int mouseX; // 鼠标按键横坐标
    static private int mouseY; // 鼠标按键纵坐标
    static private Point point; // 组件位置
    static private Color background = Color.red; // 组件背景颜色

    MoveBaseComponents() {
        addMouseListener(new Move());
        addMouseMotionListener(new Move());
        point = new Point(100, 100);
        // position();
        setBackground(background);
        setBounds(50, 50, 50, 50);
    }

    // 组件定位
    public void position() {
        setLocation(point);
        // repaint();
    }

    class Move extends MouseAdapter {
        public void mousePressed(MouseEvent e) {
            mouseX = e.getX();
            mouseY = e.getY();
        }

        public void mouseReleased(MouseEvent e) {
            mouseX = 0;
            mouseY = 0;
        }

        public void mouseDragged(MouseEvent e) {
            Component com = (Component) e.getSource();
            x = com.getX() + e.getX() - mouseX;
            y = com.getY() + e.getY() - mouseY;

            // 需要做边界限定

            point.setLocation(x, y);
            position();

            // 重合区域变色
            Rectangle rectSource = com.getBounds();
            Rectangle rectDesc = null;
            for (Object obj : Basic.vector) {
                if (e.getSource() == obj) {
                    continue; // 消除点击后本控件着色
                }

                rectDesc = ((Component) obj).getBounds();
                if (rectSource.intersects(rectDesc)) {
                    Rectangle good = rectSource.intersection(rectDesc);//重合区域
                    // 新建重合部分
                    Graphics pen = com.getGraphics();
                    pen.setColor(Color.green);
                    pen.fillRect(good.x - rectSource.x, good.y - rectSource.y, good.width, good.height);
//                    System.out.println((good.x - rectSource.x) + "," + (good.y - rectSource.y) + "," + good.width + "," + good.height);
//                    System.out.println(com);
//                    System.out.println(pen);
                }
            }
        }
    }

    public void mouseMoved(MouseEvent e) {
        // System.out.println("move = " + e.getSource());
    }
}




文件二:MyApplet.java
Java code

import java.applet.Applet;
import java.awt.Component;

public class MyApplet extends Applet {

    @Override
    public Component add(Component comp) {
        Basic.add(comp);
        super.add(comp);
        return comp;
    }

    @Override
    public void remove(Component comp) {
        Basic.remove(comp);
        super.remove(comp);
    }

}




文件三:Basic.java
Java code

import java.util.Vector;

public class Basic {
    public static Vector vector = new Vector();

    public static void add(Object obj) {
        vector.add(obj);
    }

    public static void remove(Object obj) {
        vector.remove(obj);
    }
}




文件四:main.java
Java code

public class main extends MyApplet {
    MoveBaseComponents[] aa = {new MoveBaseComponents(), new MoveBaseComponents(),new MoveBaseComponents(),new MoveBaseComponents()};

    public void init() {
        for(int i=0 ; i<aa.length ; i++){
            add(aa[i]);
        }
    }
}




问题描述:
运行时,会有四个矩形,背景色为红色;
四个矩形可以拖拽;