Page 1 of 3 123 LastLast
Results 1 to 20 of 46
Like Tree4Likes

Thread: Really stuck need help

  1. #1
    B_Tank88 is offline Member
    Join Date
    Jul 2011
    Posts
    22
    Rep Power
    0

    Default Really stuck need help

    I have a task to complete within a couple of days, and I'm really struggling. I have been working on it since this afternoon but with my lack of experience in Java I'm really struggling in trying to grasp is many class structure.

    I have to create an interface which simulates a television remote, use up and down to flick between 10 channels, use left and right buttons to change volume level and display this change for 10 seconds.

    The channels should just show a background picture, that changes when the channel is changed.

    I have stored 10 picture links in an strArray, and I can manage to get a single index displayed. I am totally clueless on how to incorporate that into a dynamic array that can be changed with JButtons. I have experimented with the ActionListener but I can not manage to change the array counter within the ActionListener function.

    This is what I have so far:

    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.*;
    import java.io.ObjectInputStream.GetField;
    
    import javax.imageio.*;
    import javax.swing.*;
    import java.net.*;
    
    public class Automotest {
    	
    	
    	public static void main (String [] args) {
    		
    	
    		// Declare and initialise images array, and single image string.
    		String strArray[] = {"http://thepublicblog.net/images/bbc1logo.jpg",
    				"http://www.nomogen.co.uk/images/java_get_powered.gif",
    				"http://thenextweb.com/uk/files/2010/08/itv-logo-665984612.jpg",
    				"http://ctva.biz/UK/Yorkshire/C4_logo_original.jpg",
    				"http://4.bp.blogspot.com/-SJoSDJyd5ks/Te3Bw_lhATI/AAAAAAAAEek/AKoOcJ9f6n8/s1600/five.png",
    				"http://1.bp.blogspot.com/_AkrZ09GYiws/TE6zaVhjlyI/AAAAAAAASSM/brY7RHd-iFw/s1600/bbc3logo.png",
    				"http://www.bbc.co.uk/pressoffice/pressreleases/stories/2010/08_august/25/assets/gfx/forEMP/448_bbc4.jpg",
    				"http://www.theprisma.co.uk/wp-content/uploads/2010/08/itv-news-300x300.jpg",
    				"http://images1.wikia.nocookie.net/__cb20060913110621/uncyclopedia/images/2/23/Crappy_itv_logo.PNG", 
    				"http://hub.tv-ark.org.uk/images/itvyorkshire/yorkshire_images/idents/yorkshireITV_ident1989a.jpg", 
    				"http://www.sub-tv.co.uk/pics/ITVSchools/ITV80sClock.jpg"
    				};
    
    		Image my_jpg = null;
    		int arrayCount = 0;
    		boolean quit = true;
    		
    		// Create new JInterface
    		JFrame frame = new JFrame("Simulation");
    		JFrame frame2 = new JFrame("Remote");
    		
    		try {
    			my_jpg = ImageIO.read(new URL(strArray[0]));
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    		frame.setVisible(true);
    		frame2.setVisible(true);
    		
    		frame.setSize(1024, 768);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame2.setSize(300, 100);
    		frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		// Add buttons		
    		JButton upButton = new JButton("Up");
    		JButton downButton = new JButton("Down");
    		JButton leftButton = new JButton("Left");
    		JButton rightButton = new JButton("Right");
    		
    		JLabel label = new JLabel("tvSimulation");
    		JLabel label2 = new JLabel("Remote");
    		JPanel panel = new JPanel();
    		JPanel panel2 = new JPanel();
    		
    		frame.add(panel);
    		frame2.add(panel2);
    		
    		panel2.add(upButton);
    		panel2.add(downButton);
    		panel2.add(leftButton);
    		panel2.add(rightButton);
    		
    		panel.add(label);
    		panel2.add(label2);
    		
    		
    		label = new JLabel(new ImageIcon(my_jpg));
    		frame.getContentPane().add(label, BorderLayout.CENTER);
    
    						
    		}
    		
    	}

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    For actions to occur when a button is pressed, you need to add action listeners to the buttons.
    Go to this site and search for Action Listener for a tutorial on how to write listeners.
    The Really Big Index

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Are you getting the pictures to display at all? If so, you could just keep track of the current channel, and index into the next array slot when you increase the channel. Then sat the background of that current index.

    Java Code:
    currentChannel++;
    currentImage = array[currentChannel];
    This is the basic idea, and to go down a channel it will obviously be similar but in the opposite direction.

    Keep in mind that arrays have 0-based indexing.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Your main() method is already too long. To avoid getting into a tangle you could think about the general structure of your application and have a clear plan before you write even the first line of code.

    I think I would want to have two classes: a TV which had public setter methods to change the channel and volume, and a Remote that would call these methods. Both would probably be panels rather than frames.

    The TV would respond to calls to its setter methods as described above by setting its notion of the "current image". It's painting behaviour would be to paint the current image. Eventually it would use a timer to implement a 10s onscreen display of changes.

    The remote would have buttons with action listeners (also as described above). The constructor of the remote could be passed the tv as an argument so that the action listeners would know which methods to call. This suggests starting with the TV class.

    The advantage of breaking things up this way is that you would have two separate classes that could be written and tested somewhat independently of each other. The way in which frames are created to hold the panels is well described in the tutorial.
    Norm likes this.

  5. #5
    B_Tank88 is offline Member
    Join Date
    Jul 2011
    Posts
    22
    Rep Power
    0

    Default

    Thanks for the replies.

    I have taken the advice into account, attempted to draw the design out.

    I have this nagging problem now though, I have imported the 10 jpg files I need to display, but when initialising the ImageIcon I can only get the image to output when I specify the full filepath.

    image = new ImageIcon("C2.jpg"); // Does not work
    image = new ImageIcon("C:\Users\Bhavesh\workspace\TVRemote\bin \C2.jpg"); // Works

    The program needs to run on another PC in the future so the working example is no good. I was under the impression simply specifying the name of the file should work as long as the files reside within the package, my files are present in \bin.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    If you are going to run your code on another PC you will want to put it and its images in a jar file. Learning how to do that adds another thing to learn. It involves getting inputstreams and using another class to load the image.

    If the image files are in the same directory as the class files and your class is not in a package, then you should be able to read the images without the full path. If your class is in a package then you need to consider that in the path.

    Here's sample code from a program that works for me. The images directory is in the same folder as the class file:
    Java Code:
       ImageIcon icon1 = new ImageIcon("images / middle.gif");
    Ignore spaces around the /
    Last edited by Norm; 07-12-2011 at 04:51 PM.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Have you tried something like "./file.jpg", or storing the file in a different directory and using "../dir/file.jpg", obviously for this to work the jpg has to be back one directory, then in a new folder.

    If you have a resource file at
    Java Code:
    C:/java/myproject/resources
    And then the source is in
    Java Code:
    C:/java/myproject/src
    This should work.

  8. #8
    B_Tank88 is offline Member
    Join Date
    Jul 2011
    Posts
    22
    Rep Power
    0

    Default

    image = new ImageIcon(getClass().getResource("C2.jpg"));

    I am now using getClass and getResource then appending the filename at the end. This works.

    I now need to include interaction which changes the pictures. I was thinking two buttons with action listeners which will use string arrays which store the filenames, using the appropriate index as the argument when calling the painting function.

    Does that sound reasonable?

    I am hoping I can look at jar files at another time to get the program working on another pc.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    Instead of an array of paths to the images, you might have an array of images that you preload.

  10. #10
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    I agree with pbrockway2 on this one. On a program of this scope I would at max have only 2-4 lines in main(). Create many public classes that main() will call, in order to test and debug each of the classes by themselves. Also, instead of an array[] of paths to file names, put every image in an array instead. Makes it easy to debug because Java pathway structure is very cluttered (in my opinion) what with the backslashes and un-interoperability with the various file systems in different OSs.

  11. #11
    B_Tank88 is offline Member
    Join Date
    Jul 2011
    Posts
    22
    Rep Power
    0

    Default

    import javax.swing.*;

    import java.awt.*;


    public class Paint extends JPanel {

    private ImageIcon image;
    private ImageIcon imgArray[];

    public void PopulateImg() {

    boolean imgArrayPop = false;

    // Populate imgArray
    // (imgArrayPop = false) {

    System.out.println("Populating array");
    imgArray[0] = new ImageIcon(getClass().getResource("C1.jpg"));
    imgArray[1] = new ImageIcon(getClass().getResource("C2.jpg"));
    imgArray[2] = new ImageIcon(getClass().getResource("C3.jpg"));
    imgArray[3] = new ImageIcon(getClass().getResource("C4.jpg"));
    imgArray[4] = new ImageIcon(getClass().getResource("C5.jpg"));
    imgArray[5] = new ImageIcon(getClass().getResource("C6.jpg"));
    imgArray[6] = new ImageIcon(getClass().getResource("C7.jpg"));
    imgArray[7] = new ImageIcon(getClass().getResource("C8.jpg"));
    imgArray[8] = new ImageIcon(getClass().getResource("C9.jpg"));
    imgArray[9] = new ImageIcon(getClass().getResource("C10.jpg"));

    imgArrayPop = true;
    //}

    }
    public void paintComponent(Graphics g) {

    System.out.println("Painting image");
    image = new ImageIcon(getClass().getResource("C1.jpg"));
    super.paintComponent(g);
    image.paintIcon(this, g, 0, 0);

    }

    }



    I am getting a null pointer exception when entering the PopulateImg method, it cant seem to assign the ImageIcon to the array index, it fails on the first one. I dont understand because this statement:

    image = new ImageIcon(getClass().getResource("C1.jpg"));

    executes fine, and displays the image as intended. I dont see how that is different from the array population. Both 'image' and 'imgArray' are ImageIcon type.
    Last edited by B_Tank88; 07-12-2011 at 06:17 PM.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    There are two steps in defining and using an array.
    First you define it with the number of elements you want in it. int[] intAr = new int[10];
    Then you fill those slots with the values you want at each element. intAr[0] = 123;

    You have not defined the array, so it is still null.

  13. #13
    B_Tank88 is offline Member
    Join Date
    Jul 2011
    Posts
    22
    Rep Power
    0

    Default

    Many thanks, as you can see I come from a C++ background

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    Java requires using new. Object references and arrays are really pointers, so you always need a new to give it a value.

  15. #15
    B_Tank88 is offline Member
    Join Date
    Jul 2011
    Posts
    22
    Rep Power
    0

    Default

    Thanks for the help, I feel like I'm getting somewhere now.

    After the array population, I call the Remote class which returns the currentchannel. The currentchannel is retrieved after this call, and is then put into the return of the array population function. So now, the paintComponent makes the call to the array population function and retrives the appropriate channel, together with the channels ImageIcon, and it displays!

    I have a small niggle though, I only want to populate the arrays once, obviously. I have an if which uses a false boolean, and that boolean is set to true after the population so that it doesnt do it again. The statement doesnt work though, execution never hits it even though its set to run when boolean is false, and the boolean is also initialised to false. What's wrong here?

    import javax.swing.*;

    import java.awt.*;


    public class Paint extends JPanel {

    private ImageIcon image;
    private ImageIcon imgArray[] = new ImageIcon[10];
    boolean imgArrayPop = false;


    public ImageIcon PopulateImg() {

    // Populate imgArray
    if (imgArrayPop = false) {

    System.out.println("Populating array");
    imgArray[0] = new ImageIcon(getClass().getResource("C1.jpg"));
    imgArray[1] = new ImageIcon(getClass().getResource("C2.jpg"));
    imgArray[2] = new ImageIcon(getClass().getResource("C3.jpg"));
    imgArray[3] = new ImageIcon(getClass().getResource("C4.jpg"));
    imgArray[4] = new ImageIcon(getClass().getResource("C5.jpg"));
    imgArray[5] = new ImageIcon(getClass().getResource("C6.jpg"));
    imgArray[6] = new ImageIcon(getClass().getResource("C7.jpg"));
    imgArray[7] = new ImageIcon(getClass().getResource("C8.jpg"));
    imgArray[8] = new ImageIcon(getClass().getResource("C9.jpg"));
    imgArray[9] = new ImageIcon(getClass().getResource("C10.jpg"));
    imgArrayPop = true;

    }

    // Call Remote class to retrieve current channel, and set this as the return value
    // together with appropriate channels ImageIcon.
    Remote remoteObj = new Remote();
    return imgArray[remoteObj.SetChannel()];

    }
    public void paintComponent(Graphics g) {


    Paint paintObj = new Paint();
    // Recieve return from PopulateImg() and display it.
    image = paintObj.PopulateImg();

    super.paintComponent(g);
    image.paintIcon(this, g, 0, 0);

    }

    }

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    Please wrap your posted code in code tags to preserve formatting. Use the # icon above the input area or See: BB Code List - Java Forums

    Why do you call the PopulateImg method more than one time at initialization? Change the logic to populate once. Have another method that gets an image.

    BTW naming conventions for variable and method names use lowercase first letter. Classnames use uppercase.
    PopulateImg looks like a class name.

  17. #17
    B_Tank88 is offline Member
    Join Date
    Jul 2011
    Posts
    22
    Rep Power
    0

    Default

    After doing some rearranging to a couple of classes I have followed execution from start to finish, and each method is only called once, as desired. I have been following processing using system.outs and everything looks good now.

    Changed the method names as suggested. I guess its now time to work on the remote class and action listeners.

    I was thinking the action listeners would assign the next channel number 'upon action', and then would call the paintComponent method, which should get the new channel number by calling setChannel() and do the painting again, with the new image.

    Do I have to do anything to the current painted image before overwriting it? - Seeing as my JFrame requires an EXIT_ON_CLOSE, I don't want to lose out resources this way when painting a new picture.

    I still have a problem on my if statement too, processing just doesnt enter it so I have to comment it out when testing.

    Java Code:
    import javax.swing.*;
    
    import java.awt.*;
    
    
    public class Paint extends JPanel {
    	
    	private ImageIcon image;	
    	private ImageIcon imgArray[] = new ImageIcon[10];
    	boolean imgArrayPop = false;
    		
    	public ImageIcon populateImg() {
    		
    		// Populate imgArray
    		if (imgArrayPop = false) { // The if statement doesn't work?
    			
    			System.out.println("Entered populateImg");
    			imgArray[0] = new ImageIcon(getClass().getResource("C1.jpg"));
    			imgArray[1] = new ImageIcon(getClass().getResource("C2.jpg"));
    			imgArray[2] = new ImageIcon(getClass().getResource("C3.jpg"));
    			imgArray[3] = new ImageIcon(getClass().getResource("C4.jpg"));
    			imgArray[4] = new ImageIcon(getClass().getResource("C5.jpg"));
    			imgArray[5] = new ImageIcon(getClass().getResource("C6.jpg"));
    			imgArray[6] = new ImageIcon(getClass().getResource("C7.jpg"));
    			imgArray[7] = new ImageIcon(getClass().getResource("C8.jpg"));
    			imgArray[8] = new ImageIcon(getClass().getResource("C9.jpg"));
    			imgArray[9] = new ImageIcon(getClass().getResource("C10.jpg"));
    			imgArrayPop = true;	// This should only let this population occur once.
    
    		}
    		
    		// Call Remote class to retrieve current channel, and set this as the return value
    		// together with appropriate channels ImageIcon.
    			Remote remoteObj = new Remote();	
    			return imgArray[remoteObj.setChannel()];	
    		
    	}
    	public void paintComponent(Graphics g) {
    		 
    		System.out.println("Entered paintComponent");
    		Paint paintObj = new Paint();
    		
    		// Receive return from PopulateImg() and display it.
    		image = paintObj.populateImg();
    		
    		System.out.println("Painting Image");
    		super.paintComponent(g);
    		image.paintIcon(this, g, 0, 0);
    		
    		}
    		
    }

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    then would call the paintComponent method
    When you want the paintComponent method called, you call repaint() which tells the JVM to schedule a call to the paintComponent method soon.
    Do I have to do anything to the current painted image
    No, it will be cleared when you call super.paintComponent()

    Java Code:
                     Paint paintObj = new Paint();
    		
    		// Receive return from PopulateImg() and display it.
    		image = paintObj.populateImg();
    You don't what to create a new object on every call to paintComponent. Move the creation of the object out of that method.

    Java Code:
    if (imgArrayPop = false)
    An assignment statement uses =
    a compare uses ==
    Last edited by Norm; 07-12-2011 at 08:26 PM.

  19. #19
    B_Tank88 is offline Member
    Join Date
    Jul 2011
    Posts
    22
    Rep Power
    0

    Default

    I'm not really sure how to use repaint().. what does this method actually do? I had a look on the net but I cant really figure it out.

    I seem to be nearly there for the image changing, the action listener responds, the correct channel is being returned but I cant get the repaint to work. Do I have to target an object for the repaint? At the moment I am just repainting my paint class.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    repaint().. what does this method actually do
    It tells the JVM to schedule a call to your component's paint methods

    but I cant get the repaint to work
    Please post your code that calls repaint.

Page 1 of 3 123 LastLast

Similar Threads

  1. I'm stuck help!!!
    By nobody58 in forum Advanced Java
    Replies: 2
    Last Post: 03-18-2010, 02:52 PM
  2. Stuck in sea
    By programmer_007 in forum JDBC
    Replies: 1
    Last Post: 09-17-2009, 04:00 AM
  3. Im on my last lab!!!! And im stuck...:(
    By clanboru15 in forum New To Java
    Replies: 5
    Last Post: 03-13-2009, 01:44 AM
  4. really stuck now..
    By shongo in forum Advanced Java
    Replies: 2
    Last Post: 11-09-2008, 02:56 AM
  5. Stuck in need of help!
    By Zombie_Leg! in forum New To Java
    Replies: 1
    Last Post: 09-23-2008, 02:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •