Programming Assignment Unit 6 1
Programming Assignment Unit 6 1
/**
* It is modified File DrawTextPanel.java
*/
package textcollage;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
/**
* The panel that contains a large drawing area where strings
* it can be drawn. The strings are represented by objects of
* the type DrawTextItem. An input box under the panel allows
* of the user to specify what string will be drawn when the
* the user clicks on the drawing area of it.
*/
public class DrawTextPanel extends JPanel {
/**
* An object of type Canvas is used for the drawing area.
* The canvas simply displays all the DrawTextItems that
* are stored in the ArrayList, strings.
*/
private class Canvas extends JPanel {
Canvas() {
setPreferredSize( new Dimension(800,600) );
setBackground(Color.LIGHT_GRAY);
setFont( new Font( "Serif", Font.BOLD, 24 ));
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
if (theString != null) {
for (DrawTextItem thisString: theString) {
thisString.draw(g);
}
}
}
}
/**
* An object of type MenuHandler is registered as the ActionListener
* for all the commands in the menu bar. The MenuHandler object
* simply calls doMenuCommand() when the user selects a command
* from the menu.
*/
private class MenuHandler implements ActionListener {
public void actionPerformed(ActionEvent evt) {
doMenuCommand( evt.getActionCommand());
}
}
/**
* Creates a DrawTextPanel. The panel has a large drawing area and
* a text input box where the user can specify a string. When the
* user clicks the drawing area, the string is added to the drawing
* area at the point where the user clicked.
*/
public DrawTextPanel() {
fileChooser = new SimpleFileChooser();
undoMenuItem = new JMenuItem("Remove Item");
undoMenuItem.setEnabled(false);
menuHandler = new MenuHandler();
setLayout(new BorderLayout(3,3));
setBackground(Color.BLACK);
setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
canvas = new Canvas();
add(canvas, BorderLayout.CENTER);
JPanel bottom = new JPanel();
bottom.add(new JLabel("Text to add: "));
input = new JTextField("Hello I love the Univeristy of the People!", 40);
bottom.add(input);
add(bottom, BorderLayout.SOUTH);
canvas.addMouseListener( new MouseAdapter() {
public void mousePressed(MouseEvent e) {
doMousePress( e );
}
} );
}
/**
* This method is called when the user clicks the drawing area.
* A new string is added to the drawing area. The center of
* the string is at the point where the user clicked.
* @param e the mouse event that was generated when the user clicked
*/
public void doMousePress( MouseEvent e ) {
String text = input.getText().trim();
if (text.length() == 0) {
input.setText("Hello I love the Univeristy of the People!");
text = "Hello I love the Univeristy of the People!";
}
DrawTextItem s = new DrawTextItem( text, e.getX(), e.getY() );
s.setTextColor(currentTextColor); // Default is null, meaning default color of
the canvas (black).
/**
* Returns a menu bar containing commands that affect this panel. The menu
* bar is meant to appear in the same window that contains this panel.
*/
public JMenuBar getMenuBar() {
if (menuBar == null) {
menuBar = new JMenuBar();
}
return menuBar;
}
/**
* Carry out one of the commands from the menu bar.
* @param command the text of the menu command.
*/
private void doMenuCommand(String command) {
if (command.equals("Save...")) { // save all the string info to a file
//JOptionPane.showMessageDialog(this, "Sorry, the Save command is not
implemented.");
// write bg color
bw.write(canvas.getHeight() + "~"+ canvas.getWidth()
+"~"+canvas.getBackground().getRGB());
bw.close();
}
catch (Exception e) {
JOptionPane.showMessageDialog(this,
"Sorry, an error occurred while trying to save the image
details:\n" + e);
}
}
else if (command.equals("Open...")) { // read a previously saved file, and
reconstruct the list of strings
//JOptionPane.showMessageDialog(this, "Sorry, the Open command is not
implemented.");
File textFile = fileChooser.getInputFile(this, "Select Text File Name");
if (textFile == null)
return;
try {
Scanner sc = new Scanner(textFile);
String meta = sc.nextLine();
canvas.setSize(Integer.parseInt(meta.split("~")[1]),
Integer.parseInt(meta.split("~")[0]));
canvas.setBackground(new Color(Integer.parseInt(meta.split("~")[2])));
//canvas.repaint();
sc.close();
}
catch (Exception e) {
JOptionPane.showMessageDialog(this,
"Sorry, an error occurred while trying to save the image
details:\n" + e);
}
canvas.repaint(); // (you'll need this to make the new list of strings
take effect)
}
else if (command.equals("Clear")) { // remove all strings
theString = new ArrayList<DrawTextItem>(); // Remove the ONLY string
from the canvas.
undoMenuItem.setEnabled(false);
canvas.repaint();
}
else if (command.equals("Remove Item")) { // remove the most recently added
string
theString.remove(theString.size()-1); // Remove the ONLY string from the
canvas.
if (theString.size()==0) {
undoMenuItem.setEnabled(false);
}
canvas.repaint();
}
else if (command.equals("Set Text Color...")) {
Color c = JColorChooser.showDialog(this, "Select Text Color",
currentTextColor);
if (c != null)
currentTextColor = c;
}
else if (command.equals("Set Background Color...")) {
Color c = JColorChooser.showDialog(this, "Select Background Color",
canvas.getBackground());
if (c != null) {
canvas.setBackground(c);
canvas.repaint();
}
}
else if (command.equals("Save Image...")) { // save a PNG image of the drawing
area