【发布时间】:2015-04-01 15:44:01
【问题描述】:
我需要开发一个应用程序,它有 3 个按钮用于绘制一条线、一个矩形和一个圆形。应用程序的行为应该是这样的:用户单击一个按钮来绘制想要的形状,鼠标光标变为一个点,用户将鼠标向下移动到某个容器,通过在所需位置单击鼠标两次然后绘制两个点来绘制想要的形状使用这两个点绘制。根据我已经收集到的信息,我知道我应该使用MouseClickListener 来绘制点,然后使用从点类传递的参数调用形状类来绘制形状。我需要知道的是用于形状的容器,将MouseClickListener 放置在何处以便仅允许在该容器中绘图以及如何限制用户在再次按下按钮之前不再绘制任何点。
到目前为止,这是我的代码:
`public class MyPanel {
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 构建图形应用程序的优秀教程(参见 docs.oracle.com/javase/tutorial/uiswing)
-
我不需要为我完成工作,我需要指南(伪代码)。
标签: java swing graphics2d