Results 1 to 9 of 9
  1. #1
    bballin24 is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default JFrame repaint()

    Hey,

    Right now I am doing a project in my Computer science IB (International baccalaureate) Higher level program, and I am having trouble with a repaint method within my code.

    The current issue is that, I have a base frame that needs to be repainted when a button is pressed from another frame in another class.

    The base is that the actionPerformed method in class Months needs to call the paintComponent method in class Calander1. Both classes are different and have seperate frames

    Code is shown below, I need the monthListener to basically call the paintComponent method in the other class

    Java Code:
    class monthListener implements ActionListener {
    			
    			public void actionPerformed(ActionEvent e) {
    				if (e.getActionCommand().equals("jan")){
    					month = "jan";
    					Calander1 calander = new Calander1();
    					calander.repaints();
    				}
    				
    			}
    			
    		}


    The paintComponent class : Obviously not all of the code in their is relevant to this

    Java Code:
    public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		
    		Months month = new Months();
    		
    		System.out.println(month.getMonth());
    		System.out.println("Called the paintComponent outside");
    		if (month.getMonth().equals("jan")){
    			System.out.println("Called the paintComponent inside");
    		}
    
    		// Setting the variables in the frame to the correct amount
    		friends = friends();
    		school = school();
    		activities = activities();
    		startTime1 = start();
    		endTime1 = end();
    		amount = amount();
    
    		if (friends==0 && activities == 0){
    			school = 0;
    		}
    		else{
    			school = school - friends - activities;
    		}
    
    		// The next section includes drawing the rectangle and the strings
    		// inside the rectangle
    		for (int count = 0; count < 7; count++) {
    
    			for (int count2 = 0; count2 < 5; count2++) {
    				g.drawRect(x, y, 100, 100);
    
    				if (count2 == 0 && count == 1) {
    					g.drawString("Total events " + amount, x + 5, y + 11);
    					g.drawString("School Events: " + school, x + 5, y + 30);
    					g.drawString("Friend Events: " + friends, x + 5, y + 49);
    					g.drawString("Activity Events: " + activities, x + 5,
    							y + 65);
    					g.drawString("Start of Events: " + startTime1, x + 5,
    							y + 80);
    					g.drawString("End of Events: " + endTime1, x + 5, y + 99);
    				}
    				y += 100;
    			}
    			y = 100;
    			x += 100;
    
    		}
    		// This section includes drawing the days of the week
    		for (int count = 0; count < 7; count++) {
    			g.setFont(daysFont);
    			g.setColor(Color.BLUE);
    			g.drawString(days[count], daysX, daysY);
    
    			daysX += 100;
    		}
    
    	}

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: JFrame repaint()

    What does the Calendar1 class's repaints() method do?

    If you create a new instance of the Calendar1 class in the listener method, is that the one you want to do the display or is there another one that has the data to be displayed?

  3. #3
    bballin24 is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: JFrame repaint()

    I want to repaint the calander1 panel that has already been created. The repaints method is just a method that has the repaint method within it

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: JFrame repaint()

    You will need a reference to the existing instance of the class so you can call its methods.
    You could pass one to the MonthListener class(in its constructor) when you create an instance of it.

  5. #5
    bballin24 is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: JFrame repaint()

    Quote Originally Posted by Norm View Post
    You will need a reference to the existing instance of the class so you can call its methods.
    You could pass one to the MonthListener class(in its constructor) when you create an instance of it.
    The existing panel is attached to a frame in the main class that I have, so how would gain access to that panel through a constructor

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: JFrame repaint()

    I was referring to the constructor for the MonthListener class. You need the reference to the Calender1 class inside of the MonthListener class. When you create the MonthListener instance, pass a reference to the Calender1 class in the MonthListener's constructor.

  7. #7
    bballin24 is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: JFrame repaint()

    Im not trying to get spoonfed answers here, but how exactly would I do this i'm fairly confused

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: JFrame repaint()

    I'm not sure where you are having problems.
    Do you have a reference to the Calender1 object when you create the MonthListener object?
    Do you know how to write a class's constructor?
    Do you know how to pass a variable to a constructor when you create an instance of the class using new?
    Do you know how to have the constructor save the value that is passed to it in a class level variable?

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

    Default Re: JFrame repaint()

    Java Code:
    public class A {
    
      public void aMethod() {
      }
    }
    
    
    
    public class B {
    
      private A a;
    
      public B(A a) {
        this.a = a;
      }
    
      public void bMethod() {
        a.aMethod();
      }
    
      public static void main(String[] args) {
        A a = new A();
        B b = new B(a);
        b.bMethod();
      }
    }
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Repaint on a JFRAME
    By Jakers in forum AWT / Swing
    Replies: 6
    Last Post: 01-24-2012, 05:17 PM
  2. Replies: 3
    Last Post: 11-27-2011, 05:18 AM
  3. Replies: 5
    Last Post: 06-14-2010, 06:10 PM
  4. Repaint the entire JFrame (GroupLayout)
    By Willi in forum AWT / Swing
    Replies: 13
    Last Post: 12-19-2009, 10:11 PM
  5. Replies: 4
    Last Post: 04-22-2009, 11:02 PM

Posting Permissions

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