我需要制作一个简单的绘图应用程序,它能够在 2个用户给定的点之间绘制一条直线,一个矩形和一个圆。
确切的应用程序行为应如下所示:
JPanel
,使光标变为a
十字准线到目前为止,这是我提出的:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.border.LineBorder;
import javax.swing.AbstractAction;
import javax.swing.Action;
public class MainFrame
{
private boolean readyToDraw = false;
private int clickCount = 0;
private JFrame frame;
private JPanel drawPanel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame window = new MainFrame();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainFrame() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 800, 600);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.WHITE);
buttonPanel.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
buttonPanel.setBounds(10, 11, 100, 85);
frame.getContentPane().add(buttonPanel);
buttonPanel.setLayout(null);
JButton btnLine = new JButton("Line");
btnLine.setBackground(Color.LIGHT_GRAY);
btnLine.setBounds(4, 4, 92, 25);
btnLine.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Execute when button is pressed
if (readyToDraw == true) {
System.out.println("Let's draw!");
}
else {
}
System.out.println("Line");
}
});
JButton btnRectangle = new JButton("Rectangle");
btnRectangle.setBackground(Color.LIGHT_GRAY);
btnRectangle.setBounds(4, 30, 92, 25);
btnRectangle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Execute when button is pressed
if (readyToDraw == true) {
System.out.println("Let's draw!");
}
else {
}
System.out.println("Rectangle");
}
});
JButton btnCircle = new JButton("Circle");
btnCircle.setBackground(Color.LIGHT_GRAY);
btnCircle.setBounds(4, 56, 92, 25);
btnCircle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Execute when button is pressed
if (readyToDraw == true) {
System.out.println("Let's draw!");
}
else {
}
System.out.println("Circle");
}
});
buttonPanel.add(btnLine);
buttonPanel.add(btnRectangle);
buttonPanel.add(btnCircle);
}
@SuppressWarnings("serial")
private class Paint extends JPanel implements MouseListener{
drawPanel = new JPanel();
drawPanel.setBackground(Color.WHITE);
drawPanel.setBounds(120, 11, 664, 550);
frame.getContentPane().add(drawPanel);
this.addMouseListener(new MouseListener());
@Override
public void mouseEntered(MouseEvent arg0) {
Cursor dotCursor = new Cursor(Cursor.CROSSHAIR_CURSOR);
drawPanel.setCursor(dotCursor);
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseClicked(MouseEvent e) {
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
if (clickCount == 0) {
x1 = e.getX();
y1 = e.getY();
clickCount++;
} else if (clickCount == 1) {
x2 = e.getX();
y2 = e.getY();
clickCount++;
readyToDraw = true;
} else {
clickCount = 0;
readyToDraw = false;
}
System.out.println(x1 + " " + y1 + " " + clickCount + " " + x2 + " "
+ y2 + readyToDraw);
}
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
}
这包含一些我无法解决的错误。我需要的是如何使我的应用程序能够至少画一条线的解释。
答案 0 :(得分:0)
在Paint
类中,您似乎尝试在方法或构造函数的上下文之外执行功能......
private class Paint extends JPanel implements MouseListener {
// Undefined variable...
drawPanel = new JPanel();
// Executing functionality outside of a method or constructor
drawPanel.setBackground (Color.WHITE);
drawPanel.setBounds (
120, 11, 664, 550);
frame.getContentPane ()
.add(drawPanel);
this.addMouseListener(
new MouseListener());
所以,你可以做点像......
private class Paint extends JPanel implements MouseListener {
private JPanel drawPanel = new JPanel();
public Paint() {
drawPanel.setBackground(Color.WHITE);
drawPanel.setBounds(
120, 11, 664, 550);
frame.getContentPane()
.add(drawPanel);
this.addMouseListener(
new MouseListener());
}
但是这会在this.addMouseListener
调用时出现错误,因为MouseListener
是一个接口,需要在使用之前实现,但猜猜是什么,你可以做this.addMouseListener(this);
1}}
这会解决编译器错误,但是存在一堆逻辑错误......
您似乎在JPanel
班级(JPanel
中drawPanel
)内创建JPanel
的实例,然后添加此子级面板(drawPanel
)直接到frame
!这似乎是完全浪费时间,一般来说,简直就是糟糕的设计......
但是,然后您将MouseListener
添加到Paint
面板,然后尝试使用它来更新drawPanel
.... ????
简单来说,您不需要drawPanel
,只需摆脱它并直接使用Paint
面板(但不要在构造函数中将其添加到框架中) Paint
,Paint
不应该关心)
避免使用null
布局,像素完美布局是现代ui设计中的一种幻觉。影响组件个体大小的因素太多,您无法控制。 Swing旨在与布局管理器一起工作,放弃这些将导致问题和问题的终结,您将花费越来越多的时间来纠正