https://codehs.com/editor/1739998/
or https://codehs.com/student/3056506/section/287940/assignment/41268354/
Hi could you help me fix this error and complete the assignments:
Steganography.java:19: error: expected public static void clearLow(Pixelp ) ^ Steganography.java:35: error: reached end of file while parsing } ^ 2 errors
Steganography.java
import java.awt.Color; public class Steganography { public static void main(String[] args) { //Notice how the picture needs to be created! //Don't change the String value in the Picture parameter. Picture beach = new Picture("./labs/steganography/beach.jpg"); beach.explore(); Picture copy = testClearLow(beach); copy.explore(); } /** * Clear the lower (rightmost) two bits in a pixel. */
public static void clearLow(Pixelp ) { p.setRed(p.getRed()/4 * 4); p.setGreen(p.getGreen()/4 * 4); p.setBlue(p.getBlue()/4 * 4); } /** * Returns a new Picture object with the lowest two bits of each pixel cleared. */ public static Picture testClearLow(Picture){ Picture newPicture = new Picture(pic); Pixel[] pixels = newPicture.getPixels(); for (Pixel p:pixels){ clearLow(p); } return newPicture; } }
33279490
ColorChooser.java
import javax.swing.JColorChooser; import javax.swing.JFrame; import java.awt.Color;
/** * A class to make working with a color chooser easier for students. It uses a * JColorChooser to let the user pick a color and returns the chosen color * object. * */ public class ColorChooser {
/** * Method to let the user pick a color and return the color object. * * @return the picked color or red if no color was picked */ public static Color pickAColor() { Color color = Color.white;
// creat JFrame to be the parent of the color chooser open dialog // if you don't then you may not see the dialog. JFrame frame = new JFrame(); frame.setAlwaysOnTop(true);
// use the color chooser to pick the color color = JColorChooser.showDialog(frame, "Pick a color", color);
return color; }
/** * Main method for testing the ColorChooser */ public static void main(String[] args) { Color pickedColor = ColorChooser.pickAColor(); System.out.println(pickedColor); }
}
FileChooser.java
import javax.swing.JFileChooser; import javax.swing.JFrame; import java.util.Properties; import java.io.*; import java.net.*; /** * A class to make working with a file chooser easier * for students. It uses a JFileChooser to let the user * pick a file and returns the chosen file name. * */ public class FileChooser { /////////////////////// methods ///////////////////////////// /** * Method to get the full path for the passed file name * @param fileName the name of a file * @return the full path for the file */ public static String getMediaPath(String fileName) { String path = null; String directory = getMediaDirectory(); boolean done = true; // get the full path path = directory + fileName; return path; } /** * Method to pick an item using the file chooser * @param fileChooser the file Chooser to use * @return the path name */ public static String pickPath(JFileChooser fileChooser) { String path = null; /* JFrame to be the parent of the file * chooser open dialog if you don't then * you may not see the dialog. */ JFrame frame = new JFrame(); frame.setAlwaysOnTop(true); // get the return value from choosing a file int returnVal = fileChooser.showOpenDialog(frame); // if the return value says the user picked a file if (returnVal == JFileChooser.APPROVE_OPTION) path = fileChooser.getSelectedFile().getPath(); return path; } /** * Method to let the user pick a file and return * the full file name as a string. If the user didn't * pick a file then the file name will be null. * @return the full file name of the picked file or null */ public static String pickAFile() { JFileChooser fileChooser = null; // start off the file name as null String fileName = null; // get the current media directory String mediaDir = getMediaDirectory(); /* file for this and check that the directory exists * and if it does set the file chooser to use it */ try { File file = new File(mediaDir); if (file.exists()) fileChooser = new JFileChooser(file); } catch (Exception ex) { } // if no file chooser yet create one if (fileChooser == null) fileChooser = new JFileChooser(); // pick the file fileName = pickPath(fileChooser); return fileName; } /** * Method to get the directory for the media * @return the media directory */ public static String getMediaDirectory() { String directory = null; boolean done = false; File dirFile = null; // try to find the images directory try { // get the URL for where we loaded this class Class currClass = Class.forName("FileChooser"); URL classURL = currClass.getResource("FileChooser.class"); URL fileURL = new URL(classURL,"../images/"); directory = fileURL.getPath(); directory = URLDecoder.decode(directory, "UTF-8"); dirFile = new File(directory); if (dirFile.exists()) { //setMediaPath(directory); return directory; } } catch (Exception ex) { } return directory; } }
ImageDisplay.java
import javax.swing.*; import java.awt.*; import java.awt.image.*;
/** * Class to display an image and the current location with a + sign * */ public class ImageDisplay extends JPanel implements Scrollable { /////////////////////////// fields (attributes /////////////////////////// /** the image to draw */ private Image image; /** the preferred size of the display */ private Dimension prefSize; /** the current x index */ private int currentX = 0; /** the current y index */ private int currentY = 0; //////////////////////////// constructors ///////////////////////////////// /** * Constructor that takes the image to display * @param theImage the image to display */ public ImageDisplay(Image theImage) { image = theImage; prefSize = new Dimension(image.getWidth(this),image.getHeight(this)); setPreferredSize(prefSize); revalidate(); } /** * Constructor that takes the image and current x and y * @param theImage the image to display * @param x the current x value to use * @param y the current y value to use */ public ImageDisplay(Image theImage, int x, int y) { this(theImage); currentX = x; currentY = y; } ////////////////////// methods ///////////////////////////////////////////// /** * Method to get the image * @return the image */ public Image getImage() { return image; } /** * Method to get the current x * @return the current x value */ public int getCurrentX() { return currentX; } /** * Method to get the current y * @return the current y value */ public int getCurrentY() { return currentY; } /** * Method to set the current x * @param x the x value to use */ public void setCurrentX(int x) { currentX = x; repaint(); } /** * Method to set the current y * @param y the y value to use */ public void setCurrentY(int y) { currentY = y; repaint(); } /** * Method to set the image * @param theImage the new image to use */ public void setImage(Image theImage) { image = theImage; setPreferredSize(new Dimension(image.getWidth(this),image.getHeight(this))); repaint(); } /** * Method to return the preferred size * @return the preferred size of this component */ public Dimension getPreferredScrollableViewportSize() { return prefSize; } /** * Method to return the unit increment for scrolling * @param visibleRect the visible rectangle * @param orientation vertical or horizontal * @param direction neg is up or left and pos is right or down * @return the unit increment for arrow clicks */ public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { return 1; } /** * Method to return the block increment for scrolling * @param visibleRect the visible rectangle * @param orientation vertical or horizontal * @param direction neg is up or left and pos is right or down * @return the block increment for clicking in scroll area */ public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) { return 10; } /** * Method to check if the viewport width is the source width * @return true if viewport and source have same width */ public boolean getScrollableTracksViewportWidth() { return false; } /** * Method to check if the viewport height is the source height * @return true if viewport and source have same height */ public boolean getScrollableTracksViewportHeight() { return false; } /** * Method to handle displaying this object * @param g the graphics object for drawing with */ public void paintComponent(Graphics g) { super.paintComponent(g); int num = 3; int xStart = currentX - num; int xEnd = currentX + num; int yStart = currentY - num; int yEnd = currentY + num; int width = image.getWidth(this); int maxX = width - 1; int height = image.getHeight(this); int maxY = height - 1; // draw the image g.drawImage(image,0,0,this); // check if the current index is in the image if (currentX >= 0 && currentX < width && currentY >= 0 && currentY < height) { // check that the start and end values are visible if (xStart < 0) xStart = 0; if (xEnd > maxX) xEnd = maxX; if (yStart < 0) yStart = 0; if (yEnd > maxY) yEnd = maxY; // draw a small cross at the current x and y in yellow g.setColor(Color.yellow); g.drawLine(xStart,currentY,xEnd,currentY); g.drawLine(currentX,yStart,currentX,yEnd); g.setColor(Color.black); // outline the cross in black so that it shows up better int leftX = currentX - 1; int rightX = currentX + 1; int upY = currentY - 1; int downY = currentY + 1; if (xStart <= leftX && upY >= 0) g.drawLine(xStart,upY,leftX,upY); if (yStart <= upY && leftX >= 0) g.drawLine(leftX,yStart,leftX,upY); if (yStart <= upY && rightX <= maxX) g.drawLine(rightX,yStart,rightX,upY); if (upY >= 0 && rightX <= xEnd) g.drawLine(rightX,upY,xEnd,upY); if (downY < height && rightX <= xEnd) g.drawLine(rightX,downY,xEnd,downY); if (downY <= yEnd && rightX < width) g.drawLine(rightX,downY,rightX,yEnd); if (xStart <= leftX && downY < height) g.drawLine(xStart,downY,leftX,downY); if (leftX >= 0 && downY <= yEnd) g.drawLine(leftX,downY,leftX,yEnd); } } }
PictureFrame.java
import javax.swing.*; import java.awt.*;
/** * Class that holds a digital picture and displays it using a JFrame * */ public class PictureFrame { ////////////////// fields //////////////////////////// /** Main window used as the frame */ JFrame frame = new JFrame(); /** ImageIcon used to display the picture in the label*/ ImageIcon imageIcon = new ImageIcon(); /** Label used to display the picture */ private JLabel label = new JLabel(imageIcon); /** Digital Picture to display */ private DigitalPicture picture; ///////////////// constructors //////////////////////// /** * A constructor that takes no arguments. This is needed * for subclasses of this class */ public PictureFrame() { // set up the frame initFrame(); } /** * A constructor that takes a picture to display * @param picture the digital picture to display in the * picture frame */ public PictureFrame(DigitalPicture picture) { // set the current object's picture to the passed in picture this.picture = picture; // set up the frame initFrame(); } ///////////////////////// methods /////////////////////////////// /** * Method to set the picture to show in this picture frame * @param picture the new picture to use */ public void setPicture(Picture picture) { this.picture = picture; imageIcon.setImage(picture.getImage()); frame.pack(); frame.repaint(); } /** * A method to update the picture frame image with the image * in the picture */ public void updateImage() { // only if there is a picture if (picture != null) { // set the image for the image icon from the picture imageIcon.setImage(picture.getImage()); // set the title of the frame to the title of the picture frame.setTitle(picture.getTitle()); } } /** * A method to update the picture frame image with the image in * the picture and show it */ public void updateImageAndShowIt() { // first update the image updateImage(); // now make sure it is shown frame.setVisible(true); } /** * A method to make sure the frame is displayed */ public void displayImage() { frame.setVisible(true); } /** * A method to hide the frame */ public void hide() { frame.setVisible(false); } /** * A method to set the visible flag on the frame * @param flag the flag to use */ public void setVisible(boolean flag) { frame.setVisible(flag); } /** * A method to close a picture frame */ public void close() { frame.setVisible(false); frame.dispose(); } /** * Method to set the title for the picture frame * @param title the title to use */ public void setTitle(String title) { frame.setTitle(title); } /** * Method to force the picture frame to repaint (redraw) */ public void repaint() { // make it visible frame.setVisible(true); // update the image from the picture updateImage(); // tell the JFrame to handle the repaint frame.repaint(); } /** * A method to initialize the picture frame */ private void initFrame() { // set the image for the picture frame updateImage(); // add the label to the frame frame.getContentPane().add(label); // pack the frame (set the size to as big as it needs to be) frame.pack(); // make the frame visible frame.setVisible(true); } }
PictureExplorer.java
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.awt.image.*; import javax.swing.border.*; /** * Displays a picture and lets you explore the picture by displaying the row, column, red, * green, and blue values of the pixel at the cursor when you click a mouse button or * press and hold a mouse button while moving the cursor. It also lets you zoom in or * out. You can also type in a row and column value to see the color at that location. * * Originally created for the Jython Environment for Students (JES). * Modified to work with DrJava by Barbara Ericson * Also modified to show row and columns by Barbara Ericson * */ public class PictureExplorer implements MouseMotionListener, ActionListener, MouseListener {
// current indicies /** row index */ private int rowIndex = 0; /** column index */ private int colIndex = 0; // main GUI /** window to hold GUI */ private JFrame pictureFrame; /** window that allows the user to scroll to see a large picture */ private JScrollPane scrollPane; // GUI components /** column label */ private JLabel colLabel; /** column previous button */ private JButton colPrevButton; /** row previous button */ private JButton rowPrevButton; /** column next button */ private JButton colNextButton; /** row next button */ private JButton rowNextButton; /** row label */ private JLabel rowLabel; /** text field to show column index */ private JTextField colValue; /** text field to show row index */ private JTextField rowValue; /** red value label */ private JLabel rValue; /** green value label */ private JLabel gValue; /** blue value label */ private JLabel bValue; /** color swatch label */ private JLabel colorLabel; /** panel to show the color swatch */ private JPanel colorPanel; // menu components /** menu bar */ private JMenuBar menuBar; /** zoom menu */ private JMenu zoomMenu; /** 25% zoom level */ private JMenuItem twentyFive; /** 50% zoom level */ private JMenuItem fifty; /** 75% zoom level */ private JMenuItem seventyFive; /** 100% zoom level */ private JMenuItem hundred; /** 150% zoom level */ private JMenuItem hundredFifty; /** 200% zoom level */ private JMenuItem twoHundred; /** 500% zoom level */ private JMenuItem fiveHundred; /** The picture being explored */ private DigitalPicture picture; /** The image icon used to display the picture */ private ImageIcon scrollImageIcon; /** The image display */ private ImageDisplay imageDisplay; /** the zoom factor (amount to zoom) */ private double zoomFactor; /** the number system to use, 0 means starting at 0, 1 means starting at 1 */ private int numberBase=0; /** * Public constructor * @param picture the picture to explore */ public PictureExplorer(DigitalPicture picture) { // set the fields this.picture=picture; zoomFactor=1; // create the window and set things up createWindow(); } /** * Changes the number system to start at one */ public void changeToBaseOne() { numberBase=1; } /** * Set the title of the frame *@param title the title to use in the JFrame */ public void setTitle(String title) { pictureFrame.setTitle(title); } /** * Method and initialize the picture frame */ private void createAndInitPictureFrame() { pictureFrame = new JFrame(); // create the JFrame pictureFrame.setResizable(true); // allow the user to resize it pictureFrame.getContentPane().setLayout(new BorderLayout()); // use border layout pictureFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // when close stop pictureFrame.setTitle(picture.getTitle()); PictureExplorerFocusTraversalPolicy newPolicy = new PictureExplorerFocusTraversalPolicy(); pictureFrame.setFocusTraversalPolicy(newPolicy); } /** * Method to create the menu bar, menus, and menu items */ private void setUpMenuBar() { //create menu menuBar = new JMenuBar(); zoomMenu = new JMenu("Zoom"); twentyFive = new JMenuItem("25%"); fifty = new JMenuItem("50%"); seventyFive = new JMenuItem("75%"); hundred = new JMenuItem("100%"); hundred.setEnabled(false); hundredFifty = new JMenuItem("150%"); twoHundred = new JMenuItem("200%"); fiveHundred = new JMenuItem("500%"); // add the action listeners twentyFive.addActionListener(this); fifty.addActionListener(this); seventyFive.addActionListener(this); hundred.addActionListener(this); hundredFifty.addActionListener(this); twoHundred.addActionListener(this); fiveHundred.addActionListener(this); // add the menu items to the menus zoomMenu.add(twentyFive); zoomMenu.add(fifty); zoomMenu.add(seventyFive); zoomMenu.add(hundred); zoomMenu.add(hundredFifty); zoomMenu.add(twoHundred); zoomMenu.add(fiveHundred); menuBar.add(zoomMenu); // set the menu bar to this menu pictureFrame.setJMenuBar(menuBar); } /** * initialize the scrolling image */ private void createAndInitScrollingImage() { scrollPane = new JScrollPane(); BufferedImage bimg = picture.getBufferedImage(); imageDisplay = new ImageDisplay(bimg); imageDisplay.addMouseMotionListener(this); imageDisplay.addMouseListener(this); imageDisplay.setToolTipText("Click a mouse button on a pixel to see the pixel information"); scrollPane.setViewportView(imageDisplay); pictureFrame.getContentPane().add(scrollPane, BorderLayout.CENTER); } /** * Creates the JFrame and sets everything up */ private void createWindow() { // create the picture frame and initialize it createAndInitPictureFrame(); // set up the menu bar setUpMenuBar(); //create the information panel createInfoPanel(); //creates the scrollpane for the picture createAndInitScrollingImage(); // show the picture in the frame at the size it needs to be pictureFrame.pack(); pictureFrame.setVisible(true); } /** * Method to set up the next and previous buttons for the * pixel location information */ private void setUpNextAndPreviousButtons() { // create the image icons for the buttons Icon prevIcon = new ImageIcon(DigitalPicture.class.getResource("leftArrow.gif"), "previous index"); Icon nextIcon = new ImageIcon(DigitalPicture.class.getResource("rightArrow.gif"), "next index"); // create the arrow buttons colPrevButton = new JButton(prevIcon); colNextButton = new JButton(nextIcon); rowPrevButton = new JButton(prevIcon); rowNextButton = new JButton(nextIcon); // set the tool tip text colNextButton.setToolTipText("Click to go to the next column value"); colPrevButton.setToolTipText("Click to go to the previous column value"); rowNextButton.setToolTipText("Click to go to the next row value"); rowPrevButton.setToolTipText("Click to go to the previous row value"); // set the sizes of the buttons int prevWidth = prevIcon.getIconWidth() + 2; int nextWidth = nextIcon.getIconWidth() + 2; int prevHeight = prevIcon.getIconHeight() + 2; int nextHeight = nextIcon.getIconHeight() + 2; Dimension prevDimension = new Dimension(prevWidth,prevHeight); Dimension nextDimension = new Dimension(nextWidth, nextHeight); colPrevButton.setPreferredSize(prevDimension); rowPrevButton.setPreferredSize(prevDimension); colNextButton.setPreferredSize(nextDimension); rowNextButton.setPreferredSize(nextDimension); // handle previous column button press colPrevButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { colIndex--; if (colIndex < 0) colIndex = 0; displayPixelInformation(colIndex,rowIndex); } }); // handle previous row button press rowPrevButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { rowIndex--; if (rowIndex < 0) rowIndex = 0; displayPixelInformation(colIndex,rowIndex); } }); // handle next column button press colNextButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { colIndex++; if (colIndex >= picture.getWidth()) colIndex = picture.getWidth() - 1; displayPixelInformation(colIndex,rowIndex); } }); // handle next row button press rowNextButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { rowIndex++; if (rowIndex >= picture.getHeight()) rowIndex = picture.getHeight() - 1; displayPixelInformation(colIndex,rowIndex); } }); } /** * Create the pixel location panel * @param labelFont the font for the labels * @return the location panel */ public JPanel createLocationPanel(Font labelFont) { // location panel JPanel locationPanel = new JPanel(); locationPanel.setLayout(new FlowLayout()); Box hBox = Box.createHorizontalBox(); // create the labels rowLabel = new JLabel("Row:"); colLabel = new JLabel("Column:"); // create the text fields colValue = new JTextField(Integer.toString(colIndex + numberBase),6); colValue.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { displayPixelInformation(colValue.getText(),rowValue.getText()); } }); rowValue = new JTextField(Integer.toString(rowIndex + numberBase),6); rowValue.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { displayPixelInformation(colValue.getText(),rowValue.getText()); } }); // set up the next and previous buttons setUpNextAndPreviousButtons(); // set up the font for the labels colLabel.setFont(labelFont); rowLabel.setFont(labelFont); colValue.setFont(labelFont); rowValue.setFont(labelFont); // add the items to the vertical box and the box to the panel hBox.add(Box.createHorizontalGlue()); hBox.add(rowLabel); hBox.add(rowPrevButton); hBox.add(rowValue); hBox.add(rowNextButton); hBox.add(Box.createHorizontalStrut(10)); hBox.add(colLabel); hBox.add(colPrevButton); hBox.add(colValue); hBox.add(colNextButton); locationPanel.add(hBox); hBox.add(Box.createHorizontalGlue()); return locationPanel; } /** * Create the color information panel * @param labelFont the font to use for labels * @return the color information panel */ private JPanel createColorInfoPanel(Font labelFont) { // color info panel JPanel colorInfoPanel = new JPanel(); colorInfoPanel.setLayout(new FlowLayout()); // get the pixel at the x and y Pixel pixel = new Pixel(picture,colIndex,rowIndex); // create the labels rValue = new JLabel("R: " + pixel.getRed()); gValue = new JLabel("G: " + pixel.getGreen()); bValue = new JLabel("B: " + pixel.getBlue()); // create the sample color panel and label colorLabel = new JLabel("Color at location: "); colorPanel = new JPanel(); colorPanel.setBorder(new LineBorder(Color.black,1)); // set the color sample to the pixel color colorPanel.setBackground(pixel.getColor()); // set the font rValue.setFont(labelFont); gValue.setFont(labelFont); bValue.setFont(labelFont); colorLabel.setFont(labelFont); colorPanel.setPreferredSize(new Dimension(25,25)); // add items to the color information panel colorInfoPanel.add(rValue); colorInfoPanel.add(gValue); colorInfoPanel.add(bValue); colorInfoPanel.add(colorLabel); colorInfoPanel.add(colorPanel); return colorInfoPanel; } /** * Creates the North JPanel with all the pixel location * and color information */ private void createInfoPanel() { // create the info panel and set the layout JPanel infoPanel = new JPanel(); infoPanel.setLayout(new BorderLayout()); // create the font Font largerFont = new Font(infoPanel.getFont().getName(), infoPanel.getFont().getStyle(),14); // create the pixel location panel JPanel locationPanel = createLocationPanel(largerFont); // create the color information panel JPanel colorInfoPanel = createColorInfoPanel(largerFont); // add the panels to the info panel infoPanel.add(BorderLayout.NORTH,locationPanel); infoPanel.add(BorderLayout.SOUTH,colorInfoPanel); // add the info panel pictureFrame.getContentPane().add(BorderLayout.NORTH,infoPanel); } /** * Method to check that the current position is in the viewing area and if * not scroll to center the current position if possible */ public void checkScroll() { // get the x and y position in pixels int xPos = (int) (colIndex * zoomFactor); int yPos = (int) (rowIndex * zoomFactor); // only if the image is larger than normal if (zoomFactor > 1) { // get the rectangle that defines the current view JViewport viewport = scrollPane.getViewport(); Rectangle rect = viewport.getViewRect(); int rectMinX = (int) rect.getX(); int rectWidth = (int) rect.getWidth(); int rectMaxX = rectMinX + rectWidth - 1; int rectMinY = (int) rect.getY(); int rectHeight = (int) rect.getHeight(); int rectMaxY = rectMinY + rectHeight - 1; // get the maximum possible x and y index int macolIndexX = (int) (picture.getWidth() * zoomFactor) - rectWidth - 1; int macolIndexY = (int) (picture.getHeight() * zoomFactor) - rectHeight - 1; // calculate how to position the current position in the middle of the viewing // area int viewX = xPos - (int) (rectWidth / 2); int viewY = yPos - (int) (rectHeight / 2); // reposition the viewX and viewY if outside allowed values if (viewX < 0) viewX = 0; else if (viewX > macolIndexX) viewX = macolIndexX; if (viewY < 0) viewY = 0; else if (viewY > macolIndexY) viewY = macolIndexY; // move the viewport upper left point viewport.scrollRectToVisible(new Rectangle(viewX,viewY,rectWidth,rectHeight)); } } /** * Zooms in the on picture by scaling the image. * It is extremely memory intensive. * @param factor the amount to zoom by */ public void zoom(double factor) { // save the current zoom factor zoomFactor = factor; // calculate the new width and height and get an image that size int width = (int) (picture.getWidth()*zoomFactor); int height = (int) (picture.getHeight()*zoomFactor); BufferedImage bimg = picture.getBufferedImage(); // set the scroll image icon to the new image imageDisplay.setImage(bimg.getScaledInstance(width, height, Image.SCALE_DEFAULT)); imageDisplay.setCurrentX((int) (colIndex * zoomFactor)); imageDisplay.setCurrentY((int) (rowIndex * zoomFactor)); imageDisplay.revalidate(); checkScroll(); // check if need to reposition scroll } /** * Repaints the image on the scrollpane. */ public void repaint() { pictureFrame.repaint(); } //****************************************// // Event Listeners // //****************************************// /** * Called when the mouse is dragged (button held down and moved) * @param e the mouse event */ public void mouseDragged(MouseEvent e) { displayPixelInformation(e); } /** * Method to check if the given x and y are in the picture * @param column the horizontal value * @param row the vertical value * @return true if the row and column are in the picture * and false otherwise */ private boolean isLocationInPicture(int column, int row) { boolean result = false; // the default is false if (column >= 0 && column < picture.getWidth() && row >= 0 && row < picture.getHeight()) result = true; return result; } /** * Method to display the pixel information from the passed x and y but * also converts x and y from strings * @param xString the x value as a string from the user * @param yString the y value as a string from the user */ public void displayPixelInformation(String xString, String yString) { int x = -1; int y = -1; try { x = Integer.parseInt(xString); x = x - numberBase; y = Integer.parseInt(yString); y = y - numberBase; } catch (Exception ex) { } if (x >= 0 && y >= 0) { displayPixelInformation(x,y); } } /** * Method to display pixel information for the passed x and y * @param pictureX the x value in the picture * @param pictureY the y value in the picture */ private void displayPixelInformation(int pictureX, int pictureY) { // check that this x and y are in range if (isLocationInPicture(pictureX, pictureY)) { // save the current x and y index colIndex = pictureX; rowIndex = pictureY; // get the pixel at the x and y Pixel pixel = new Pixel(picture,colIndex,rowIndex); // set the values based on the pixel colValue.setText(Integer.toString(colIndex + numberBase)); rowValue.setText(Integer.toString(rowIndex + numberBase)); rValue.setText("R: " + pixel.getRed()); gValue.setText("G: " + pixel.getGreen()); bValue.setText("B: " + pixel.getBlue()); colorPanel.setBackground(new Color(pixel.getRed(), pixel.getGreen(), pixel.getBlue())); } else { clearInformation(); } // notify the image display of the current x and y imageDisplay.setCurrentX((int) (colIndex * zoomFactor)); imageDisplay.setCurrentY((int) (rowIndex * zoomFactor)); } /** * Method to display pixel information based on a mouse event * @param e a mouse event */ private void displayPixelInformation(MouseEvent e) { // get the cursor x and y int cursorX = e.getX(); int cursorY = e.getY(); // get the x and y in the original (not scaled image) int pictureX = (int) (cursorX / zoomFactor + numberBase); int pictureY = (int) (cursorY / zoomFactor + numberBase); // display the information for this x and y displayPixelInformation(pictureX,pictureY); } /** * Method to clear the labels and current color and reset the * current index to -1 */ private void clearInformation() { colValue.setText("N/A"); rowValue.setText("N/A"); rValue.setText("R: N/A"); gValue.setText("G: N/A"); bValue.setText("B: N/A"); colorPanel.setBackground(Color.black); colIndex = -1; rowIndex = -1; } /** * Method called when the mouse is moved with no buttons down * @param e the mouse event */ public void mouseMoved(MouseEvent e) {} /** * Method called when the mouse is clicked * @param e the mouse event */ public void mouseClicked(MouseEvent e) { displayPixelInformation(e); } /** * Method called when the mouse button is pushed down * @param e the mouse event */ public void mousePressed(MouseEvent e) { displayPixelInformation(e); } /** * Method called when the mouse button is released * @param e the mouse event */ public void mouseReleased(MouseEvent e) { } /** * Method called when the component is entered (mouse moves over it) * @param e the mouse event */ public void mouseEntered(MouseEvent e) { } /** * Method called when the mouse moves over the component * @param e the mouse event */ public void mouseExited(MouseEvent e) { } /** * Method to enable all menu commands */ private void enableZoomItems() { twentyFive.setEnabled(true); fifty.setEnabled(true); seventyFive.setEnabled(true); hundred.setEnabled(true); hundredFifty.setEnabled(true); twoHundred.setEnabled(true); fiveHundred.setEnabled(true); } /** * Controls the zoom menu bar * * @param a the ActionEvent */ public void actionPerformed(ActionEvent a) { if(a.getActionCommand().equals("Update")) { this.repaint(); } if(a.getActionCommand().equals("25%")) { this.zoom(.25); enableZoomItems(); twentyFive.setEnabled(false); } if(a.getActionCommand().equals("50%")) { this.zoom(.50); enableZoomItems(); fifty.setEnabled(false); } if(a.getActionCommand().equals("75%")) { this.zoom(.75); enableZoomItems(); seventyFive.setEnabled(false); } if(a.getActionCommand().equals("100%")) { this.zoom(1.0); enableZoomItems(); hundred.setEnabled(false); } if(a.getActionCommand().equals("150%")) { this.zoom(1.5); enableZoomItems(); hundredFifty.setEnabled(false); } if(a.getActionCommand().equals("200%")) { this.zoom(2.0); enableZoomItems(); twoHundred.setEnabled(false); } if(a.getActionCommand().equals("500%")) { this.zoom(5.0); enableZoomItems(); fiveHundred.setEnabled(false); } } /** * Class for establishing the focus for the textfields */ private class PictureExplorerFocusTraversalPolicy extends FocusTraversalPolicy { /** * Method to get the next component for focus */ public Component getComponentAfter(Container focusCycleRoot, Component aComponent) { if (aComponent.equals(colValue)) return rowValue; else return colValue; } /** * Method to get the previous component for focus */ public Component getComponentBefore(Container focusCycleRoot, Component aComponent) { if (aComponent.equals(colValue)) return rowValue; else return colValue; } public Component getDefaultComponent(Container focusCycleRoot) { return colValue; } public Component getLastComponent(Container focusCycleRoot) { return rowValue; } public Component getFirstComponent(Container focusCycleRoot) { return colValue; } } /** * Test Main. It will explore the beach */ public static void main( String args[]) { Picture pix = new Picture("beach.jpg"); pix.explore(); } }
import javax.swing.JFileChooser; import javax.swing.JFrame; import java.util.Properties; import java.io.*; import java.net.*; /** * A class to make working with a file chooser easier * for students. It uses a JFileChooser to let the user * pick a file and returns the chosen file name. * */ public class FileChooser { /////////////////////// methods ///////////////////////////// /** * Method to get the full path for the passed file name * @param fileName the name of a file * @return the full path for the file */ public static String getMediaPath(String fileName) { String path = null; String directory = getMediaDirectory(); boolean done = true; // get the full path path = directory + fileName; return path; } /** * Method to pick an item using the file chooser * @param fileChooser the file Chooser to use * @return the path name */ public static String pickPath(JFileChooser fileChooser) { String path = null; /* create a JFrame to be the parent of the file * chooser open dialog if you don't do this then * you may not see the dialog. */ JFrame frame = new JFrame(); frame.setAlwaysOnTop(true); // get the return value from choosing a file int returnVal = fileChooser.showOpenDialog(frame); // if the return value says the user picked a file if (returnVal == JFileChooser.APPROVE_OPTION) path = fileChooser.getSelectedFile().getPath(); return path; } /** * Method to let the user pick a file and return * the full file name as a string. If the user didn't * pick a file then the file name will be null. * @return the full file name of the picked file or null */ public static String pickAFile() { JFileChooser fileChooser = null; // start off the file name as null String fileName = null; // get the current media directory String mediaDir = getMediaDirectory(); /* create a file for this and check that the directory exists * and if it does set the file chooser to use it */ try { File file = new File(mediaDir); if (file.exists()) fileChooser = new JFileChooser(file); } catch (Exception ex) { } // if no file chooser yet create one if (fileChooser == null) fileChooser = new JFileChooser(); // pick the file fileName = pickPath(fileChooser); return fileName; } /** * Method to get the directory for the media * @return the media directory */ public static String getMediaDirectory() { String directory = null; boolean done = false; File dirFile = null; // try to find the images directory try { // get the URL for where we loaded this class Class currClass = Class.forName("FileChooser"); URL classURL = currClass.getResource("FileChooser.class"); URL fileURL = new URL(classURL,"../images/"); directory = fileURL.getPath(); directory = URLDecoder.decode(directory, "UTF-8"); dirFile = new File(directory); if (dirFile.exists()) { //setMediaPath(directory); return directory; } } catch (Exception ex) { } return directory; } } import java.awt.Image; import java.awt.image.BufferedImage;
/** * Interface to describe a digital picture. A digital picture can have an * associated file name. It can have a title. It has pixels * associated with it and you can get and set the pixels. You * can get an Image from a picture or a BufferedImage. You can load * it from a file name or image. You can show a picture. You can * explore a picture. You can create a new image for it. */ public interface DigitalPicture { public String getFileName(); // get the file name that the picture came from public String getTitle(); // get the title of the picture public void setTitle(String title); // set the title of the picture public int getWidth(); // get the width of the picture in pixels public int getHeight(); // get the height of the picture in pixels public Image getImage(); // get the image from the picture public BufferedImage getBufferedImage(); // get the buffered image public int getBasicPixel(int x, int y); // get the pixel information as an int public void setBasicPixel(int x, int y, int rgb); // set the pixel information public Pixel getPixel(int x, int y); // get the pixel information as an object public Pixel[] getPixels(); // get all pixels in row-major order public Pixel[][] getPixels2D(); // get 2-D array of pixels in row-major order public void load(Image image); // load the image into the picture public boolean load(String fileName); // load the picture from a file public void show(); // show the picture public void explore(); // explore the picture public boolean write(String fileName); // write out a file }
import javax.swing.JColorChooser; import javax.swing.JFrame; import java.awt.Color;
/** * A class to make working with a color chooser easier for students. It uses a * JColorChooser to let the user pick a color and returns the chosen color * object. * */ public class ColorChooser {
/** * Method to let the user pick a color and return the color object. * * @return the picked color or red if no color was picked */ public static Color pickAColor() { Color color = Color.white;
// create a JFrame to be the parent of the color chooser open dialog // if you don't do this then you may not see the dialog. JFrame frame = new JFrame(); frame.setAlwaysOnTop(true);
// use the color chooser to pick the color color = JColorChooser.showDialog(frame, "Pick a color", color);
return color; }
/** * Main method for testing the ColorChooser */ public static void main(String[] args) { Color pickedColor = ColorChooser.pickAColor(); System.out.println(pickedColor); }
}
Hello could you please help me with this assignment code.