首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java DrawRect问题

Java DrawRect问题
EN

Stack Overflow用户
提问于 2019-12-03 01:41:30
回答 1查看 64关注 0票数 0

我正在用Java写一个FileManager。我需要添加矩形选择到我的程序,与它在窗口中的工作方式相同(为了选择一个矩形内的多个文件)。我的问题是,每当我向DrawRect面板添加布局来放置图标时,我就不能再绘制矩形了!这是我的DrawRect代码:

代码语言:javascript
复制
   import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class DrawRect extends JPanel {

    int x, y, x2, y2;

//    public static void main(String[] args) {
//        JFrame f = new JFrame("Draw Box Mouse 2");
//        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        f.setContentPane(new DrawRect());
//        f.setSize(300, 300);
//        f.setVisible(true);
//    }

   public DrawRect() {
        x = y = x2 = y2 = 0; //
        MyMouseListener listener = new MyMouseListener();
        addMouseListener(listener);
        addMouseMotionListener(listener);
    }

    public void setStartPoint(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void setEndPoint(int x, int y) {
        x2 = (x);
        y2 = (y);
    }

    public void drawPerfectRect(Graphics g, int x, int y, int x2, int y2) {
        int px = Math.min(x,x2);
        int py = Math.min(y,y2);
        int pw=Math.abs(x-x2);
        int ph=Math.abs(y-y2);
        g.drawRect(px, py, pw, ph);
        g.fillRect(px,py,pw,ph);

    }

    class MyMouseListener extends MouseAdapter {

        public void mousePressed(MouseEvent e) {
            setStartPoint(e.getX(), e.getY());
        }

        public void mouseDragged(MouseEvent e) {
            setEndPoint(e.getX(), e.getY());
            repaint();
        }

        public void mouseReleased(MouseEvent e) {
            setEndPoint(e.getX(), e.getY());
            repaint();
        }
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        int alpha = 50; // 50% transparent
        Color myColour = new Color(0, 0, 200,50);
       g.setColor(myColour);
        drawPerfectRect(g, x, y, x2, y2);

    }

}

enter image description here

为了解释更多,在上面的图片中,我有一个图标,它的右边部分是一个ScrollPane,DrawRect的一个实例被添加到滚动痛苦中,图标也被添加到我的DrawRect面板中,其中有多行网格布局,每个都有一个流布局。我能做些什么才能画出矩形?

正如你在下面看到的,如果我不向我的DrawRect面板添加任何布局,它工作得很好,但仍然缺少存在图标和布局的部分:enter image description here

最后,我还有一个问题:解决了这个问题后,我可以告诉这个选择矩形中的按钮被选中吗?非常感谢!

====更新:

代码语言:javascript
复制
   import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

abstract public class GridIcon extends JButton {
    private Color pressedBackgroundColor = Color.blue;
    private String shortenedName;
    private String path;
    private boolean setSelected = false;


    public GridIcon(String text, Icon icon, String path) {
        this.addActionListener(new ButtonListener());
        this.path = path;

        if (text.length() > 9) {
            shortenedName = text.substring(0, 9);
            shortenedName += "...";
        } else
            shortenedName = text;

        this.setIcon(icon);
        this.setText(shortenedName);
        this.setFocusable(false);


        this.setVerticalTextPosition(SwingConstants.BOTTOM);
        this.setHorizontalTextPosition(SwingConstants.CENTER);
        super.setOpaque(false);
        super.setContentAreaFilled(false);
        super.setBorderPainted(false);
        super.setBorder(null);
        this.setBackground(new Color(0, 0, 0, 0));


        //  super.setPreferredSize(new Dimension(60,60));

    }

    @Override
    protected void paintComponent(Graphics g) {
        if (getModel().isPressed() || setSelected) {
            g.setColor(pressedBackgroundColor);
        } else {
            g.setColor(getBackground());
        }
        g.fillRect(0, 0, getWidth(), getHeight());
        super.paintComponent(g);
    }

    @Override
    public void setContentAreaFilled(boolean b) {
    }


    public String getPath() {
        return path;
    }

    public Color getPressedBackgroundColor() {
        return pressedBackgroundColor;
    }

    public void setPressedBackgroundColor(Color pressedBackgroundColor) {
        this.pressedBackgroundColor = pressedBackgroundColor;
    }

    public void setSetSelected(boolean isSelected) {
        setSelected = isSelected;
    }

    public boolean isSetSelected() {
        return setSelected;
    }

    class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (setSelected)
                setSelected = false;

            else
                setSelected = true;

        }
    }


}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-05 01:33:48

看看A Closer Look at the Painting Mechanism上的Swing教程中的一节,了解绘画是如何完成的。

实际情况是,在绘制选择矩形之后,会绘制添加到面板中的组件。

解决方案是用您的自定义代码覆盖paint(...)。然后,选择矩形将绘制在子组件的顶部。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59144101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档