- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的项目遇到了障碍。使用我从上一个问题收到的代码,我重现了我的问题。我需要添加一个清除和撤消 JButton。如您所见,我已经添加了按钮,但是我无法执行所需的操作。我尝试将 ActionListeners 添加到创建按钮的类中,然后尝试执行itemsDrawn = new ArrayList<>; (明确)itemsDrawn.remove(itemsDrawn.size()-1) (用于撤消)作为结果操作,但程序锁定。
基本上我做对了吗/我把 ActionListeners 放在哪里?
任何帮助将不胜感激
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.LinearGradientPaint;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class MyPaint {
static ArrayList<Drawable> itemsDrawn;
static JButton Clear,Undo;
public static void main(String[] args) {
new MyPaint();
}
public MyPaint() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Java 2D Drawing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
DrawablePane dp = new DrawablePane();
ControlPane cp = new ControlPane(dp);
frame.add(dp);
frame.add(cp, BorderLayout.NORTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class State {
private final Color foreground;
private final Color background;
private final boolean gradient;
private final boolean filled;
private final boolean dashed;
private int LineWidth, DashLength;
public State(Color foreground, Color background, boolean gradient, boolean filled, boolean dashed,int LineWidth, int DashLength) {
this.foreground = foreground;
this.background = background;
this.gradient = gradient;
this.filled = filled;
this.dashed = dashed;
this.LineWidth = LineWidth;
this.DashLength = DashLength;
}
public Color getBackground() {
return background;
}
public Color getForeground() {
return foreground;
}
public boolean isGradient() {
return gradient;
}
public boolean isFilled() {
return filled;
}
public boolean isDashed(){
return dashed;
}
public int getLineWidth(){
return LineWidth;
}
public int getDashLength(){
return DashLength;
}
}
public class ControlPane extends JPanel {
private JComboBox shapes;
private JLabel foreground;
private JLabel background;
private JCheckBox gradient,Filled,dashed;
private JTextField Dashlength,Linewidth;
private JLabel Len,Wid;
private DrawablePane drawablePane;
public ControlPane(DrawablePane pane) {
shapes = new JComboBox<>(new String[]{"Rectangle", "Oval","Line"});
foreground = createColorLable(Color.BLACK);
foreground.setToolTipText("Foreground");
background = createColorLable(Color.WHITE);
background.setToolTipText("Background");
gradient = new JCheckBox("Gradient");
Filled = new JCheckBox("Filled");
dashed = new JCheckBox("Dashed");
Dashlength = new JTextField("1");
Linewidth = new JTextField("1");
Len = new JLabel("Dash Length:");
Wid = new JLabel("Line Width:");
Clear = new JButton("Clear");
Undo = new JButton("Undo");
JPanel panel = new JPanel();
panel.add(Clear);
panel.add(Undo);
panel.add(foreground);
panel.add(background);
panel.add(Filled);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
add(shapes, gbc);
add(panel, gbc);
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.NORTH;
add(gradient, gbc);
add(dashed, gbc);
add(Len);
add(Linewidth);
add(Wid);
add(Dashlength);
setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(12, 12, 12, 12)));
this.drawablePane = pane;
MouseHandler mouseHandler = new MouseHandler();
drawablePane.addMouseListener(mouseHandler);
drawablePane.addMouseMotionListener(mouseHandler);
}
public int getDash(){
String Length = Dashlength.getText();
int Dash = Integer.parseInt(Length);
return Dash;
}
public int getLine(){
String LWidth = Linewidth.getText();
int Line = Integer.parseInt(LWidth);
return Line;
}
protected Drawable createDrawable() {
Drawable drawable = null;
State state = new State(foreground.getBackground(), background.getBackground(), gradient.isSelected(), Filled.isSelected(), dashed.isSelected(),getLine(), getDash());
String selected = (String) shapes.getSelectedItem();
if ("rectangle".equalsIgnoreCase(selected)) {
drawable = new Square(state);
} else if ("oval".equalsIgnoreCase(selected)) {
drawable = new Circle(state);
}
else if ("Line".equalsIgnoreCase(selected)){
drawable = new Line(state);
}
return drawable;
}
protected JLabel createColorLable(Color base) {
final JLabel label = new JLabel();
label.setBackground(base);
label.setBorder(new LineBorder(Color.BLACK));
label.setPreferredSize(new Dimension(25, 25));
label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
label.setOpaque(true);
label.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
Color color = JColorChooser.showDialog(label, "Color", label.getBackground());
if (color != null) {
label.setBackground(color);
}
}
});
return label;
}
public class MouseHandler extends MouseAdapter {
private Drawable drawable;
private Point clickPoint;
@Override
public void mousePressed(MouseEvent e) {
drawable = createDrawable();
drawable.setLocation(e.getPoint());
drawablePane.addDrawable(drawable);
clickPoint = e.getPoint();
}
@Override
public void mouseDragged(MouseEvent e) {
Point drag = e.getPoint();
Point start = clickPoint;
int maxX = Math.max(drag.x, start.x);
int maxY = Math.max(drag.y, start.y);
int minX = Math.min(drag.x, start.x);
int minY = Math.min(drag.y, start.y);
int width = maxX - minX;
int height = maxY - minY;
drawable.setLocation(new Point(minX, minY));
drawable.setSize(new Dimension(width, height));
drawablePane.repaint();
}
}
}
public interface Drawable {
public void paint(JComponent parent, Graphics2D g2d);
public void setLocation(Point location);
public void setSize(Dimension size);
public State getState();
public Rectangle getBounds();
}
public abstract class AbstractDrawable implements Drawable {
private Rectangle bounds;
private State state;
public AbstractDrawable(State state) {
bounds = new Rectangle();
this.state = state;
}
@Override
public State getState() {
return state;
}
public abstract Shape getShape();
@Override
public void setLocation(Point location) {
bounds.setLocation(location);
}
@Override
public void setSize(Dimension size) {
bounds.setSize(size);
}
@Override
public Rectangle getBounds() {
return bounds;
}
@Override
public void paint(JComponent parent, Graphics2D g2d) {
Shape shape = getShape();
State state = getState();
Rectangle bounds = getBounds();
final float dash1[] = {state.LineWidth};
final BasicStroke dashed =
new BasicStroke(state.DashLength,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
10.0f, dash1, 0.0f);
if (bounds.width > 0 && bounds.height > 0) {
if (state.isGradient()){
if (state.isDashed()){
Point2D startPoint = new Point2D.Double(bounds.x, bounds.y);
Point2D endPoint = new Point2D.Double(bounds.x + bounds.width, bounds.y + bounds.height);
LinearGradientPaint gp = new LinearGradientPaint(
startPoint,
endPoint,
new float[]{0f, 1f},
new Color[]{state.getForeground(), state.getBackground()});
g2d.setPaint(gp);
g2d.setStroke(dashed);
g2d.draw(shape);
}
else{
Point2D startPoint = new Point2D.Double(bounds.x, bounds.y);
Point2D endPoint = new Point2D.Double(bounds.x + bounds.width, bounds.y + bounds.height);
LinearGradientPaint gp = new LinearGradientPaint(
startPoint,
endPoint,
new float[]{0f, 1f},
new Color[]{state.getForeground(), state.getBackground()});
g2d.setPaint(gp);
g2d.fill(shape);
}
}
else if (state.isFilled()){
if (bounds.width > 0 && bounds.height > 0) {
g2d.setPaint(state.getForeground());
g2d.fill(shape);
}
}
else if(state.isDashed()){
g2d.setStroke(dashed);
g2d.setPaint(state.getForeground());
g2d.draw(shape);
}
else{
BasicStroke stroke = new BasicStroke();
g2d.setStroke(stroke);
g2d.setPaint(state.getForeground());
g2d.draw(shape);
}
}
}
}
public class Square extends AbstractDrawable {
public Square(State state) {
super(state);
}
@Override
public Shape getShape() {
return getBounds();
}
}
public class Circle extends AbstractDrawable {
public Circle(State state) {
super(state);
}
@Override
public Shape getShape() {
Rectangle bounds = getBounds();
return new Ellipse2D.Float(bounds.x, bounds.y, bounds.width, bounds.height);
}
}
public class Line extends AbstractDrawable {
public Line(State state){
super(state);
}
@Override
public Shape getShape() {
Rectangle bounds = getBounds();
return new Line2D.Float(bounds.x, bounds.y,bounds.width, bounds.height);
}
}
public class DrawablePane extends JPanel {
List<Drawable> itemsDrawn;
public DrawablePane() {
itemsDrawn = new ArrayList<>();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
for (Drawable d : itemsDrawn) {
d.paint(this, g2d);
}
g2d.dispose();
}
public void addDrawable(Drawable drawable) {
itemsDrawn.add(drawable);
repaint();
}
}
}
这是带有已实现的撤消和清除按钮的 ControlPane 类
public ControlPane(DrawablePane pane) {
shapes = new JComboBox<>(new String[]{"Rectangle", "Oval","Line"});
foreground = createColorLable(Color.BLACK);
foreground.setToolTipText("Foreground");
background = createColorLable(Color.WHITE);
background.setToolTipText("Background");
gradient = new JCheckBox("Gradient");
Filled = new JCheckBox("Filled");
dashed = new JCheckBox("Dashed");
Dashlength = new JTextField("1");
Linewidth = new JTextField("1");
Len = new JLabel("Dash Length:");
Wid = new JLabel("Line Width:");
Clear = new JButton("Clear");
Undo = new JButton("Undo");
JPanel panel = new JPanel();
panel.add(Clear);
panel.add(Undo);
panel.add(foreground);
panel.add(background);
panel.add(Filled);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
add(shapes, gbc);
add(panel, gbc);
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.NORTH;
add(gradient, gbc);
add(dashed, gbc);
add(Len);
add(Linewidth);
add(Wid);
add(Dashlength);
setBorder(new CompoundBorder(new LineBorder(Color.BLACK), new EmptyBorder(12, 12, 12, 12)));
this.drawablePane = pane;
MouseHandler mouseHandler = new MouseHandler();
drawablePane.addMouseListener(mouseHandler);
drawablePane.addMouseMotionListener(mouseHandler);
Clear.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
itemsDrawn = new ArrayList<>();
(Not sure what to use here).repaint();
}
});
undo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if (itemsDrawn.size() != 0) {
itemsDrawn.remove(itemsDrawn.size() - 1);
(Not sure what to put here).repaint();
}
}
});
}
所以像这样(DrawablePane 中的 clear 和 undo 方法)
Clear.addActionListner(new ActionListener() {
@Override
public void actionPreformed(ActionEvent arg0){
Clear();
}
});
Undo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
Undo();
}
});
public class DrawablePane extends JPanel {
List<Drawable> itemsDrawn;
public DrawablePane() {
itemsDrawn = new ArrayList<>();
}
public void undo(){
if (itemsDrawn.size() != 0) {
itemsDrawn.remove(itemsDrawn.size() - 1);
repaint();
}
}
public void Clear(){
itemsDrawn = new ArrayList<>();
repaint();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
for (Drawable d : itemsDrawn) {
d.paint(this, g2d);
}
g2d.dispose();
}
public void addDrawable(Drawable drawable) {
itemsDrawn.add(drawable);
repaint();
}
}
}
我收到一条错误消息,提示无法在 DrawablePane 中找到清除和撤消
最佳答案
主要问题是itemsDrawn
由 DrawablePane
使用不是 ControlPane
,因此尝试将其从 ControlPane
更改为对 DrawablePane
没有影响.
相反,您应该向 DrawablePane
添加两个方法叫,undo
和 clear
,可以从您的 ActionListener
调用在ControlPane
更新了示例
首先,添加一个undo
和 clear
DrawablePane
的方法...
public class DrawablePane extends JPanel {
List<Drawable> itemsDrawn;
public DrawablePane() {
itemsDrawn = new ArrayList<>();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
for (Drawable d : itemsDrawn) {
d.paint(this, g2d);
}
g2d.dispose();
}
public void addDrawable(Drawable drawable) {
itemsDrawn.add(drawable);
repaint();
}
public void undo() {
if (!itemsDrawn.isEmpty()) {
int size = itemsDrawn.size() - 1;
itemsDrawn.remove(size);
repaint();
}
}
public void clear() {
itemsDrawn.clear();
repaint();
}
}
其次,从适当的 ActionListener
调用方法...
clear.addActionListner(new ActionListener() {
@Override
public void actionPreformed(ActionEvent arg0) {
drawablePane.clear();
}
});
undo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
drawablePane.undo();
}
});
显然,这些将在您的 ControlPane
中创建
关于java - 在java绘图程序中添加clear和undo Jbutton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23119830/
为了重置我的最后一次提交,我这样做了: git reset --soft HEAD^ 但是我做了太多次了。现在我想往回走几步。这可能吗? 另外,我有一个开发人员在一个生产应用程序上提取和 merge
在 Vim 中,我做了太多撤消操作。我如何撤消此操作(即重做)? 最佳答案 Ctrl+r 关于vim - 如何在 Vim 中重做(即 "undo undo")?,我们在Stack Overflow上找
在过去的 3 周里,我一直在尝试能够以编程方式调用撤消命令。我最近的尝试涉及从 C# 调用 LISP 代码。现在我的 C# 方法看起来像这样 [CommandMethod("TestLispFromC
如何使用 ctrl+z (ApplicationCommand.Undo) 键来实现我们自己的撤消方法,而不是使用 WPF 中的默认命令。基本上用我们自己的撤消方法替换默认命令。 例如:我想将 ctr
在 Word 窗口中,执行诸如键入、格式化字体、段落...以确保撤消列表不为空,然后通过单击功能区上的任何样式来更改某些文本的样式。撤消列表中会出现一个名为“应用快速样式”的条目。然后运行宏,如: S
我有一个在“Worksheet_SelectionChange” 事件上触发的宏。宏验证一列的数据,如果错误则更改单元格的背景颜色。 问题是运行宏后,它会清除所有文档的更改历史记录(Ctrl Z),甚
有什么方法可以撤销 Mikephil 图表上的选择?当我在条形图中选择一个值时,我有一个应用程序会打开一个 Activity 。这很好用,但是,当我返回到包含图表的 Activity 时,选择仍然存在
完成此任务的最佳做法是什么: 我有提交: A -- B -- C 提交 B 包括: file1.java file2.java icon.png 我想撤消对 file1 和 file2 的更改,但不想
我刚刚用 git branch -D branchName 进行了一些我需要的实验性更改删除了错误的分支。 如何恢复分支? 最佳答案 您可以使用 git reflog找到分支最后一次提交的 SHA1。
所以我正在使用 CodeMirror,我想要一种方法来从 undo 状态中省略某些编辑。特别是,我遇到了一种情况,我想要一次击键 替换部分镜像文本并 自动缩进新区域 天真地这样做意味着使用击键,然后点
我正在编写指令,它将像这样: 允许编辑一些文本(使用内容可编辑) 在失去焦点时,它应该将其值保存到模型(最近观看并保存到 DB) 应该有按钮“撤消”可以恢复更改。 我的实现是:http://plnkr
我听说纯函数式数据结构的好处之一是您可以免费获得撤消/重做操作。有人可以解释为什么吗?我不明白为什么在函数式语言中添加撤消/重做更容易。 例如,假设我有以下队列实现: data Queue a = Q
我有一个内置于 iPhone 核心数据中的数据模型。我还有一个适用于该模型的 NSUndoManager 。问题是,我不想在撤消过程中影响实体上的某些属性,而我确实希望回滚其他属性。在不彻底改变事情的
令我惊讶的是,我找不到有关该主题的任何信息,但是搜索此类内容总是会产生很多相似但不相关的内容... 无论如何,一些注意事项以避免误解:- 问题与 HTML、HTTP、Javascript 和浏览器行为
这个问题已经有答案了: 已关闭13 年前。 Possible Duplicate: Design Pattern for Undo Engine 一般来说,您如何处理在应用程序中支持“撤消”功能?我曾
我一直在问很多有关事件溯源的问题,因此对此表示歉意,但我想从一开始就解决这个问题。 设置 | p_key | invoice_id | EmployeeId | Event type |
我从主分支在 Visual Studio 中做了一些代码更改,并试图在 TortoiseHG 的新分支中提交更改,但显然我不小心将更正提交到主分支。我没有将更改推送到远程存储库,但如果我尝试对更改执行
有没有办法在单击后退按钮时“撤消”由 jQuery 执行的函数?例如,我要执行的函数名为 doSomething: function doSomething(button) { ...cli
假设我有这个迁移: class MigrateStuff false add_index :contacts, :receive_newsletter for t in SomeOt
现状 我有一个使用 MVVM 和 NHibernate 的 WPF 应用程序。我有一个包含所有客户的列表框的主/详细信息窗口,列表框的 selectedItem 是用于在详细信息屏幕中显示客户详细信息
我是一名优秀的程序员,十分优秀!