Bing-Bong
2016-07-26 05:52
采纳率: 100%
浏览 3.7k
已采纳

java中 在一张图片上画一个矩形框

我想在一张图片上画一个矩形框 矩形框写好了 继承了Jpanel 但是只要add到frame上 就会覆盖原来的内容
有什么办法不覆盖吗? 两者都保留
下面代码main函数里 先add一个videoArea 然后再add矩形框的时候 就会覆盖原来的 有什么办法不覆盖吗 求教


```import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.*;

public class DrawSketch extends JPanel implements MouseMotionListener {

    private static final int recW = 75;
    private static final int MAX = 1;
    private Rectangle[] rect = new Rectangle[MAX];
    private int numOfRecs = 0;
    private int currentSquareIndex = -1;

    public DrawSketch() {

        addMouseListener(new MouseAdapter() {

            @Override

            public void mousePressed(MouseEvent evt) {

                int x = evt.getX();

                int y = evt.getY();

                currentSquareIndex = getRec(x, y);

                if (currentSquareIndex < 0) // not inside a square

                {

                    add(x, y);

                }

            }

//            @Override
//
//            public void mouseClicked(MouseEvent evt) {
//
//                int x = evt.getX();
//
//                int y = evt.getY();
//
//                if (evt.getClickCount() >= 2) {
//
//                    remove(currentSquareIndex);
//
//                }
//
//            }
//
        });

        addMouseMotionListener(this);
    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        g.setColor(Color.RED);

        for (int i = 0; i < numOfRecs; i++) {

            ((Graphics2D) g).draw(rect[i]);

        }
    }

    public int getRec(int x, int y) {

        for (int i = 0; i < numOfRecs; i++) {

            if (rect[i].contains(x, y)) {

                return i;

            }

        }

        return -1;
    }

    public void add(int x, int y) {

        if (numOfRecs < MAX) {

            rect[numOfRecs] = new Rectangle(x, y, recW, recW);

            currentSquareIndex = numOfRecs;

            numOfRecs++;

            repaint();

        }
    }

    @Override
    public void remove(int n) {

        if (n < 0 || n >= numOfRecs) {

            return;

        }

        numOfRecs--;

        rect[n] = rect[numOfRecs];

        if (currentSquareIndex == n) {

            currentSquareIndex = -1;

        }

        repaint();
    }

    @Override
    public void mouseMoved(MouseEvent event) {

        int x = event.getX();

        int y = event.getY();

        if (getRec(x, y) >= 0) {

            setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));

        } else {

            setCursor(Cursor.getDefaultCursor());

        }
    }

    @Override
    public void mouseDragged(MouseEvent event) {

        int x = event.getX();

        int y = event.getY();
        System.out.println(x+"---"+y);

        if (currentSquareIndex >= 0) {

            Graphics graphics = getGraphics();

            graphics.setXORMode(getBackground());
            graphics.setColor(Color.RED);

            ((Graphics2D) graphics).draw(rect[currentSquareIndex]);

            rect[currentSquareIndex].x = x;

            rect[currentSquareIndex].y = y;

            ((Graphics2D) graphics).draw(rect[currentSquareIndex]);

            graphics.dispose();

        }
    }

    public static void main(String[] args) {

        JFrame jFrame = new JFrame();

        jFrame.setTitle("");

        jFrame.setSize(1000, 500);

        jFrame.addWindowListener(new WindowAdapter() {

            @Override

            public void windowClosing(WindowEvent e) {

                System.exit(0);

            }

        });

        JLabel videoArea = new JLabel("load a video,please",JLabel.CENTER);
        videoArea.setFont(new Font("",Font.BOLD,40));
        videoArea.setBounds(new Rectangle(0, 0, 856, 480));
        videoArea.setBorder(new EmptyBorder(0, 0, 0, 0));

        Container cPane = jFrame.getContentPane();
        cPane.add(videoArea);
        cPane.add(new DrawSketch());
        jFrame.setVisible(true);
    }
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • IT痴者 2016-07-26 07:01
    已采纳

    你这个的问题是因为布局叠加了。所以下面的文字看不见了。只需要把自定义的panel背景设成透明的就行了。

    #Container cPane = jFrame.getContentPane();
    #cPane.add(videoArea);
    #DrawSketch d = new DrawSketch();
    #d.setOpaque(false);
    #cPane.add(d);
    #jFrame.setVisible(true);

    点赞 打赏 评论

相关推荐