Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 46
Like Tree4Likes

Thread: Really stuck need help

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

    Default

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    
    public class Channel {
    	
    	public int currentChannel = 0;
    	
    	public int setChannel() {
    		
    		System.out.println("Entered setChannel");
    		return currentChannel;
    		
    	}
    	
    	public class UpAction implements ActionListener {
    		
    		Paint paintObj = new Paint();
    		
    		public void actionPerformed (ActionEvent e) {
    			
    			System.out.println("Entered actionPerformed");
    			System.out.println(currentChannel);
    			currentChannel++;
    			System.out.println(currentChannel);
    			paintObj.repaint();
    		}
    		
    	}
    
    }
    Execution gets to the final system.out here, setting the currentChannel to +1. A successful call of the paint function should do whats intended, but it seems to just pause when it gets to the repaint. CPU usage is ok at this stage too, nothing seems to be happening. paintObj points to my paint class containing paintComponent.

    Also, I didnt ignore your suggestion for moving the paintObj declaration out of the method, I tried but it I was getting a stack overload so its just sitting there for now.

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    How many different Paint class objects does your program create? I see one in the UpAction class.
    Why do you want more than one? What do the other instances of the class do?

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

    Default

    I have four paint class objects! They dont do anything special apart from accessing methods really. Am I missing a fundamental part of inheritance here?

    At the moment I literally just declare an object whenever I cant compile due to a method call in another class.

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Are any of the paint objects you are creating drawing on the screen? I suppose only one is.
    What are the other ones doing? Laying around waiting for their big chance to make it to the big time.
    If you call repaint for an instance of a class that is not being shown on the GUI, nothing happens.
    DarrylBurke likes this.

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

    Default

    Here is my whole program, if it helps.

    At the moment it is mainly the repaint that I am concerned about.

    Java Code:
    public class Main {
    	public static void main(String[] args) {
    	
    		// Create instances of classes
    		Frame frameObj = new Frame();
    		Remote remObj = new Remote();
    		
    		frameObj.setFrame();
    		remObj.createRemote();
    
    	}
    }
    Java Code:
    import javax.swing.*;
    
    public class Frame {
    	
    	JFrame TVFrame = new JFrame("TVFrame");
    	Paint paintObj = new Paint();
    	
    	public void setFrame() {
    		
    		System.out.println("Entered SetFrame");
    		
    		// Create, and set properties of frame.
    		TVFrame.setSize(800,600);
    		TVFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		TVFrame.setVisible(true);
    		TVFrame.add(paintObj);
    	}
    }
    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;
    	Channel chanObj = new Channel();
    		
    	public ImageIcon populateImg() {
    		
    		System.out.println("Entered populateImg");
    		
    		// Populate imgArray
    		if (imgArrayPop == false) { // The if statement doesn't work?
    			
    			System.out.println("Populating Arrays");
    			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.
    
    			System.out.println(imgArray[chanObj.setChannel()]);
    			return imgArray[chanObj.setChannel()];
    		
    	}
    	
    	public void paintComponent(Graphics g) {
    		 
    		Paint paintObj = new Paint();
    		System.out.println("Entered paintComponent");
    		// Receive return from PopulateImg() and display it.
    		image = paintObj.populateImg();
    		
    		System.out.println("Painting Image");
    		super.paintComponent(g);
    		image.paintIcon(this, g, 0, 0);
    		
    		}
    		
    }
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class Channel {
    	
    	public int currentChannel = 0;
    
    	public int setChannel() {
    		
    		System.out.println("Entered setChannel");
    		return currentChannel;
    		
    	}
    	
    	public class UpAction implements ActionListener {
    		
    		Paint paintObj = new Paint();
    		
    		public void actionPerformed (ActionEvent e) {
    			
    			System.out.println("Entered actionPerformed");
    			System.out.println(currentChannel);
    			currentChannel++;
    			System.out.println(currentChannel);
    			paintObj.repaint();
    		}
    	}
    }
    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    
    public class Remote {
    	public void createRemote() {
    		
    		Channel chanObj = new Channel();
    		
    		System.out.println("Entered createRemote");
    		JFrame remFrame = new JFrame("Remote");
    		remFrame.setVisible(true);
    		remFrame.setSize(300, 200);
    		remFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		JPanel remPanel = new JPanel();
    		JButton upButton = new JButton("Up");
    					
    		remFrame.add(remPanel);
    		remPanel.add(upButton);
    		
    		upButton.addActionListener(chanObj.new UpAction());
    		
    	}
    
    }

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

    Default

    I have a paint object on the Frame class which adds the paintObject declared within it to the TVFrame. And yes, I have only one image that is displayed, when I click the up button the next one doesnt appear.

  7. #27
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Get rid of all of the = new Paint() calls except the one that creates the panel that is being shown.
    If you call repaint for an object not being shown, nothing happens.
    You must get a reference to the Paint object that is being shown to be able to call its repaint() method.
    Only create one instance of the Paint object in the setFrame method. Then pass that reference to the places it is needed.

    Your class name conflicts with one of Java's class names. Frame should be something like MyFrame to be sure it's yours and not java's.

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

    Default

    Right.

    I changed the currentChannel variable, it now resides outside the main function, in the main class. Its static, and I can now access it from the other classes without having to create an instance of its class. Im not quite sure if this is what you meant, I am not that familiar with references in Java.

    The paintObj is now also static, and it resides in the MyFrame class.

    I had to change the currentChannel variable to static because it was being initialised to 0 every time its class was called, so I now moved it to the main class and I hope it would not be initialised again.

    It works now! The action listener works and the image is redrawn every time I click the button. I have noticed though, without even clicking the button, there is activity in the console and at random times code seems to be executed, especially when I seem to navigate through my classes in Eclipse. Im guessing this is just AWT?

    I now have to add functionality so that it moves down channels too, and I need the arrays to loop so it goes from [10] to [0] and [0] to [10], but I guess this should not be as difficult.. I am just thinking if statements in the action listeners.

    I really appreciate your quick replies and help Norm

  9. #29
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Sounds like you need to work on how to define and initialize class instances and variables. Making variables static is not the solution. When you need a reference to another class you can pass it via a method call with that reference as an argument.
    pbrockway2 likes this.

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

    Default

    I need to work on many things in Java, this isn't my ideal way to learn I always like to thoroughly cover the basics first but unfortunately time is out of question this time around so I have to do it this way.

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

    Default

    Making variables static is not the solution.
    I agree.

    The aim should be to have nothing static except the main() method. If you get stuck doing this, post the code. It's a "knack" and once you've seen the alternative that Norm referred to (initialising instances and passing references to them as arguments) you should have no problem.

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

    Default

    Quote Originally Posted by pbrockway2 View Post
    I agree.

    The aim should be to have nothing static except the main() method. If you get stuck doing this, post the code. It's a "knack" and once you've seen the alternative that Norm referred to (initialising instances and passing references to them as arguments) you should have no problem.
    I appreciate the help and I will take your advice into account. If you can post a simple example of passing references then I should be able to implement them easily, I have done much practise passing arguments and references just not in Java.

    Java Code:
    public class Main {
    	
    	// The current channel (picture to be displayed)
    	static int currentChannel = 0;
    	// The current volume
    	static int currentVol = 5;
    	
    	public static void main(String[] args) {
    	
    		// Create instances of classes
    		Remote remObj = new Remote();
    		MyFrame frameObj = new MyFrame();
    		
    		// Call the frame function, and then create remote.
    		frameObj.setFrame();
    		remObj.createRemote();
    	}
    }
    Java Code:
    import javax.swing.*;
    
    public class MyFrame {
    	
    	// instance of Paint class
    	static Paint paintObj = new Paint();
    	
    	public void setFrame() {
    		
    		System.out.println("Entered SetFrame");
    		
    		// Create, and set properties of frame.
    		JFrame TVFrame = new JFrame("TVFrame");
    		TVFrame.setSize(800,600);
    		TVFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		TVFrame.setVisible(true);
    		// Add the painting functionality to the frame.
    		TVFrame.add(paintObj);
    	}
    }
    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;
    	Channel chanObj = new Channel();
    		
    	public ImageIcon populateImg() {
    		
    		System.out.println("Entered populateImg");
    		
    		// Populate imgArray
    		if (imgArrayPop == false) { // The if statement doesn't work?
    			
    			System.out.println("Populating Arrays");
    			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.
    		System.out.println(chanObj.setChannel());
    		System.out.println(imgArray[chanObj.setChannel()]);
    		return imgArray[chanObj.setChannel()];
    	}
    	
    	public void paintComponent(Graphics g) {
    		 
    		System.out.println("Entered paintComponent");
    		// Receive return from PopulateImg() and display it. The return is the appropriate ImageIcon.
    		image = MyFrame.paintObj.populateImg();
    		
    		// Paint the image
    		System.out.println("Painting Image");
    		super.paintComponent(g);
    		image.paintIcon(this, g, 0, 0);
    	}
    }
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class Remote {
    	public void createRemote() {
    		
    		Channel chanObj = new Channel();
    		
    		// Create the remote
    		System.out.println("Entered createRemote");
    		JFrame remFrame = new JFrame("Remote");
    		remFrame.setVisible(true);
    		remFrame.setSize(300, 200);
    		remFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		JPanel remPanel = new JPanel();
    		JButton upButton = new JButton("Up");
    		JButton downButton = new JButton("Down");
    		JButton leftButton = new JButton("Left");
    		JButton rightButton = new JButton("Right");
    		JLabel volLabel = new JLabel("Volume  " + Main.currentVol);
    					
    		remFrame.add(remPanel);
    		
    		remPanel.add(upButton);
    		remPanel.add(downButton);
    		remPanel.add(rightButton);
    		remPanel.add(leftButton);
    		remPanel.add(volLabel);
    		
    		upButton.addActionListener(chanObj.new UpAction());
    		downButton.addActionListener(chanObj.new DownAction());
    		rightButton.addActionListener(chanObj.new RightAction());
    		leftButton.addActionListener(chanObj.new LeftAction());
    		
    	}
    	
    }
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class Channel {
    	public int setChannel() {
    		
    		System.out.println("Entered setChannel");
    		return Main.currentChannel;
    	}
    	
    	public class UpAction implements ActionListener {
    			public void actionPerformed (ActionEvent e) {
    							
    				System.out.println("Entered UpAction");
    				System.out.println(Main.currentChannel);
    				// Channel is incremented
    				Main.currentChannel++;
    				
    				// if channel is incremented to 10, set it back to 0 to prevent overrun.
    				if (Main.currentChannel == 10) {
    					Main.currentChannel = 0;
    				}
    				
    				System.out.println(Main.currentChannel);
    				// repaint frame with new channel
    				MyFrame.paintObj.repaint();
    		}
    	}
    	
    	public class DownAction implements ActionListener {
    		public void actionPerformed (ActionEvent e) {
    									
    			System.out.println("Entered DownAction");
    			System.out.println(Main.currentChannel);
    			// Channel is decremented
    			Main.currentChannel--;
    			
    			// if channel is decremented to -1, set it back to 9 to prevent overrun.
    			if (Main.currentChannel == -1) {
    				Main.currentChannel = 9;
    			}
    			System.out.println(Main.currentChannel);
    			// repaint frame with new channel
    			MyFrame.paintObj.repaint();
    		}
    	}
    	
    	public class RightAction implements ActionListener {
    		public void actionPerformed (ActionEvent e) {
    						
    			System.out.println("Entered RightAction");
    			
    			// create volume max limit
    			if (Main.currentVol != 10) {
    				// Volume is moved up
    				Main.currentVol++;
    			}
    			System.out.println(Main.currentVol);
    		}
    	}
    	
    	public class LeftAction implements ActionListener {
    		public void actionPerformed (ActionEvent e) {
    						
    			System.out.println("Entered LeftAction");
    			
    			// create volume min limit
    			if (Main.currentVol != 0) {
    				// Volume is moved down
    				Main.currentVol--;
    			}
    			System.out.println(Main.currentVol);
    		}
    	}
    	
    	
    }
    Im going sleep now its been a long day but I will resume first thing in the morning.

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

    Default

    If you can post a simple example of passing references...
    Here's a simple example:

    Java Code:
    import java.awt.Color;
    
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    	/** A panel that displays a label showing its background colour. */
    public class ColorPanel extends JPanel {
    	
    	private JLabel label = new JLabel("???");
    	
    	public ColorPanel() {
    		add(label);
    	}
    	
    		/** Sets the colour to given red, green and blue values. */
    	public void setBackgroundColor(int r, int g, int b) {
    		setBackground(new Color(r, g, b));
    		label.setText(getBackgroundColorString());
    	}
    	
    		/** Returns the current colour as a formatted string. */
    	public String getBackgroundColorString() {
    		Color back = getBackground();
    		return String.format("#%h%h%h", back.getRed(), back.getGreen(), back.getBlue());
    	}
    }
    
    
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    	/** A panel that allows a ColorPanel to be altered. */
    public class ControllerPanel extends JPanel {
    	
    	private JTextField rTF = new JTextField();
    	private JTextField gTF = new JTextField();
    	private JTextField bTF = new JTextField();
    	private JButton button = new JButton("Change!");
    	
    	[color=red]private ColorPanel panel;[/color]
    	
    		/** Creates a new controller that will control a given colour panel. */
    	public ControllerPanel([color=red]ColorPanel panel[/color]) {
    		super(new GridLayout(0, 1));
    		[color=red]this.panel = panel;[/color]
    		
    		add(rTF);
    		add(gTF);
    		add(bTF);
    		button.addActionListener(changer);
    		add(button, BorderLayout.SOUTH);
    	}
    	
    		/** Handles button clicks by changing the panel's colour. */
    	private ActionListener changer = new ActionListener() {
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			try {
    				int r = Integer.parseInt(rTF.getText()) % 256;
    				int g = Integer.parseInt(gTF.getText()) % 256;
    				int b = Integer.parseInt(bTF.getText()) % 256;
    				panel.setBackgroundColor(r, g, b);
    			} catch(NumberFormatException nfe) {
    				nfe.printStackTrace();
    			}
    		}
    	};
    
    }
    
    
    
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    public class Main {
    
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			@Override
    			public void run() {
    				ColorPanel color = new ColorPanel();
    				ControllerPanel controller = new ControllerPanel([color=red]color[/color]);
    				
    				JFrame frame = new JFrame("ColorPanel test");
    				frame.add(color);
    				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				frame.pack();
    				frame.setVisible(true);
    				
    				frame = new JFrame("Controller");
    				frame.add(controller);
    				frame.pack();
    				frame.setVisible(true);
    			}
    		});
    
    	}
    }
    There are a couple of things to note:

    I don't have separate classes for the colour panel and the frame. The frame is a "throw away" that gets created in the Main class, shown and forgotten. Also I put the controller's button click handler in the same class as the panel containing the button. Again the frame doesn't warrant a separate class. So, all together, three classes for the colour panel, a controller panel and the application itself.

    The controller needs to know what to control. That's where the reference passing comes in. (Shown in red). Notice how constructors have been used in both the panel classes - the COlorPanel constructor doesn't do a lot, but the ControllerPanel constructor is critical in recieving the reference to the ColorPanel that is being controlled.

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

    Default

    I note that your code is currently functioning - at least when I supplied some images and ran it the channel changing buttons did the right thing. So it is totally up to you whether or not you decide to pull things apart in order to remove all the statics. Because it will involve pulling things apart!

    One place to start might be the "static int currentChannel = 0;" in Main. You could remove this completely. Classes are models of things in the world. And typically instance variables (like currentChannel) are created on a "need to know" basis - that makes it easy to manage them. There is only one class that really needs to know anything about the current channel and that's the Paint class. (A better name for this class would be TvPanel because it's a ... panel depicting a TV). I would move currentChannel into this class as an ordinary, nonstatic, variable.

    Then see what breaks. Which is the Channel class.

    For a start there's setChannel(), but you aren't using this method anyway so it can just be removed.

    If you think about it, it's the TvPanel (aka Paint) that should have a setChannel() and a getChannel() - because channels are things that are set on TVs Altogether Paint will look something like this:

    Java Code:
    import java.awt.Graphics;
    
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
    
    	/** A panel depicting a TV. */
    public class Paint extends JPanel {
    	
    	private ImageIcon image;	
    	private ImageIcon imgArray[] = new ImageIcon[10];
    
    	public Paint() {
    		System.out.println("Creating a new TV panel");
    		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"));
    	}
    	
    		/** Returns the current channel. */
    	public int getChannel() {
    		return -1; // return the actual current channel value.  Should there be an
    		           // instance variable for the current channel?
    	}
    	
    		/** Sets the current channel to a given value. */
    	public void setChannel(int ch) {
    		// set the channel
    		// repaint now?
    	}
    
    	public void paintComponent(Graphics g) {
    		System.out.println("Entered paintComponent");
    		super.paintComponent(g);
    		image.paintIcon(this, g, 0, 0);
    	}
    }
    (Note the use of a constructor to replace the ... interesting ... code you had to read the images just once. The constructor is called just once and its job is to get the instance ready to use.

    The paintComponent() is now simpler because all it has to do is paint image. setChannel() will take care of making sure the value of image is correct. It's good when a method does just one thing.)

    Two of the CHannel classes actionPerformed() methods will still have lots of errors because they use the static (Main) currentChannel variable. They need to be replaced with code that calls the newly added setChannel()/getChannel() methods. The problem is that the Channel instance has no way of knowing what TV they are linked to.

    Enter the referencing passing. Remote needs a constructor to be told what it is a remote for.

    Getting the button click handlers to use this Paint reference will be easier if the Remote and Channel channels are rolled into one. Getting the Paint reference actually passed to the Remote when you construct it might be easier if both panels are actually created in the main() method at the start.
    Last edited by pbrockway2; 07-13-2011 at 09:39 AM.
    B_Tank88 likes this.

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

    Default

    Ok, its finished now. All functionality works, I implemented a timer and it works great.

    I will start work on optimising it using the advice above after I figure out how to make it work on any PC, I will research JAR files as suggested earlier in the thread.

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

    Default

    I am re writing the whole program without using static variables and making use of passing references.

    I am struggling to create the channel variable, I am not sure where abouts to assign it. It seems to initialise it sometime before the variable is returned to the recieving method, as the returned value is always 0, despite it incrementing correctly. At the moment it resides in the class, but when I move it to the constructor of the channel method then scope is lost.

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    
    public class Channel {
    
    	public ImageProcess imgObj;
    	int channel = 0;
    	
    	public Channel(ImageProcess imgRef) {
    		this.imgObj = imgRef;
    		// IF I MOVE int channel HERE THEN SCOPE IS LOST
    	}
    	
    	public class UpAction implements ActionListener {
    		public void actionPerformed (ActionEvent e) {
    			System.out.println("Entered UpAction");
    			channel++;
    			System.out.println("Channel = " + channel);
    			if (channel == 10) channel = 0;
    			imgObj.repaint();
    		}
    	}
    	
    	public int ChanReturn() {
    		return this.channel;
    	}
    }
    Last edited by B_Tank88; 07-16-2011 at 03:33 PM.

  17. #37
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    It seems to initialise it sometime before the variable is returned to the recieving method, as the returned value is always 0
    How many different Channel objects are you creating? You should only create one if you want to be able to retain the value of the channel variable as it changes. If you create new Channel objects the value of the channel variable is initialized to 0.

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

    Default

    Thanks for the help, I did have more than one channel object which I have removed and I am passing just one instance of each around now.

    I have rewritten it all and it looks good to me:

    Java Code:
    public class Main {	
    	public static void main (String[] args) {
    		System.out.println("Entered main");
    		
    		ImageProcess imgObj = new ImageProcess(); // Create class instances
    		Channel chanObj = new Channel(imgObj); // Pass ImageProcess instance to channel class
    		imgObj.ChannelRecieve(chanObj); // Pass Channel class instance to ImageProcess class
    		
    		GUI guiObj = new GUI(imgObj, chanObj); // Start creating GUI
    	}
    }
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    
    public class ImageProcess extends JPanel {	
    	
    	private ImageIcon imgArray[] = new ImageIcon[10];
    	private ImageIcon image;
    	private Channel chanObj;
    	
    	public ImageProcess() { // Populate the image array with filepaths
    		
    		System.out.println("Entered Image class - Populating imgArray");
    		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"));
    	}
    	
    	public void ChannelRecieve(Channel chanRef) { // Recieve and assign channel referece
    		System.out.println("Entered ImageProcess - ChannelRecieve");
    		this.chanObj = chanRef;
    	}
    	
    	public void paintComponent(Graphics g) { // Paint the image
    		System.out.println("Entered paintComponent of Image class");
    		System.out.println("Current channel = " + imgArray[chanObj.ChanReturn()]);
    		image = imgArray[chanObj.ChanReturn()];
    		super.paintComponent(g);
    		image.paintIcon(this, g, 0, 0);
    	}	
    }
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class Channel {
    
    	private ImageProcess imgObj;
    	private int channel = 0;
    	private int volume = 5;
    	private JLabel volLabel; // Contains return reference of volLabel from GUI
    	private TimerAction taObj = new TimerAction();
    	private Timer timer = new Timer(1000, taObj);
    	
    	public Channel(ImageProcess imgRef) { // Assign reference to ImageProcess class, set timer stop.
    		System.out.println("Entered Channel constructor");
    		this.imgObj = imgRef;
    		timer.stop();
    	}
    	
    	public void RecieveLabel (JLabel labelRef) {
    		this.volLabel = labelRef;
    	}
    
    	public class UpAction implements ActionListener {
    		public void actionPerformed (ActionEvent e) {
    			System.out.println("Entered UpAction");
    			channel++;
    			System.out.println("Channel = " + channel);
    			if (channel == 10) channel = 0;
    			imgObj.repaint();
    		}
    	}
    	
    	public class DownAction implements ActionListener {
    		public void actionPerformed (ActionEvent e) {
    			System.out.println("Entered DownAction");
    			channel--;
    			System.out.println("Channel = " + channel);
    			if (channel == -1) channel = 9;
    			imgObj.repaint();
    		}
    	}
    	
    	public class LeftAction implements ActionListener {
    		public void actionPerformed (ActionEvent e) {
    			System.out.println("Entered LeftAction");
    			if (volume != 0) {
    				volume--;
    				volLabel.setText("Vol:  " + volume);
    			}
    			timer.restart();
    			volLabel.setVisible(true);
    		}
    	}
    	
    	public class RightAction implements ActionListener {
    		public void actionPerformed (ActionEvent e) {
    			System.out.println("Entered RightAction");
    			if (volume != 10) {
    				volume++;
    				volLabel.setText("Vol:  " + volume);
    			}
    			timer.restart();
    			volLabel.setVisible(true);
    		}
    	}
    	
    	public class TimerAction implements ActionListener {
    		public void actionPerformed (ActionEvent e) {
    			System.out.println ("Entered TimerAction");
    			volLabel.setVisible(false);
    			timer.stop();
    		}
    	}
    	
    	public int ChanReturn() { // Return the current channel to caller
    		System.out.println("Entered ChanReturn, returning: " + channel);
    		return channel;
    	}
    }
    Java Code:
    //package org.tvGUI;
    
    import javax.swing.*;
    import java.awt.event.*;
    
    // The GUI class will create the TV simulation view and Remote interface
    
    public class GUI extends JPanel {
    	
    	private Channel chanObj;
    	private ImageProcess imgObj;
    	
    	public GUI(ImageProcess imgRef, Channel chanRef) { // Assign references, create TV and remote.
    		System.out.println("Entered GUI class constructor");	
    		this.imgObj = imgRef;
    		this.chanObj = chanRef;
    		createTV(); // draw TV
    		createRemote(); // draw remote
    	}
    	
    	public void createTV() {
    		System.out.println("Entered method createTV of class GUI");
    		
    		// Create TVFrame
    		JFrame tvFrame = new JFrame("TV");
    		tvFrame.setVisible(true);
    		tvFrame.setSize(1024,768);
    		tvFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		// REVIEW - volLabel must sit on top of tvPanel
    		
    		// Create and add TVPanel
    		JPanel tvPanel = new JPanel();
    		tvFrame.add(tvPanel);
    
    		// Create and add LabelPanel
    		JPanel labelPanel = new JPanel();
    		tvFrame.add(labelPanel);
    		
    		// Create and add volume label to tvPanel
    		JLabel volLabel = new JLabel();
    		labelPanel.add(volLabel);
    		chanObj.RecieveLabel(volLabel); // Send label to Channel class
    		
    		// /REVIEW
    		
    		tvFrame.add(imgObj); // assign image processor to tvFrame
    	}
    	
    	public void createRemote() {
    		System.out.println("Entered method createRemote of class GUI");
    		
    		// Create Remote frame
    		JFrame remFrame = new JFrame("Remote");
    		remFrame.setVisible(true);
    		remFrame.setSize(200, 110);
    		remFrame.setLocation(400, 0);
    		remFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		// Create and add REMPanel
    		JPanel remPanel = new JPanel();
    		remFrame.add(remPanel);
    		
    		// Create and add JButtons
    		JButton upButton = new JButton("Up");
    		JButton downButton = new JButton("Down");
    		JButton leftButton = new JButton("Left");
    		JButton rightButton = new JButton("Right");
    		remPanel.add(upButton);
    		remPanel.add(downButton);
    		remPanel.add(leftButton);
    		remPanel.add(rightButton);
    		
    		// Set JButton tooltips
    		upButton.setToolTipText("Go up one channel");
    		downButton.setToolTipText("Go down one channel");
    		leftButton.setToolTipText("Decrease volume");
    		rightButton.setToolTipText("Increase volume");
    		
    		// add actionlisteners from Channel class to buttons
    		upButton.addActionListener(chanObj.new UpAction());
    		downButton.addActionListener(chanObj.new DownAction());
    		leftButton.addActionListener(chanObj.new LeftAction());
    		rightButton.addActionListener(chanObj.new RightAction());
    	}	
    }
    I need to perform one amendment though, I hope somebody could point me in the right direction. I had the volume label update within the remote before, but I now have to have it appear on top of the TV screen (to simulate a real TV).

    I am not sure how to go about doing this, I have at the moment added a second panel to the TV frame and tried to orient it but not having success. The label itself works as its supposed, I just need to display it differently.

    Advice?

  19. #39
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

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

    Default

    Could someone please advise on how actionlistener events can be changed from the default 'after click' to 'on click'?

Page 2 of 3 FirstFirst 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
  •