Instructions
Requirements and Specifications
- The window title must be "TCSS 305 Paint – Autumn 2022" and the title bar must contain an icon other than the default Java icon. Be creative; choose an icon that seems appropriate to you. Use the same icon on the “About” message described later. (The screenshots show a University of Washington icon.)
- The initial window width should be 1/3 of the screen width and the initial window height should be 1/3 of the screen height and the window should be resizable.
- The GUI should use the ‘Metal’ look and feel as shown in the second JFrame code example this quarter.
- When the program opens the window should be centered on the screen.
- When the window is closed, the program should terminate.
- Below are some screen shots of the window in various states:
Source Code
LINE
package model;
import java.awt.*;
public class Line extends Shape {
private final int x1;
private final int y1;
private final int x2;
private final int y2;
public Line(Color color, int thickness, int x1, int y1, int x2, int y2) {
super(color, thickness);
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}
public int getX1() {
return x1;
}
public int getY1() {
return y1;
}
public int getX2() {
return x2;
}
public int getY2() {
return y2;
}
@Override
public void drawShape(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.setColor(getColor());
g2d.setStroke(new BasicStroke(getThickness()));
g.drawLine(x1, y1, x2, y2);
}
}
SHAPE
package model;
import java.awt.*;
public abstract class Shape {
private final Color color;
private final int thickness;
public Shape(Color color, int thickness) {
this.color = color;
this.thickness = thickness;
}
public Color getColor() {
return color;
}
public int getThickness() {
return thickness;
}
public abstract void drawShape(Graphics g);
}
PAINT GUI
/*
* TCSS 305 - Assignment 5
*/
package view;
import model.Line;
import model.Shape;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
/**
* Presents the GUI for the PowerPaint application.
*
* @author Alan Fowler (acfowler@uw.edu)
* @version Autumn 2022
*/
public final class PaintGUI {
private static final Color UW_PURPLE = new Color(58, 32, 91);
// constants (if any)
// instance fields
private JFrame myFrame;
private java.util.List<Shape> shapes = new ArrayList<>();
private Color color = UW_PURPLE;
private int thickness = 3;
public PaintGUI() {
super();
// initialize instance fields here
myFrame = new JFrame("Place holder.");
myFrame.setTitle("TCSS 305 Paint - Autumn 2022");
myFrame.setIconImage(new ImageIcon("files/w_small.png").getImage());
myFrame.setLayout(new BorderLayout());
JMenuBar jMenuBar = new JMenuBar();
JMenu optionsMenu = new JMenu("Options");
JMenu thicknessMenuItem = new JMenu("Thickness");
JSlider thicknessSlider = new JSlider();
thicknessSlider.setMinimum(0);
thicknessSlider.setMaximum(15);
thicknessSlider.setPaintTicks(true);
thicknessSlider.setMinorTickSpacing(1);
thicknessSlider.setMajorTickSpacing(5);
thicknessSlider.setPaintLabels(true);
thicknessSlider.setValue(3);
thicknessMenuItem.add(thicknessSlider);
optionsMenu.add(thicknessMenuItem);
optionsMenu.addSeparator();
JMenuItem colorMenuItem = new JMenuItem("Color...");
colorMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JColorChooser.showDialog(myFrame.getContentPane(), "Choose a color", color);
}
});
optionsMenu.add(colorMenuItem);
optionsMenu.addSeparator();
JMenuItem clearMenuItem = new JMenuItem("Clear");
clearMenuItem.setEnabled(false);
optionsMenu.add(clearMenuItem);
jMenuBar.add(optionsMenu);
JMenu toolsMenu = new JMenu("Tools");
ButtonGroup tools = new ButtonGroup();
JRadioButtonMenuItem lineTool = new JRadioButtonMenuItem("Line", new ImageIcon("files/line_bw.gif"));
JRadioButtonMenuItem rectangleTool = new JRadioButtonMenuItem("Rectangle", new ImageIcon("files/rectangle_bw.gif"));
JRadioButtonMenuItem ellipseTool = new JRadioButtonMenuItem("Ellipse", new ImageIcon("files/ellipse_bw.gif"));
JRadioButtonMenuItem pencilTool = new JRadioButtonMenuItem("Pencil", new ImageIcon("files/pencil_bw.gif"));
tools.add(lineTool);
toolsMenu.add(lineTool);
tools.add(rectangleTool);
toolsMenu.add(rectangleTool);
tools.add(ellipseTool);
toolsMenu.add(ellipseTool);
tools.add(pencilTool);
toolsMenu.add(pencilTool);
jMenuBar.add(toolsMenu);
JMenu helpMenu = new JMenu("Help");
JMenuItem aboutItem = new JMenuItem("About...");
aboutItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(myFrame.getContentPane(), "Alan Fowler" + System.lineSeparator() + "Autumn 2022", "TCSS 305 Paint", JOptionPane.INFORMATION_MESSAGE, new ImageIcon("files/w_small.png"));
}
});
helpMenu.add(aboutItem);
jMenuBar.add(helpMenu);
JToolBar toolBar = new JToolBar();
ButtonGroup toolButtonGroup = new ButtonGroup();
JToggleButton lineButton = new JToggleButton("Line", new ImageIcon("files/line_bw.gif"));
toolButtonGroup.add(lineButton);
toolBar.add(lineButton);
JToggleButton rectangleButton = new JToggleButton("Rectangle", new ImageIcon("files/rectangle_bw.gif"));
toolButtonGroup.add(rectangleButton);
toolBar.add(rectangleButton);
JToggleButton ellipseButton = new JToggleButton("Ellipse", new ImageIcon("files/ellipse_bw.gif"));
toolButtonGroup.add(ellipseButton);
toolBar.add(ellipseButton);
JToggleButton pencilButton = new JToggleButton("Pencil", new ImageIcon("files/pencil_bw.gif"));
toolButtonGroup.add(pencilButton);
toolBar.add(pencilButton);
myFrame.add(toolBar, BorderLayout.SOUTH);
lineTool.addActionListener(e -> {
if (lineTool.isSelected()) {
lineButton.setSelected(true);
}
});
lineButton.addActionListener(e -> {
if (lineButton.isSelected()) {
lineTool.setSelected(true);
}
});
rectangleTool.addActionListener(e -> {
if (rectangleTool.isSelected()) {
rectangleButton.setSelected(true);
}
});
rectangleButton.addActionListener(e -> {
if (rectangleButton.isSelected()) {
rectangleTool.setSelected(true);
}
});
ellipseTool.addActionListener(e -> {
if (ellipseTool.isSelected()) {
ellipseButton.setSelected(true);
}
});
ellipseButton.addActionListener(e -> {
if (ellipseButton.isSelected()) {
ellipseTool.setSelected(true);
}
});
pencilTool.addActionListener(e -> {
if (pencilTool.isSelected()) {
pencilButton.setSelected(true);
}
});
pencilButton.addActionListener(e -> {
if (pencilButton.isSelected()) {
pencilTool.setSelected(true);
}
});
myFrame.setJMenuBar(jMenuBar);
myFrame.add(new Canvas(), BorderLayout.CENTER);
myFrame.pack();
lineTool.setSelected(true);
// setup and display the GUI
start();
}
/**
* Performs all tasks necessary to display the UI.
*/
protected void start() {
try {
UIManager.setLookAndFeel("Metal");
} catch (Exception ignored) {
}
// replace the temporary code shown below with your code
myFrame.setLocationRelativeTo(null);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
myFrame.setSize(new Dimension(Math.toIntExact(Math.round(screenSize.getWidth() / 3)), Math.toIntExact(Math.round(screenSize.getHeight() / 3))));
myFrame.setVisible(true);
myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private class Canvas extends JPanel {
private Shape currentShape;
Canvas() {
setBackground(Color.WHITE);
MouseAdapter ma = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
super.mousePressed(e);
if (SwingUtilities.isLeftMouseButton(e)) {
if (currentShape == null) {
currentShape = new Line(color, thickness, e.getX(), e.getY(), e.getX(), e.getY());
System.out.println("pressed");
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
super.mouseReleased(e);
if (SwingUtilities.isLeftMouseButton(e)) {
if (currentShape != null) {
shapes.add(currentShape);
currentShape = null;
System.out.println("released");
}
}
}
@Override
public void mouseDragged(MouseEvent e) {
super.mouseMoved(e);
if (SwingUtilities.isLeftMouseButton(e)) {
if (currentShape != null) {
Line line = (Line) currentShape;
int x1 = line.getX1();
int y1 = line.getY1();
currentShape = new Line(color, thickness, x1, y1, e.getX(), e.getY());
System.out.println("moved");
repaint();
revalidate();
}
}
}
};
addMouseListener(ma);
addMouseMotionListener(ma);
}
@Override
public void paint(Graphics g) {
super.paint(g);
for (Shape shape : shapes) {
shape.drawShape(g);
}
if (currentShape != null) {
currentShape.drawShape(g);
}
}
}
}