gpt4 book ai didi

Java:按住鼠标时基于二维数组填充图形

转载 作者:行者123 更新时间:2023-12-02 06:50:47 24 4
gpt4 key购买 nike

所以我有一个由 2D 数组的内容填充的 JPanel。我有一个鼠标监听器,按下时会改变单元格的颜色。我的问题是,是否可以让用户将鼠标拖动到一行单元格上并连续为它们全部着色?我已经研究过鼠标运动监听器,但这似乎没有帮助。

有什么想法吗?

最佳答案

您可以将 MouseMotionListenermouseDragged() 方法与 MouseListenermousePressed() 方法结合使用.

mousePressed() 方法将处理无需移动的简单点击,而 mouseDragged() 将处理完成的任何拖动。我结合了为回答您原来的问题而编写的代码 here为了更好地阐明一切的作用,非常感谢您对其他问题的答复。

package stackoverflow.answers;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;

import javax.swing.*;

public class JPanelPaint {
JPanel panel;
JFrame frame;
BufferedImage image;

public JPanelPaint() {
image = new BufferedImage(50, 50, BufferedImage.TYPE_INT_ARGB);
for (int i = 0; i < image.getWidth(); i++) {
for (int j=0; j < image.getHeight(); j++) {
/* I'm just initializing the image with an arbitrary color (white in this case), you can easily change this. */
image.setRGB(i, j, new Color((int)(255 ), (int)(255 ), (int)(255 )).getRGB());
}
}

frame = new JFrame("JPanel Paint");
panel = new JPanel() {
@Override
public void paint(Graphics g) {
super.paint(g);
Rectangle rect = g.getClipBounds();
g.setColor(Color.white);
g.fillRect(rect.x, rect.y, rect.width, rect.height);
for (int i = 0; i < image.getWidth(); i++) {
for (int j=0; j < image.getHeight(); j++) {
/* Set the color of the "quadpixel" to that of the original cell on the image. */
g.setColor(new Color(image.getRGB(i, j)));
g.fillRect(j*4, i*4, 4, 4);
}
}


}
};

panel.addMouseListener(new MouseListener() {

@Override
public void mouseClicked(MouseEvent arg0) { }

@Override
public void mouseEntered(MouseEvent arg0) { }

@Override
public void mouseExited(MouseEvent arg0) { }

@Override
public void mousePressed(MouseEvent arg0) {
/* Y and X are swapped, just a quirk in the JRE */
/* I'm just setting the pixel with an arbitrary color (black in this case), you can easily change this. */
image.setRGB(arg0.getY() / 4, arg0.getX() / 4, new Color(0, 0, 0).getRGB());
panel.repaint();
}

@Override
public void mouseReleased(MouseEvent arg0) { }

});

panel.addMouseMotionListener(new MouseMotionListener() {

@Override
public void mouseDragged(MouseEvent arg0) {
/* Y and X are swapped, just a quirk in the JRE */
/* I'm just setting the pixel with an arbitrary color (black in this case), you can easily change this. */
image.setRGB(arg0.getY() / 4, arg0.getX() / 4, new Color(0, 0, 0).getRGB());
panel.repaint();
}

@Override
public void mouseMoved(MouseEvent arg0) { }

});

panel.setPreferredSize(new Dimension(200, 200));

frame.add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

panel.repaint();
}

public static void main(String[] args) {
new JPanelPaint();
}
}

关于Java:按住鼠标时基于二维数组填充图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18046940/

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