使用Java在鼠标单击时绘制形状
我正在尝试创建一个java程序,当用户单击JFrame时,它将在JFrame上绘制一个形状。我已经到了可以接受不同形状并识别点击的程度,但是我很难弄清楚如何实现形状的绘制使用Java在鼠标单击时绘制形状,java,swing,mouseevent,actionlistener,paintcomponent,Java,Swing,Mouseevent,Actionlistener,Paintcomponent,我正在尝试创建一个java程序,当用户单击JFrame时,它将在JFrame上绘制一个形状。我已经到了可以接受不同形状并识别点击的程度,但是我很难弄清楚如何实现形状的绘制 import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.Mou
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.JComponent;
public class StamperFrame extends JFrame {
private JButton circleButton, ovalButton, squareButton, rectButton;
private int buttonValue = 0;
public StamperFrame() {
setTitle("Shape Stamper");
setSize(500, 500);
//Setting up the buttons and positioning them.
JPanel buttonPanel = new JPanel();
circleButton = new JButton("Circle");
ovalButton = new JButton("Oval");
squareButton = new JButton("Square");
rectButton = new JButton("Rectangle");
buttonPanel.add(circleButton);
buttonPanel.add(ovalButton);
buttonPanel.add(squareButton);
buttonPanel.add(rectButton);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
//end button init
//Setting up button logic
circleButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
buttonValue = 1;
System.out.println(buttonValue);
}
});
ovalButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
buttonValue = 2;
System.out.println(buttonValue);
}
});
squareButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
buttonValue = 3;
System.out.println(buttonValue);
}
});
rectButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
buttonValue = 4;
System.out.println(buttonValue);
}
});
//end button click configuration
getContentPane().addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (buttonValue == 1) {
System.out.println("Circle added at: " + e.getX() + "," + e.getY());
} else if (buttonValue == 2) {
System.out.println("Oval added at: " + e.getX() + "," + e.getY());
}else if (buttonValue == 3) {
System.out.println("Square added at: " + e.getX() + "," + e.getY());
}else if (buttonValue == 4) {
System.out.println("Rectangle added at: " + e.getX() + "," + e.getY());
}
}
});
}
}
我知道它需要以某种方式进入我的鼠标事件,但我不知道如何
我的框架当前看起来如下所示:
如有任何建议,将不胜感激。有关定制喷漆的两种方法,请参阅:
将要绘制的对象添加到ArrayList
将对象直接绘制到BuffereImage上。
链接中的示例只绘制了一个矩形,因此您显然需要修改代码以支持不同的形状,但它应该会给您一些想法。您将在内容窗格中添加一个新的JPanel,可能位于中心
JPanel drawingPanel = new JPanel();
getContentPane().add(drawingPanel, BorderLayout.CENTER);
然后,在每个按钮的action listerner代码处调用代码以绘制JPanel的图形:
rectButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Graphics2D g = Graphics2D drawingPanel.getGraphics2D();
g.draw(new java.awt.Rectangle(42,42,20,40);
}
});
等等
您需要熟悉Java2D才能完成FinalExam包的工作。好的。真正完整的aswer now: TL;DR:复制并粘贴以下代码:
package FinalExam;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
public class StamperFrame extends JFrame {
private final JButton circleButton, ovalButton, squareButton, rectButton;
private final JPanel shapesPanel;
private Shape shape;
private final int w = 100;
private final int h = 200;
private Object lastButtonPressed;
public StamperFrame() {
setTitle("Shape Stamper");
setSize(500, 500);
final Container contentPane = getContentPane();
//Setting up the buttons and positioning them.
JPanel buttonPanel = new JPanel();
circleButton = new JButton("Circle");
ovalButton = new JButton("Oval");
squareButton = new JButton("Square");
rectButton = new JButton("Rectangle");
buttonPanel.add(circleButton);
buttonPanel.add(ovalButton);
buttonPanel.add(squareButton);
buttonPanel.add(rectButton);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
//end button init
shapesPanel = new JPanel(){
@Override
public void paintComponent(Graphics graphics) {
Graphics2D g = (Graphics2D) graphics;
super.paintComponent(g);
if(shape != null) g.draw(shape);
}
};
contentPane.add(shapesPanel, BorderLayout.CENTER);
final ActionListener buttonPressed = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
lastButtonPressed = event.getSource();
}
};
circleButton.addActionListener(buttonPressed);
ovalButton.addActionListener(buttonPressed);
squareButton.addActionListener(buttonPressed);
rectButton.addActionListener(buttonPressed);
contentPane.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if(lastButtonPressed == circleButton){
shape = new Ellipse2D.Double(x, y, w, w);
echo("Circle",x,y);
} else if(lastButtonPressed == ovalButton){
shape = new Ellipse2D.Double(x, y, w, h);
echo("Oval",x,y);
} else if (lastButtonPressed == squareButton){
shape = new Rectangle2D.Double(x, y, w, w);
echo("Square",x,y);
} else if (lastButtonPressed == rectButton){
echo("Rectangle",x,y);
shape = new Rectangle2D.Double(x, y, w, h);
}
shapesPanel.repaint();
}
private void echo(String shape, int x, int y){
System.out.println(shape + " added at: " + x + "," + y);
}
});
}
}
长篇大论
我们不需要一个可变的buttonValue来记住按下了哪个按钮。按钮是事件源,所以我们只需存储它,然后就可以知道按下了哪个按钮。
我们不需要每个按钮都有一个ActionListener的4个实例,因为它们都做相同的事情来存储事件源作为按下的按钮'
绘图技巧是通过使用JPanel和shapePanel变量作为绘图画布来完成的。我们扩展了JPanel以覆盖paintComponent。此方法将使用Graphics2D的形状绘制API。
在contentPane中添加的ActionListener将获得单击的x和y坐标,根据上次按下的按钮创建要绘制的形状,将其存储在variable Shape中,并要求ShapePanel重新绘制自身。
-1、不应使用getGraphics进行绘制。您所做的任何绘制都是临时的,并且在Swing第一次确定组件需要重新绘制时将丢失。您是对的。我想应该重写paintComponentGraphics g,检查怪异的buttonValue属性并绘制形状。当然,重画应该在某个地方进行。我讨厌期末考试。嘿,谢谢你的反馈。实际上,我提出了一个新问题,因为我让程序在单击“立即”时绘制,但重新绘制方法导致了一个我需要解决的问题。问题在于重新喷漆,但我不确定修复方法。我一定会采纳你的建议,减少行动听众的数量。