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

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);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python