Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Norm

Thread: Need help with Simple Swing program

  1. #1
    abhishek777 is offline Member
    Join Date
    Feb 2017
    Posts
    2
    Rep Power
    0

    Default Need help with Simple Swing program

    I have coded a small Swing program to re-paint a oval every time a button is clicked.
    The re-paint part works fine. i.e. everytime i click the button, the oval gets re-painted fine.

    However, i want that everytime i launch my program, my oval should be having a default fixed color but in the current program everytime i launch my program, my oval comes up with a random color

    How can I achieve:
    1. Oval with a default color (say Red) when I launch the program
    -------------------------------------------------------
    Below is my full program:
    -------------------------------------------------------
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class SimpleGui3C implements ActionListener {
    	JFrame frame;
    
    	public static void main(String[] args) {
    		SimpleGui3C gui = new SimpleGui3C();
    		gui.go();
    	}
    
    	public void go() {
    		frame = new JFrame("Changing colors");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		JButton button = new JButton("Change colors");
    		button.addActionListener(this);
    		MyDrawPanel1 drawPanel = new MyDrawPanel1();
    		frame.getContentPane().add(BorderLayout.SOUTH, button);
    		frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
    		frame.setSize(300, 300);
    		frame.setLocation(400, 300);
    		frame.setVisible(true);
    	}
    
    	public void actionPerformed(ActionEvent event) {
    		frame.repaint();
    	}
    }
    
    class MyDrawPanel1 extends JPanel {
    	public void paintComponent(Graphics g) {
    		int red = (int) (Math.random() * 255);
    		int green = (int) (Math.random() * 255);
    		int blue = (int) (Math.random() * 255);
    		Color randomColor = new Color(red, green, blue);
    		g.setColor(randomColor);
    		g.fillOval(40, 40, 100, 100);
    	}
    }
    Last edited by abhishek777; 02-19-2017 at 03:59 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Need help with Simple Swing program

    So you are asking how to use a default value the first time and a random value every time after that?
    One way is to use a boolean variable to detect the first time and change its value after that so the code knows it is not the first time.
    Java Code:
      if(firstTime) {
        firstTime = false; // no longer first
        // use fixed color
      }else {  // not the first time
       // use random color
      }
    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    Last edited by Norm; 02-19-2017 at 03:26 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    abhishek777 is offline Member
    Join Date
    Feb 2017
    Posts
    2
    Rep Power
    0

    Default Re: Need help with Simple Swing program

    I tried your suggested approach by creating a static boolean variable in my main class and re-setted it to false. I want to understand from guru if this a good approach or there can be other alternative. Modified code is as follows. It can be pasted directly in eclipse and run to see the output
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class SimpleGui3C implements ActionListener {
    	JFrame frame;
    	static boolean firstTime=true;
    
    	public static void main(String[] args) {
    		SimpleGui3C gui = new SimpleGui3C();
    		gui.go();
    	}
    
    	public void go() {
    		frame = new JFrame("Changing colors");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		JButton button = new JButton("Change colors");
    		button.addActionListener(this);
    		MyDrawPanel1 drawPanel = new MyDrawPanel1();
    		frame.getContentPane().add(BorderLayout.SOUTH, button);
    		frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
    		frame.setSize(300, 300);
    		frame.setLocation(400, 300);
    		frame.setVisible(true);
    	}
    
    	public void actionPerformed(ActionEvent event) {
    		frame.repaint();
    	}
    }
    
    class MyDrawPanel1 extends JPanel {
    	public void paintComponent(Graphics g) {
    		if(SimpleGui3C.firstTime){
    			SimpleGui3C.firstTime = false;
    			g.setColor(Color.RED);
    		}
    		else{
    			int red = (int) (Math.random() * 255);
    			int green = (int) (Math.random() * 255);
    			int blue = (int) (Math.random() * 255);
    			Color randomColor = new Color(red, green, blue);
    			g.setColor(randomColor);		
    		}		
    		g.fillOval(40, 40, 100, 100);
    	}
    }

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Need help with Simple Swing program

    The boolean variable should be in the class that uses it: MyDrawPanel1
    not in the SimpleGui3C class.

    That depends on if you have multiple instances of MyDrawPanel1 and you want each to have its own starting color
    or if you only want one of the instances to use its starting color and all the others to start with random colors.
    abhishek777 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Need help with Simple Swing program

    Did you consider having the actionListener in MyDrawPanel1. Then having the button change the value of randomColor and repaint the Oval?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 6
    Last Post: 12-15-2013, 07:40 AM
  2. Simple Swing problem
    By Arom in forum AWT / Swing
    Replies: 1
    Last Post: 02-11-2012, 06:50 PM
  3. Changing squares to rectangles in simple Swing program
    By codeRunner1 in forum AWT / Swing
    Replies: 0
    Last Post: 03-02-2011, 06:40 AM
  4. Simple swing program, help please!
    By N00Bie in forum New To Java
    Replies: 5
    Last Post: 02-17-2011, 06:16 PM
  5. Java Swing Simple Addition help
    By dbasenoob in forum New To Java
    Replies: 4
    Last Post: 04-27-2010, 02:26 PM

Tags for this Thread

Posting Permissions

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