Ok so this code works great with an if else statement but i dont why when i change the showMenu to a switch statement you cannot see my print out statement as a menu!?
Iv tried different ways of writing the print outs and they havent worked at all.
Also my public void forward statement keeps going up to 9 then wraps back to one when it should only stop at 8 then wrap back to one. is my logic wrong? the public int forward works fine and goes only up to 8 and wraps back to 1
thanks
christine
_____________________________________________________________
import java.util.Random;
import java.util.Scanner;
import javax.swing.JFrame;
import java.awt.*;
import java.net.*;
import javax.swing.JPanel;
public class PictureViewer {
int min = 1;
int max = 8;
int current_image= 1;
Image theimage;
public int forward ( int num ) { //this will be passed current_image to work with
if (num>=8){
num = 1;
}//these methods are the same logic, just no GB
else {
num++;
}
return num;
}
public int backward ( int num ) { //this will be passed current_image to work with
if (num>1){
num--;
}
else{
num = 1;
}
return num;
}
public void forward ( ) {
if (current_image>max){
current_image = 1;//wrap back aroudn to 1, when method is called again, 1 will increment
}
else { current_image++;
}
}
public void backward ( ) {
if (current_image>min){
current_image--;
}
else {
current_image = min; //this is where the decrement will stop because current image is not > 1. current image = 1
}
}
public String createFileName ( int current_img ) {
String current_imgString = new Integer(current_img).toString();
current_imgString = "picture" + current_img + ".gif";
return current_imgString;
}
public String createRandomName () {
Random generator = new Random();
int num= generator.nextInt(max) + 1;
String current_imgString = new Integer(num).toString();
current_imgString = "picture" + num + ".gif";
return current_imgString;
}
public void showMenu(){
Scanner input = new Scanner(System.in);
int x ;
do{
x = input.nextInt();
switch (x) {
case 1:System.out.println("Forward");
current_image = forward(current_image);
break;
case 2:System.out.println("Backward");
current_image = backward(current_image);
break;
case 3:System.out.println("Forward Global");
forward();
break;
case 4:System.out.println("Backward Global");
backward();
break;
case 5:System.out.println("Create File Name");
createFileName(current_image);
break;
case 6:System.out.println("Create Random");
createRandomName();
break;
case 7:System.out.println("Get Any Image");
getAnyImage();
break;
case 8:System.out.println("Exit");
break;
}
}while (x >=1 && x <=8);
}
public void showWindow( String filename ) {
JPanel panel = new MyPanel();
JFrame frame1 = new JFrame();
frame1.add(panel);
theimage = load_picture(filename, frame1);
frame1.setTitle(filename);
frame1.setSize(500, 500);
frame1.setLocation(200, 150);
frame1.setVisible(true);}
public class MyPanel extends JPanel {
public void paintComponent (Graphics g) {
int xpos,ypos;
super.paintComponent(g);
// set the xpos and ypos before you display the image
xpos = 10; // you pick the position
ypos = 10; // you pick the position
if (theimage != null) {
g.drawImage(theimage,xpos,ypos,this);
// note: theimage global variable must be set BEFORE paint is called
}
}
}
public Image load_picture(String imagefile, JFrame theframe)
{
Image tempimage;
MediaTracker tracker;
tracker = new MediaTracker(theframe);
String startURL;
if (imagefile.startsWith("http"))
startURL = "";
else
startURL = "http://www.canyons.edu/departments/comp_sci/ferguson/cs111/images/";
URL myURL=null;
try
{
myURL = new URL(startURL + imagefile);
}
catch(MalformedURLException e) {
System.out.println("Error caught " + e.toString());
}
//tempimage = getImage(myURL); // JApplet version
tempimage = Toolkit.getDefaultToolkit().getImage(myURL); // stand alone program version
// Add the image to the MediaTracker so that we can wait for it
tracker.addImage(tempimage, 0);
try { tracker.waitForID(0); }
catch ( InterruptedException err) { System.err.println(err); }
return tempimage;
}
public String getAnyImage() {
String picture ;
Scanner input = new Scanner (System.in);
System.out.println("Enter the URL");
picture=input.next();
return picture;
}
public static void main(String[] args) {
PictureViewer object = new PictureViewer ();
object.showMenu();
}
}