Use icon instead of text for Paths panel buttons.

This commit is contained in:
Holt59 2018-03-05 21:46:13 +01:00
parent 0102277821
commit d73430759e

View File

@ -3,6 +3,7 @@ package org.insa.graphics;
import java.awt.Color; import java.awt.Color;
import java.awt.Component; import java.awt.Component;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image; import java.awt.Image;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
@ -17,6 +18,7 @@ import java.io.IOException;
import javax.swing.BorderFactory; import javax.swing.BorderFactory;
import javax.swing.Box; import javax.swing.Box;
import javax.swing.BoxLayout; import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JCheckBox; import javax.swing.JCheckBox;
@ -50,6 +52,42 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh
*/ */
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/**
* Simple icon that represents a unicolor rectangle.
*
*/
protected class ColorIcon implements Icon {
private Color color;
private int width, height;
public ColorIcon(Color color, int width, int height) {
this.width = width;
this.height = height;
this.color = color;
}
public void setColor(Color color) {
this.color = color;
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(this.color);
g.fillRect(x, y, getIconWidth(), getIconHeight());
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}
// Solution // Solution
private final Path path; private final Path path;
@ -69,8 +107,7 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh
public PathPanel(Path path) { public PathPanel(Path path) {
super(); super();
setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS)); setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
setBorder(BorderFactory.createCompoundBorder( setBorder(BorderFactory.createCompoundBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.GRAY),
BorderFactory.createMatteBorder(0, 0, 1, 0, Color.GRAY),
new EmptyBorder(5, 0, 5, 0))); new EmptyBorder(5, 0, 5, 0)));
this.path = path; this.path = path;
this.overlay = drawing.drawPath(this.path); this.overlay = drawing.drawPath(this.path);
@ -118,13 +155,13 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh
} }
}); });
colorButton = new JButton(" "); Dimension size = new Dimension(24, 24);
colorButton.setOpaque(true);
ColorIcon icon = new ColorIcon(overlay.getColor(), 14, 14);
colorButton = new JButton(icon);
colorButton.setFocusable(false);
colorButton.setFocusPainted(false);
colorButton.setBorderPainted(false); colorButton.setBorderPainted(false);
colorButton.setBackground(this.overlay.getColor());
colorButton.setBorder(new EmptyBorder(0, 0, 0, 0));
Dimension size = colorButton.getPreferredSize();
size.setSize(size.height, size.height);
colorButton.setMinimumSize(size); colorButton.setMinimumSize(size);
colorButton.setPreferredSize(size); colorButton.setPreferredSize(size);
colorButton.setMaximumSize(size); colorButton.setMaximumSize(size);
@ -139,27 +176,27 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh
chooser.getSelectionModel().addChangeListener(new ChangeListener() { chooser.getSelectionModel().addChangeListener(new ChangeListener() {
@Override @Override
public void stateChanged(ChangeEvent e) { public void stateChanged(ChangeEvent e) {
colorButton icon.setColor(chooser.getSelectionModel().getSelectedColor());
.setBackground(chooser.getSelectionModel().getSelectedColor()); colorButton.repaint();
overlay.setColor(chooser.getSelectionModel().getSelectedColor()); overlay.setColor(chooser.getSelectionModel().getSelectedColor());
overlay.redraw(); overlay.redraw();
} }
}); });
JColorChooser.createDialog(getTopLevelAncestor(), "Pick a new color", true, JColorChooser.createDialog(getTopLevelAncestor(), "Pick a new color", true, chooser,
chooser, new ActionListener() { new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
colorButton.setBackground( icon.setColor(chooser.getSelectionModel().getSelectedColor());
chooser.getSelectionModel().getSelectedColor()); colorButton.repaint();
overlay.setColor( overlay.setColor(chooser.getSelectionModel().getSelectedColor());
chooser.getSelectionModel().getSelectedColor());
overlay.redraw(); overlay.redraw();
} }
}, new ActionListener() { }, new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
colorButton.setBackground(originalColor); icon.setColor(originalColor);
colorButton.repaint();
overlay.setColor(originalColor); overlay.setColor(originalColor);
overlay.redraw(); overlay.redraw();
} }
@ -170,6 +207,12 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh
}); });
JButton saveButton = new JButton(UIManager.getIcon("FileView.floppyDriveIcon")); JButton saveButton = new JButton(UIManager.getIcon("FileView.floppyDriveIcon"));
saveButton.setFocusPainted(false);
saveButton.setFocusable(false);
saveButton.setBorderPainted(false);
saveButton.setMinimumSize(size);
saveButton.setPreferredSize(size);
saveButton.setMaximumSize(size);
saveButton.addActionListener(new ActionListener() { saveButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
@ -182,12 +225,11 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh
fileChooser.setApproveButtonText("Save"); fileChooser.setApproveButtonText("Save");
fileChooser.setToolTipText("Save"); fileChooser.setToolTipText("Save");
if (fileChooser if (fileChooser.showSaveDialog(getTopLevelAncestor()) == JFileChooser.APPROVE_OPTION) {
.showSaveDialog(getTopLevelAncestor()) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile(); File file = fileChooser.getSelectedFile();
try { try {
BinaryPathWriter writer = new BinaryPathWriter(new DataOutputStream( BinaryPathWriter writer = new BinaryPathWriter(
new BufferedOutputStream(new FileOutputStream(file)))); new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file))));
writer.writePath(path); writer.writePath(path);
} }
catch (IOException e1) { catch (IOException e1) {
@ -199,13 +241,15 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh
} }
}); });
Image newimg = new ImageIcon("res/cross_mark.png").getImage() Image newimg = new ImageIcon("res/cross_mark.png").getImage().getScaledInstance(14, 14,
.getScaledInstance(size.width, size.height, java.awt.Image.SCALE_SMOOTH); java.awt.Image.SCALE_SMOOTH);
JButton deleteButton = new JButton(new ImageIcon(newimg)); JButton deleteButton = new JButton(new ImageIcon(newimg));
deleteButton.setFocusPainted(false);
deleteButton.setFocusable(false);
deleteButton.setBorderPainted(false);
deleteButton.setMinimumSize(size); deleteButton.setMinimumSize(size);
deleteButton.setPreferredSize(size);
deleteButton.setMaximumSize(size); deleteButton.setMaximumSize(size);
deleteButton.setBorderPainted(true);
deleteButton.setOpaque(false);
deleteButton.addActionListener(new ActionListener() { deleteButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
@ -231,18 +275,20 @@ public class PathsPanel extends JPanel implements DrawingChangeListener, GraphCh
public void updateOverlay() { public void updateOverlay() {
PathOverlay oldOverlay = this.overlay; PathOverlay oldOverlay = this.overlay;
this.overlay = drawing.drawPath(path); this.overlay = drawing.drawPath(path);
this.colorButton.setBackground(this.overlay.getColor()); this.overlay.setColor(oldOverlay.getColor());
((ColorIcon) this.colorButton.getIcon()).setColor(this.overlay.getColor());
this.colorButton.repaint();
this.overlay.setVisible(oldOverlay.isVisible()); this.overlay.setVisible(oldOverlay.isVisible());
oldOverlay.delete(); oldOverlay.delete();
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
*
* @see java.lang.Object#toString() * @see java.lang.Object#toString()
*/ */
public String toString() { public String toString() {
return "Path from #" + path.getOrigin().getId() + " to #" return "Path from #" + path.getOrigin().getId() + " to #" + path.getDestination().getId();
+ path.getDestination().getId();
} }
} }