Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How do I access frame class?

  1. #1
    Junior Member
    Join Date
    Sep 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I access frame class?

    Program.java
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.lang.*;
    import javax.swing.*;
     
    public class Program{
     
        public static void main(String[] args) {
     
    		Main main = new Main();
    		JFrame frame = new JFrame("Title");
    		frame.add(main);
    		frame.setVisible(true);
    	        frame.pack();
    		main.setup();
    		main.update();
     
        }
     
    }

    Main.java
    import java.awt.Graphics;
    import java.util.concurrent.*;
    import java.lang.*;
    import javax.swing.*;
    import java.awt.Dimension;
     
    public class Main extends JPanel{
     
    	public void setup(){
     
    	}
     
    	public void update(){
     
    		while(true){
     
    			try{
    			TimeUnit.MILLISECONDS.sleep(16);
    			frame.repaint();
    			}catch(Exception E){}
    		}
     
    	}
     
    	public void draw(Graphics g){
     
     
    	}
     
    	public Dimension getPreferredSize() {
     
            return new Dimension(500, 500);
     
        }
     
    	//override stuff
    	public void paintComponent(Graphics g) {
     
            super.paintComponent(g);
    		draw(g);
     
        }
     
    }

    the problem is that I cannot access frame.repaint() in Main.java, how do I fix it? I prefer simple, beginner friendly looking code, not making AAA game.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How do I access frame class?

    The update class could get access to the frame variable by passing frame in the call to update:
      main.update(frame);  // pass ref to frame to method
    The update method will need to be rewritten to accept frame.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java Inner Class Access
    By sanjay_d in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 13th, 2013, 06:50 AM
  2. set frame title from another class
    By arvinder in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 20th, 2013, 11:17 PM
  3. Why is a Frame Not Showing Up When I Run This Class?
    By Ormolu611 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: April 7th, 2013, 05:22 PM
  4. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  5. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 05:20 PM

Tags for this Thread