gpt4 book ai didi

Java DrawRect 问题

转载 作者:行者123 更新时间:2023-12-01 19:44:17 26 4
gpt4 key购买 nike

我正在使用 Java 编写一个文件管理器。我需要将矩形选择添加到我的程序中,就像在 Windows 中一样(以便选择矩形内的多个文件)。我的问题是,每当我向 DrawRect 面板添加布局来放置图标时,我都无法再绘制矩形了!这是我的 DrawRect 代码:

   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

为了解释更多,在上图中,我有一个 SplitPane,它的右侧部分是一个 ScrollPane,DrawRect 的实例被添加到滚动痛苦中,并且图标也添加到我的 DrawRect 面板中,每个面板中有多行网格布局有一个流程布局。我该怎么做才能绘制矩形?

正如您在下面看到的,如果我不向 DrawRect 面板添加任何布局,它可以正常工作,但仍然会错过图标和布局存在的部分: enter image description here

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

================================================== ===========================更新:

   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;

}
}


}

最佳答案

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

所发生的情况是,添加到面板的组件是在绘制选择矩形之后绘制的。

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

关于Java DrawRect 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59144101/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com