我需要开发一个应用程序,它有3个按钮用于绘制直线,矩形和圆形。应用程序的行为应该是这样的:用户点击一个按钮来绘制想要的形状,鼠标光标变成点,用户将鼠标移动到某个容器,通过在所需位置点击鼠标两次来绘制两个点,然后想要的形状使用这两个点来绘制。从我已经收集的信息中,我知道我应该使用MouseClickListener
来绘制点,然后使用从点类传入的参数调用形状类来绘制形状。我需要知道的是用于形状的容器,放置MouseClickListener
的位置,以便仅允许在该容器中绘制,以及如何限制用户再次绘制点,直到再次按下按钮。 到目前为止,这是我的代码: `公共类MyPanel {Java绘图应用程序
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MyPanel window = new MyPanel();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MyPanel() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setResizable(false);
frame.setBounds(100, 100, 500, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.setBounds(10, 25, 474, 336);
frame.getContentPane().add(panel);
JButton btnLine = new JButton("Line");
btnLine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
drawPoint draw = new drawPoint();
}
});
btnLine.setBounds(10, 0, 110, 23);
frame.getContentPane().add(btnLine);
JButton btnRectangle = new JButton("Rectangle");
btnRectangle.setBounds(196, 0, 110, 23);
frame.getContentPane().add(btnRectangle);
JButton btnCircle = new JButton("Circle");
btnCircle.setBounds(374, 0, 110, 23);
frame.getContentPane().add(btnCircle);
}
}
public class drawPoint implements MouseListener {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
getCoordinates
drawAPoint
drawLine(coordinates)
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
请发布您自己创建的代码;并指出你卡在哪里。截至目前,您的问题非常类似于“我有一项任务;请为我做”。如果您还没有任何代码,您可能需要查看Oracle优秀的使用Swing构建图形应用程序的教程(请参阅http://docs.oracle.com/javase/tutorial/uiswing/) – GhostCat 2015-04-01 07:27:36
[绘画在AWT和Swing中](http://www.oracle.com/technetwork/java/painting-140037.html),[执行自定义绘画](http://docs.oracle.com/javase/tutorial/uiswing/painting /),[如何使用按钮,复选框和单选按钮](http://docs.oracle.com/javase/tutorial/uiswing/components/button.html),[如何编写动作监听器](http ://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html) – MadProgrammer 2015-04-01 07:28:12
我不需要为我完成我的工作,我需要指导(伪代码)。 – Justin 2015-04-01 07:41:41