我目前正在研究游戏的级别编辑器。当前,要创建一个实体,您必须单击创建jbutton,然后出现表单,在该表单中输入其坐标和大小。
我想实现鼠标的功能。用户单击创建jbutton,然后必须按在jpanel上的某个地方的鼠标预览,然后拖动以设置对象的大小,最后释放按钮。然后在按下按钮的位置创建对象。
我在预告架上添加了一个Mouselistener(要获得正确的坐标)。
我的问题是:当我单击按钮时该怎么办?
在操作范围内的方法内?
基本上,该过程将是:
1)等待按下按钮
2)获得坐标
3)等待释放按钮
4)获取新的坐标以使对象的大小
5)创建对象
我应该如何正确地进行操作?
预先感谢
我的问题是:单击按钮时该怎么办? 在Action Performed方法中?
激活Mouselistener。这可以是将Mouselistener和MouseMotionListener(MouseAdapater可以两者都可以做)添加到按钮上的绘图jpanel,或者通过更改已经添加的MouseAdapater(我的偏爱)的状态,这又是已经添加到Jpanel中的。这可能很简单,就像将布尔变量切换到True,然后在执行任何操作之前先检查一下慕斯,慕斯,慕斯,慕斯的方法。
。请注意,如果您走第一条路线 - 通过按按钮在按钮上添加MouseListener/MouseMotionListener,如果您不小心要在浏览时删除它们,则可能会添加多个侦听器。这就是为什么我更喜欢第二种方法。
,例如,
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class ActivateMouse extends JPanel {
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private static final Color DRAW_RECT_COLOR = Color.pink.brighter();
private static final Color DRAW_ALL_RECTS_COLOR = Color.red;
private boolean mouseActive = false;
private Shape drawRect = null;
private List<Shape> shapeList = new ArrayList<>();
private ButtonAction buttonAction = new ButtonAction("Create New Rectangle", KeyEvent.VK_C);
public ActivateMouse() {
MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
addMouseListener(myMouseAdapter);
addMouseMotionListener(myMouseAdapter);
add(new JButton(buttonAction));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if (drawRect != null) {
g2.setColor(DRAW_RECT_COLOR);
g2.draw(drawRect);
}
g2.setColor(DRAW_ALL_RECTS_COLOR);
for (Shape shape : shapeList) {
g2.draw(shape);
}
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private class MyMouseAdapter extends MouseAdapter {
private Point firstPt;
@Override
public void mousePressed(MouseEvent e) {
if (mouseActive && e.getButton() == MouseEvent.BUTTON1) {
firstPt = e.getPoint();
}
}
@Override
public void mouseDragged(MouseEvent e) {
if (!mouseActive || firstPt == null) {
return;
}
drawRect = createRect(e.getPoint());
repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
if (!mouseActive || firstPt == null) {
return;
}
shapeList.add(createRect(e.getPoint()));
repaint();
mouseActive = false;
}
private Shape createRect(Point p) {
int x = Math.min(firstPt.x, p.x);
int y = Math.min(firstPt.y, p.y);
int width = Math.abs(firstPt.x - p.x);
int height = Math.abs(firstPt.y - p.y);
return new Rectangle(x, y, width, height);
}
}
private class ButtonAction extends AbstractAction {
public ButtonAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
mouseActive = true;
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("ActivateMouse");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ActivateMouse());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}