Menu

[solved]-Code Picture Viewer Public Class Pictureviewer Final Static Int Minnumber 1 Final Static I Q39030277

Here is the code for the picture viewer..

public class PictureViewer {
  
final static int MIN_NUMBER = 1;
final static int MAX_NUMBER = 8;
static int image_number = 1;
  
   // Add your methods here

public static void showWindow(String filename) {
JPanel myPanel = new JPanel();
JFrame pictureFrame = new JFrame();
pictureFrame.setTitle(filename);
pictureFrame.setSize(800, 600);
pictureFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myPanel.add(load_picture(filename));
pictureFrame.add(myPanel);
pictureFrame.setVisible(true);
}
public static JLabel load_picture(String imagefile) {
JLabel templabel = null;
String startURL = “”;
if(!imagefile.startsWith(“http”))
startURL = “http://riveira.x10host.com/CMPSCI111L/images/”;
URL myURL = null;
try {
myURL = new URL(startURL + imagefile);
BufferedImage myPicture = ImageIO.read(myURL);
templabel = new JLabel(new ImageIcon(myPicture));
}
catch(Exception e) {
System.out.println(“Error caught ” + e.toString());
}
return templabel;
}
}

Phase 1 – Designing Your Methods
You must design 3 algorithms (pseudo code) for thealgorithms.
Here is what each algorithm should describe:
Given an integer parameter named current_number and two symbolicconstants:
final static int MIN_NUMBER = 1;
final static int MAX_NUMBER = 8;
Create an algorithm named forward() which will advance one valuethrough a sequence of numbers 1, 2, 3… up to MAX_NUMBER. In otherwords, when forward() is passed a value of 3 in the parametercurrent_number, it simply returns a 4.
However, when MAX_NUMBER is reached, the algorithm should “wraparound” and return MIN_NUMBER. The algorithm must never return avalue larger MAX_NUMBER. This means that the algorithm should checkto see if (current_number + 1) is less than or equal to MAX_NUMBERand if this is true, the algorithm should return the value of(current_number + 1). Otherwise, the algorithm should returnMIN_NUMBER.
Create an algorithm named backward(), which will move through asequence of numbers… 5, 4, 3, 2, MIN_NUMBER. In other words, whenbackward() is passed a value of 6 in the parameter current_number,it simply returns a 5.
When MIN_NUMBER is reached the algorithm should STOP and return thevalue MIN_NUMBER. This algorithm will NEVER “wrap around”. Thismeans that the algorithm should check to see if (current_number -1) is greater than or equal to MIN_NUMBER and if this is true, thealgorithm should return the value of (current_number – 1).Otherwise the algorithm should return MIN_NUMBER.
Create an algorithm named createFileName that builds and returns aString like “pictureX.jpg”, where X is the value in the inputparameter. In other words, when
createFileName() is passed a value of 4 in the current_numberparameter, it simply returns the String “picture4.jpg”.
All of this should fit on 1 sheet of paper. Place the 3 flowcharts(one per method) on one side of the paper and the matching pseudocode next to the flowcharts on the other side of the paper.
Be sure to have me check and approve your algorithms beforeproceeding to Phase 2.

Phase 2 – Implementing Your Algorithms
In this phase, you will implement your algorithms by adding sixmethods to the attached PictureViewer class. Do not modify theexisting code in the class, simply add your methods to it. Yourmethods should behave as described in your Phase 1 algorithms. Theforward method should “wrap around” when it reaches the lastnumber, the backward method should stop when it reaches the firstnumber.
The forward() and backward() methods must use an input argument andoutput a return value. they must not use the global variableimage_number directly. Also, both methods should use the constantsMIN_NUMBER and MAX_NUMBER instead of hard coded values (1 or 8).The methods are very simple, when the current image number is 3,forward() changes it to a 4 and returns that 4. When the currentimage number is 6, backward() changes it to a 5 and returns that5.
The method createFileName() will take the current image number asinput, and will return a String containing a filename like“pictureX.jpg”, where X is the current image number.
The method createRandomName() has no input, and returns a Stringcontaining a filename like “pictureX.jpg”, where X is a randomnumber between MIN_NUMBER and MAX_NUMBER, including MIN_NUMBER andMAX_NUMBER.
Use the following method headers:
public static int forward(int current_number){
// return the new image number
}
public static int backward(int current_number){
// return the new image number
}
// The static methods above should use the constants
// MIN_NUMBER, MAX_NUMBER. Do not use a hard coded 1 or 8
public static String createFileName(int current_number){
// return a filename like pictureX.jpg
}
public static String createRandomName(){
// return a filename like picture.jpg
// using a RANDOM number between MIN_NUMBER and MAX_NUMBER
}
public static void showMenu(){
// write a loop
/*
1. Inside the loop, display a menu with options
1…N for each of the methods you are
implementing, as well as an exit option.
2. Print the image_number.
3. Prompt the user for a menu option choice.
4. Get the user’s menu option choice, then invoke
then the correct method using an if-statement.
For example, if your menu shows “1. Forward”, and
user’s menu option choice is 1, the if-statement
should then invoke the forward() method.
*/
}
public static void main(String[] args){
// invoke showMenu()
}
The menu should have options for calling forward(), backward(),createFileName() and createRandomName(). Be sure to use an argumentwhen invoking the forward(), backward() and createFileName()methods, then update the global image_number variable with valuereturned by either forward() or backward() methods. Also, when theexit option is chosen in showMenu(), use System.exit(0); toterminate the program. At this point, your program will show themenu, invoke the methods and print the image_number onto theconsole.

Phase 3 – Adding Overloaded Methods
This phase will require you to write two overloaded methods. Anoverloaded method is one that has the same name as another method.We can create a method with the same name as another, as long as ithas a different list of parameters. The difference must be ineither the number of parameters or in the data types of thoseparameters. Add the following overloaded methods to thePictureViewer class:
public static void forward(){
// Overloaded method. This method is allowed to
// use the global variable image_number for
// input and output. In other words, the method
// should check if image_number + 1 is less than
// or equal to MAX_NUMBER. If it is, then the
// metho sets image_number to image_number + 1.
// Otherwise, the method sets image_number to
// MIN_NUMBER.
}
public static void backward(){
// Overloaded method. This method is allowed to
// use the global variable image_number for
// input and output. In other words, the method
// should check if image_number – 1 is greater
// than or equal to MIN_NUMBER. If it is, then
// the method sets image_number to image_number – 1.
// Othecrwise, the method sets image_number to
// MIN_NUMBER.
}
In addition to having different parameters (no parameter at allversus one parameter), these new methods will operate differently.Instead of using a parameter and return data type for input andoutput, these new methods will be allowed to use the image_numberglobal variable for input and output.
Add two new options to your mentu to invoke these new overloadedmethods. Both versions of forward() and backward() do basically thesame thing as the previous forward() and backward() methods, butthese overloaded versions do not require a parameter. Test yourprogram. Make sure you can move forward using both overloadedforward() methods and backward using both overloaded backward()methods. Do not continue to Phase 4 until you are sure the programworks.

Phase 4 – Showing Images
Add some code to invoke the existing showWindow() method, anddisplay the image file names the Phase 2 and 3 methodscreate.
In your showMenu() method’s if-statement, where you invoke thecreateFileName() method, add a statement to invoke showWindow()then pass showWindow() the file name returned by createFileName().Where you invoke the createRandomName() method, add a statement toinvoke showWindow() then pass showWindow() the file name returnedby the createRandomName() method.
Test your program. You should be able to go forward and backwardwith proper behavior when limits are reached. When eithercreateFileName() or createRandomName() methods are invoked, a framcontaining an image should appear onscreen (you may need to click abutton on the taskbar to see it). The current file name should beshown at the top of the frame. In other words, if thecreateFileName() method is invoked when image_number is 7, a framwill open and the fram should show a picture and the title bar ofthe fram should show “picture7.jpg”. If the createRandomName()method is invoked, a frame will open and the frame should show apicture and the title bar of the frame should show a random filename between and including “picture1.jpg” to “picture8.jpg”. All 8pictures in this project are different from one another.
Remember to write a Universal comment header at the top of yoursource code file.

I have been stuck on this assignment for a while, any help wouldbe greatly appreciated.

Expert Answer


Answer to Here is the code for the picture viewer.. public class PictureViewer { final static int MIN_NUMBER = 1; final static int… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *