Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

changing the color of a circle

setpreferredsize

  • Please log in to reply
2 replies to this topic

#1 Cruel Hand

Cruel Hand

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 195 posts
  • Programming Language:Java, Objective-C, Visual Basic .NET
  • Learning:C, Java, C++, Objective-C, PHP, (Visual) Basic, Python, JavaScript, Perl, Ruby, PL/SQL, Pascal, Assembly, Haskell

Posted 06 March 2012 - 04:38 PM

ok so I have to write a program that displays a traffic light and three buttons, red, yellow, and green and when they're clicked, the light changes to the corresponding button colors. I have everything right (I think) except that when I click the buttons it doesn't change instantly, but if I maximize/minimize the window it will change. For example, if I click yellow, the light stays red. But then when I maximize the window it will change to yellow. Can someone help me so it will change instantly?

Here is my code:

TrafiicLight Class
import javax.swing.JFrame;

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

TrafficLightPanel Class
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TrafficLightPanel extends JPanel{
	private JLabel changeColor;
	private JButton red, green, yellow;
	private Circle light;
	
	public TrafficLightPanel(){
		light = new Circle(20, Color.red, 150, 70);
		changeColor = new JLabel("Change color to: ");
		red = new JButton("red");
		red.addActionListener(new RedButtonListener());
		green = new JButton("green");
		green.addActionListener(new GreenButtonListener());
		yellow = new JButton("yellow");
		yellow.addActionListener(new YellowButtonListener());
		
		add(changeColor);
		add(red);
		add(yellow);
		add(green);
		
		setPreferredSize (new Dimension(300, 100));
		setBackground (Color.gray);
	}
	public void paintComponent(Graphics page){
		super.paintComponent(page);
		
		page.setColor(Color.black);
		page.fillRect(150, 50, 20, 40);
		
		/*page.setColor(Color.red);
		page.fillOval(150, 60, 20, 20);*/
		
		light.draw(page);
		
		
	}
	private class RedButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent event){
			light.setColor(Color.red);
			System.out.println(light.getColor());
		}
	}
	private class YellowButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent event){
			light.setColor(Color.yellow);
			System.out.println(light.getColor());
		}
	}
	private class GreenButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent event){
			light.setColor(Color.green);
		}
	}
}

Circle Class
import java.awt.*;

public class Circle {
	private int diameter, x, y;
	private Color color;

	public Circle (int size, Color shade, int upperX, int upperY){
		diameter = size;
		color = shade;
		x = upperX;
		y = upperY;
	}
	public void draw (Graphics page){
		page.setColor (color);
		page.fillOval (x, y, diameter, diameter);
	}

	public void setDiameter (int size){
		diameter = size;
	}

	public void setColor (Color shade)
	{
		color = shade;
	}
	public void setX (int upperX){
		x = upperX;
	}

	public void setY (int upperY){
		y = upperY;
	}

	public int getDiameter (){
		return diameter;
	}
	public Color getColor (){
		return color;
	}

	public int getX (){
		return x;
	}

	public int getY (){
		return y;
	}
}

thanks a lot in advance for the help!!
  • 0

#2 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 06 March 2012 - 04:45 PM

Call repaint after you have set the variables telling paint what color to paint with. The JVM will then call the paint method which can then paint with the new color
  • 0

#3 Cruel Hand

Cruel Hand

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 195 posts
  • Programming Language:Java, Objective-C, Visual Basic .NET
  • Learning:C, Java, C++, Objective-C, PHP, (Visual) Basic, Python, JavaScript, Perl, Ruby, PL/SQL, Pascal, Assembly, Haskell

Posted 06 March 2012 - 09:20 PM

thank you, it worked!! for all those curious, the new code now looks like this:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TrafficLightPanel extends JPanel{
private JLabel changeColor;
private JButton red, green, yellow;
private Circle light;

public TrafficLightPanel(){
light = new Circle(20, Color.red, 150, 70);
changeColor = new JLabel("Change color to: ");
red = new JButton("red");
red.addActionListener(new RedButtonListener());
green = new JButton("green");
green.addActionListener(new GreenButtonListener());
yellow = new JButton("yellow");
yellow.addActionListener(new YellowButtonListener());

add(changeColor);
add(red);
add(yellow);
add(green);

setPreferredSize (new Dimension(300, 100));
setBackground (Color.gray);
}
public void paintComponent(Graphics page){
super.paintComponent(page);

page.setColor(Color.black);
page.fillRect(150, 50, 20, 40);

light.draw(page);


}
private class RedButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
light.setColor(Color.red);
repaint();
}
}
private class YellowButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
light.setColor(Color.yellow);
repaint();
}
}
private class GreenButtonListener implements ActionListener{
public void actionPerformed(ActionEvent event){
light.setColor(Color.green);
repaint();
}
}
}
  • 0





Also tagged with one or more of these keywords: setpreferredsize

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download