【发布时间】:2011-09-27 02:03:37
【问题描述】:
我已经花了好几天的时间来寻找一种通过鼠标捕获在 java 中将像素绘制到窗口的方法。我正在寻找一些我可以插入的框架。它似乎很简单......任何帮助将不胜感激。
(编辑) 这是一些无效的代码。
public class Base extends JPanel implements MouseMotionListener {
public static void main(String[] args) {
new Base();
}
final static int width = 800;
final static int height = 600;
BufferedImage img;
Base() {
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE);
JFrame frame = new JFrame();
frame.addMouseMotionListener(this);
frame.add(this);
frame.setSize(width, height);
frame.setEnabled(true);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
Graphics g = img.getGraphics();
g.drawRect(1, 1, width - 2, height - 2);
g.dispose();
repaint();
}
@Override
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}
【问题讨论】:
-
您可能希望提供更多信息。例如,您使用什么图形和 GUI 库?摇摆? SWT?其他?你有没有尝试过?
-
自己做也没有那么难——试一试,看看会发生什么。
-
我的部分问题是我不知道从哪里开始。
-
@Sean Pedersen: "..我不知道从哪里开始" 1) 你的first cup of Java。 2) 然后可能是Creating a GUI With JFC/Swing 3) 然后是Performing Custom Painting 4) 可能是How To s The Smart Way。
-
另见
LinePanel。