Help with actionlisteners with a loop and JButton

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 849 Views - Last Post: 08 March 2012 - 07:56 AM Rate Topic: -----

#16 NantucketSleighride  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 98
  • Joined: 13-February 11

Re: Help with actionlisteners with a loop and JButton

Posted 06 March 2012 - 05:39 PM

The null statements: for the layout - it's just saying to not use a layout manager. You can use one if you want (like the gridlayout, etc - I never use them). For the LocationRelativeTo - null just centers it.

the "this" statement is referring to the frame. You've made the class extend a JFrame, so I'm using 'this' to refer to the frame - then using the method. I believe 'this' isn't necessary if you know the methods you want to use. It's just habit for me to put them in. It's the same as you've done in your first post. You created a JPanel and then have 'add (panelName)' in your code. It's the same as my 'this.add(panelName)'. I just have a habit of always typing 'this' for some reason. I believe it's not needed, though.
Was This Post Helpful? 0
  • +
  • -

#17 AllHighway  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 08-February 12

Re: Help with actionlisteners with a loop and JButton

Posted 06 March 2012 - 05:43 PM

OK.
Was This Post Helpful? 0
  • +
  • -

#18 AllHighway  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 08-February 12

Re: Help with actionlisteners with a loop and JButton

Posted 06 March 2012 - 06:12 PM

Do I add the if statement after the method to change the light or after the paint method?
Also I am guessing I need to redraw the ovals after every if statement to progress through the colors.
Was This Post Helpful? 0
  • +
  • -

#19 AllHighway  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 08-February 12

Re: Help with actionlisteners with a loop and JButton

Posted 06 March 2012 - 06:37 PM

I am lost here.
		public void actionPerformed(ActionEvent e){
		changeLightState(); //calls the changeLightState method. The method changes the state of the light. 0 = red, 1 = yellow, 2 = green
		repaint(); //repaints the screen

public void changeLightState(){
		
		
		
		//use this method to change the Light state. You just need one if statement here, but you can do a longer one if you want. Doesn't really matter as long as it works.


I was able to draw the circles but I am not able to get the if statement to work. I really appreciate your help, but I think this is a little over my head right now.

This post has been edited by AllHighway: 06 March 2012 - 06:42 PM

Was This Post Helpful? 0
  • +
  • -

#20 AllHighway  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 08-February 12

Re: Help with actionlisteners with a loop and JButton

Posted 06 March 2012 - 07:59 PM

I can not get a button to show up in the traffic light. I managed to construct my if then statements right.....I think. But, a JButton is missing. Any help is appreciated.


import javax.swing.JFrame;
public class TrafficLight
{

public static void main (String[] args)
{
JFrame frame = new JFrame ("Traffic Signal");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
Light panel = new Light();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}

import java.awt.*;

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



public class Light extends JPanel {

	
	int light;
	JLabel label;
	JButton push;
	
	public Light (){
	
		setBackground(Color.gray);
		setPreferredSize (new Dimension(200,400));
		
		 push = new JButton("Change Color");
		 push.setBounds(60, 350, 80, 30);
		 push.addActionListener (new ButtonListener());
		
	}
			
		
	public void paint(Graphics g){
		super.paint(g); {
			
				g.setColor (Color.black);
				g.fillRect (0, 0, 200, 400); 
				g.setColor (Color.red);
				g.fillOval (50, 10, 100, 100); 
				g.setColor (Color.gray);
				g.fillOval (50, 120, 100, 100);
				g.setColor (Color.gray);
				g.fillOval (50, 230, 100, 100);
	
			
			
			
			}
	
		}
		
public class ButtonListener implements ActionListener
{
	@SuppressWarnings("null")
	public void actionPerformed (ActionEvent event)
	{
	
	Graphics page = null;
				
				if   (light==0);
				{

			 	page.setColor (Color.black);
			 	page.fillRect (0, 0, 200, 400); 
			 	page.setColor (Color.gray);
			 	page.fillOval (50, 10, 100, 100); 
			 	page.setColor (Color.gray);
			 	page.fillOval (50, 120, 100, 100);
			 	page.setColor (Color.green);
			 	page.fillOval (50, 230, 100, 100);
				light++;
				
		}
			
				if (light==1);{
				
				page.setColor (Color.black);
				page.fillRect (0, 0, 200, 400); 
				page.setColor (Color.gray);
				page.fillOval (50, 10, 100, 100); 
				page.setColor (Color.yellow);
				page.fillOval (50, 120, 100, 100);
				page.setColor (Color.gray);
				page.fillOval (50, 230, 100, 100);
				light=light%3;}
	}}}
		


Was This Post Helpful? 0
  • +
  • -

#21 AllHighway  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 08-February 12

Re: Help with actionlisteners with a loop and JButton

Posted 07 March 2012 - 09:07 AM

OK I think I might have some advancement in this code. I am now getting errors in the loop where the paint method is. I am not really sure why. I tried to research it, but I am stumped.
import java.awt.*;

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;



public class Light extends JPanel {

	
	int light;
	JLabel label;
	JButton push;
	
	public Light (){
	
		setBackground(Color.gray);
		setPreferredSize (new Dimension(200,400));
		
		 push = new JButton("Change Color");
		 push.setBounds(60, 350, 80, 30);
		 push.addActionListener (new ButtonListener());
		
	}
			
		
	public void paint1(Graphics g){
		super.paint(g); {
			
				g.setColor (Color.black);
				g.fillRect (0, 0, 200, 400); 
				g.setColor (Color.red);
				g.fillOval (50, 10, 100, 100); 
				g.setColor (Color.gray);
				g.fillOval (50, 120, 100, 100);
				g.setColor (Color.gray);
				g.fillOval (50, 230, 100, 100);
	
			
			
			
			}
	
		}
		
public class ButtonListener implements ActionListener
{

	public void actionPerformed (ActionEvent event)
	{
	
				
				if   (light==0);{
				
					public void paint(Graphics g){
					
				
							
								g.setColor (Color.black);
								g.fillRect (0, 0, 200, 400); 
								g.setColor (Color.gray);
								g.fillOval (50, 10, 100, 100); 
								g.setColor (Color.gray);
								g.fillOval (50, 120, 100, 100);
								g.setColor (Color.green);
								g.fillOval (50, 230, 100, 100);
								light++;
					}
	
					
				if   (light==1);{
					
					public void paint(Graphics g){
					
				
							
								g.setColor (Color.black);
								g.fillRect (0, 0, 200, 400); 
								g.setColor (Color.gray);
								g.fillOval (50, 10, 100, 100); 
								g.setColor (Color.gray);
								g.fillOval (50, 120, 100, 100);
								g.setColor (Color.green);
								g.fillOval (50, 230, 100, 100);
								light++;
								light=light%3;}
				}}
	


Was This Post Helpful? 0
  • +
  • -

#22 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9038
  • View blog
  • Posts: 33,526
  • Joined: 27-December 08

Re: Help with actionlisteners with a loop and JButton

Posted 07 March 2012 - 01:09 PM

Quote

I am now getting errors in the loop where the paint method is.

And they are?
Was This Post Helpful? 0
  • +
  • -

#23 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Help with actionlisteners with a loop and JButton

Posted 07 March 2012 - 01:22 PM

super.paint(g); {
if (light==0);{
if (light==1);{

You need to remove the semi-colon after those.

Also, you cannot have a method inside of a method. So the 2 paint methods that are inside of the actionPerformed are incorrect.
Was This Post Helpful? 1
  • +
  • -

#24 AllHighway  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 41
  • Joined: 08-February 12

Re: Help with actionlisteners with a loop and JButton

Posted 08 March 2012 - 07:56 AM

OK, I removed the methods. Now how would I get the if loop to reprint the graphics I want to display.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2