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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 26

Thread: I'm new to JFrame and need some help

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default I'm new to JFrame and need some help

    Basically, to familiarize myself with this class I'm doing my first project, which is drawing the Mendelbrot Set, but before that I have to handle much simpler things;

    If I have a the class
    public class Canvas extends JFrame

    and the functions

        public static void main(String args[])
        {
     
            System.out.println("Running Canvas.java");
            Canvas canvas = new Canvas();
     
        }
     
        public Canvas()
        {
            setSize(width, height);
            setVisible(true);
        }
     
        public void paint(Graphics g)
        {
            g.setColor(Color.yellow);
            g.fillRect(0,0, width, height);
        }

    Who calls the function paint() when I run Canvas.java? Is it re-called every time that it finishes? Because that's what seems to be happening with my code.

    Thank you in advance for your help


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I'm new to JFrame and need some help

    Who calls the function paint()
    What class is the paint() method in? If its in a GUI class, the JVM will call it every time the GUI needs to be drawn or after the code calls repaint().

    Canvas is the name of a Java SE class. You should chose a different name.

    If you are using swing classes, you should override the paintComponent() method, not the paint() method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I'm new to JFrame and need some help

    I'm fresh out of highschool in terms of my java knowledge, so I don't know much yet

    In your post I don't know what these terms are:

    GUI class
    JVM
    Java SE
    swing class


    Also, which name should I give it? I'm used to Canvas because of the project I made in highschool

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I'm new to JFrame and need some help

    Time to build up your vocabulary. Google is a good place to start
    GUI class - a class that is displayed on the monitor like JFrame or JButton
    JVM - Java Virtual Machine. The java program that runs the program
    Java SE - The java Standard Edition packages. See the API doc:
    Java Platform SE 6
    swing class - The newer set of GUI classes (vs AWT)

    I don't know how you can write a java program without seeing these terms.
    which name should I give it?
    Use MyCanvas
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Toldry (August 26th, 2012)

  6. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I'm new to JFrame and need some help

    I might as well post all the code I have so far


    package Mendelbrot;
     
     
    import java.awt.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JFrame;
     
    /**
     *
     * @author 
     */
    public class Canvas extends JFrame
    {
        int width=200;
        int height=200;
        int iterations=4;
     
        Thread thread=new Thread();
     
     
        public static void main(String args[])
        {
     
            System.out.println("Running Canvas.java");
            Canvas canvas = new Canvas();
     
     
        }
     
        public Canvas()
        {
            setSize(width, height);
            setVisible(true);
        }
     
     
        public void paint(Graphics g)
        {
            g.setColor(Color.yellow);
            g.fillRect(0,0, width, height);
     
     
            int[][] grid=render(width, height);
     
            for(int i=0;i<width;i++)
            {
                for(int j=0;j<height;j++)
                {
                    if(grid[i][j]==0)
                    {
                        g.setColor(Color.WHITE);
                    }
                    if(grid[i][j]==1)
                    {
                        g.setColor(Color.BLACK);
                    }
                    g.fillRect(i,j,1,1); //fills a single pixel
                }
            }
     
     
        }
     
     
        /**
         * 
         * @param c complex number
         * @param n how many iterations are used to find the answer
         * @return true if c is part of the mendelbrot set
         */
        public boolean isMend(Z c,int n)
        {
            return isMend(c,n,c);
        }
     
     
        public boolean isMend(Z zn, int n, Z c)
        {
    //        System.out.println(n);
            if(n==0)
            {
                if(zn.getSize()<=2)
                    return true;
                else
                    return false;
            }
     
            Z zn1=Z.sum(Z.multiply(zn,zn),c);
            return isMend(zn1,n-1,c);
        }
     
     
        /**
         * 
         * @param w width
         * @param h height
         * @return an image to be painted
         */
        public int[][] render(int w, int h)
        {
            System.out.println("Render "+w+","+h);
            int[][] grid=new int[w][h];
     
            //Initialization of the grid
            for(int i=0;i<w;i++)
            {
                for(int j=0;j<h;j++)
                {
                    grid[i][j]=0;
                }
            }
     
     
            //Give values
            for(int i=0;i<w;i++)
            {
                for(int j=0;j<h;j++)
                {
                    // -1<x<1  -1<y<1
                    int x=i/100-1;
                    int y=j/100-1;
                    Z c=new Z(x,y);
                    if(isMend(c,iterations))
                    {
                        grid[i][j]=1;
                    }
                    System.out.println("Finished rendering pixel ["+i+","+j+"]");
                }
            }
     
            return grid;
        }
     
    //    public void run()
    //    {
    //        while(true)
    //        {
    //            repaint();
    //            System.out.println("Repainting");
    //
    //
    //            try
    //            {
    //                thread.sleep(2000);
    //            }
    //            catch (InterruptedException ex)
    //            {
    //                Logger.getLogger(Canvas.class.getName()).log(Level.SEVERE, null, ex);
    //            }
    //        }
    //    }
     
     
     
    }


    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package Mendelbrot;
     
    /**
     *
     * @author Alon Eitan
     */
    public class Z //Imaginary Number
    {
        /**
         * Real component
         */
        public double re;
        /**
         * Imaginary component
         */
        public double im;
     
     
     
        /**
         *
         * @param a real component
         * @param b imaginary component
         */
        public Z(double a, double b)
        {
            re=a;
            im=b;
        }
     
     
     
     
        public double getSize()
        {
            return re*re+im*im;
        }
     
        public double getAngle()
        {
            return Math.atan(im/re);
        }
     
     
        public static Z sum(Z a, Z b)
        {
            double re=a.re+b.re;
            double im=a.im+b.im;
            return new Z(re,im);
        }
     
     
        public static Z multiply(Z a,Z b)
        {
            double re=a.re*b.re-(a.im*b.im);
            double im=a.re*b.im+a.im*b.re;
            return new Z(re,im);
        }
     
     
        public double getReal()
        {
            return re;
        }
     
        public double getImaginary()
        {
            return im;
        }
     
        public void setReal(double re)
        {
            this.re = re;
        }
     
        public void setImaginary(double im)
        {
            this.im = im;
        }
     
    }

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I'm new to JFrame and need some help

    What are your questions or problems?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Aug 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I'm new to JFrame and need some help

    Then it's in a GUI class because I can see it on my screen. (which is the entire point of this program)
    How does the JVM determine when the GUI needs to be drawn?

    For some reason the paint() function is being called multiple times.
    It also isn't drawing correctly, but that's a different issue (I think).

  9. #8
    Junior Member
    Join Date
    Aug 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I'm new to JFrame and need some help

    My question is why my code calls the paint() function multiple times

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I'm new to JFrame and need some help

    why my code calls the paint() function multiple times
    How do you know how many times the paint() method is called?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #10
    Junior Member
    Join Date
    Aug 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I'm new to JFrame and need some help

    I put a line in the start of the function paint()

    System.out.println("Painting");

    (not in the version I posted above)

    and it prints that three times

    edit: three times, not twice
    Last edited by Toldry; August 26th, 2012 at 10:44 AM.

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I'm new to JFrame and need some help

    The java program calls the paint()method when it thinks the GUI needs to be redrawn or after the code has called the repaint() method.
    not in the version I posted
    You should post the code you are executing when you ask questions about how it works. Posting an old version makes it harder to help.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #12
    Junior Member
    Join Date
    Aug 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I'm new to JFrame and need some help

    Right... never mind, I managed to solve my problem.

    But I have a new one now regarding threads; I'm trying to activate two threads at the same time, one that repaints every half second and another that renders the image (so that I can see the image progressively getting rendered)

    But for some reason, the moment I activate one thread, it stays in the thread's loop and doesn't do anything else.


    Here are the threads' run() functions:

        public void run()
        {
            while(true) //infinite loop
            {
                repaint();
     
     
                try
                {
                    repainter.sleep(500);
                }
                catch (InterruptedException ex)
                {
                    Logger.getLogger(Canvas.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

       public void run()
        {
            render();
        }

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I'm new to JFrame and need some help

    Can you post more code so we can see the full context of what is happening.

    it stays in the thread's loop and doesn't do anything else.
    while(true) //infinite loop
    Yes that is what I'd expect.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #14
    Junior Member
    Join Date
    Aug 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I'm new to JFrame and need some help

    These are the relevant functions

        public MyCanvas()
        {
            setSize(width, height);
            setVisible(true);
            ren.setWindow(-2.2,1.3,3.4,2.6);
     
            ren.startRendering();
            run();
        }


    Inside MyCanvas:
        public void run()
        {
            while(true)
            {
                repaint();
     
     
                try
                {
                    repainter.sleep(500);
                }
                catch (InterruptedException ex)
                {
                    Logger.getLogger(Canvas.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

    Inside Renderer:

        public void run()
        {
            render();
        }

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I'm new to JFrame and need some help

    Do you have some code I can compile, execute and see the problem? Short pieces of code can leave out something important.

    You normally do not call the run() method. That is done by the thread when it starts executing a Runnable.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #16
    Junior Member
    Join Date
    Aug 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I'm new to JFrame and need some help

    Here's my project
    https://dl.dropbox.com/u/73680434/Simple%20Graphics.rar
    Attached Files Attached Files
    Last edited by Toldry; August 26th, 2012 at 07:52 PM.

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I'm new to JFrame and need some help

    Please post a small program (not the whole project) that compiles, executes and shows the problem.

    I have not way to open a .rar file.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #18
    Junior Member
    Join Date
    Aug 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I'm new to JFrame and need some help

    Here:

    import java.util.logging.Level;
    import java.util.logging.Logger;
     
    public class ThreadProblem
    {
     
     
        public static void main(String[]args)
        {
            ThreadProblem tp=new ThreadProblem();
        }
     
        public ThreadProblem()
        {
            Apples a=new Apples();
            Oranges b=new Oranges();
        }
     
     
     
     
        public class Apples implements Runnable
        {
     
            Thread applesThread=new Thread();
     
            public Apples()
            {
                run();
            }
            public void run()
            {
                while(true)
                {
                    System.out.println("Apples");
                    try
                    {
                        applesThread.sleep(1000);
                    }
                    catch (InterruptedException ex)
                    {
                        Logger.getLogger(ThreadProblem.class.getName()).log(Level.SEVERE, null, ex);
                    }
     
                }
            }
     
        }
     
        public class Oranges implements Runnable
        {
            Thread orangesThread=new Thread();
     
            public Oranges()
            {
                run();
            }
            public void run()
            {
                while(true)
                {
                    System.out.println("Oranges");
                    try
                    {
                        orangesThread.sleep(2000);
                    }
                    catch (InterruptedException ex)
                    {
                        Logger.getLogger(ThreadProblem.class.getName()).log(Level.SEVERE, null, ex);
                    }
     
                }
            }
        }
     
    }

    I want it to print "Apples" every second and "Oranges" every two seconds, but instead it only prints "Apples" every second.

  20. #19
    Junior Member
    Join Date
    Aug 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I'm new to JFrame and need some help

    Unrelated problem:
    Assuming I have a variable that goes from 0 to 1, how do I use it and the class Color to get a rainbow spectrum? (0=red, 1=violet)

  21. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I'm new to JFrame and need some help

    See post #15:
    You normally do not call the run() method. That is done by the thread when it starts executing a Runnable.

    The run() method does not return to the constructor so it can return to its caller so that the next constructor can be called. To see, add lots of println statements to show when a method is entered and when it exits.

    .
    You need to create instances of the Thread class using instances of Apple and Oranges as its Runnable and then call the new thread's start() method.
    If you don't understand my answer, don't ignore it, ask a question.

  22. The Following User Says Thank You to Norm For This Useful Post:

    Toldry (August 27th, 2012)

  23. #21
    Junior Member
    Join Date
    Aug 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I'm new to JFrame and need some help

    Actually what you have done is a wrong appreach. this ll help u i guess

    public class ThreadProblem
    {

    public static void main(String[] arg)
    {
    new ThreadProblem();
    }

    public ThreadProblem()
    {
    Apples a=new Apples();
    Oranges b=new Oranges();
    }

    public class Apples extends Thread
    {

    public Apples()
    {
    start();
    }
    public void run()
    {
    while(true)
    {
    System.out.println("Apples");
    try
    {
    this.sleep(1000);
    }
    catch (InterruptedException ex)
    {
    System.out.println("into catch");
    }

    }
    }

    }

    public class Oranges extends Thread
    {
    public Oranges()
    {
    start();
    }
    public void run()
    {
    while(true)
    {
    System.out.println("Oranges");
    try
    {
    this.sleep(2000);
    }
    catch (InterruptedException ex)
    {
    System.out.println("into orange catch");
    }

    }
    }
    }

    }

  24. #22
    Junior Member
    Join Date
    Aug 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I'm new to JFrame and need some help

    I used your method and now I'm getting "java.lang.IllegalMonitorStateException"

  25. #23
    Junior Member
    Join Date
    Aug 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I'm new to JFrame and need some help

    I managed to find how to make rainbow colors from
    Create a rainbow with GD

    But now I have two new problems (because of the exception I posted above)
    1. My repainter thread doesn't work
    2. I tried adding a MouseListener and a MouseMotionListener to (as of now) draw rectangles, and it isn't working properly either. It's drawing the rectangles too large:


    This is the MouseListener and MouseMotionListener I used
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package Mendelbrot;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.*;
     
    /**
     *
     * @author Alon Eitan
     */
    public class Zoomer implements MouseListener, MouseMotionListener
    {
        int sx,sy; //starting x y
        int cx,cy; //current x y
        Graphics g;
     
        boolean dragging=false;
     
        public Zoomer(Graphics g)
        {
            this.g=g;
        }
     
        public void mouseClicked(MouseEvent me)
        {
        }
     
        public void mousePressed(MouseEvent me)
        {
            if(dragging)
                return;
            sx=me.getX();
            sy=me.getY();
            dragging=true;
        }
     
        public void mouseReleased(MouseEvent me)
        {
            dragging=false;
     
        }
     
        public void mouseEntered(MouseEvent me)
        {
        }
     
        public void mouseExited(MouseEvent me)
        {
        }
     
        public void mouseDragged(MouseEvent me)
        {
            if(!dragging)
                return;
            cx=me.getX();
            cy=me.getY(); 
            g.setColor(Color.white);
            g.drawRect(sx, sy, cx, cy);
        }
     
        public void mouseMoved(MouseEvent me)
        {
        }
     
    }
    It's creating rectangles but not according to the true position of the mouse, but further right and downwards.

  26. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I'm new to JFrame and need some help

    now I'm getting "java.lang.IllegalMonitorStateException"
    Please post the code that is getting the exception and the full text of the error message.
    If you don't understand my answer, don't ignore it, ask a question.

  27. #25
    Junior Member
    Join Date
    Aug 2012
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I'm new to JFrame and need some help

        public class Repainter extends Thread
        {
     
            public void run()
            {
                while (true)
                {
                    repaint();
                    try
                    {
                        this.wait(150);  //<--- exception here
                    }
                    catch (InterruptedException ex)
                    {
                        Logger.getLogger(MyCanvas.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
     
        }



    Exception in thread "Thread-2" java.lang.IllegalMonitorStateException
            at java.lang.Object.wait(Native Method)
            at Mendelbrot.MyCanvas$Repainter.run(MyCanvas.java:83)

Page 1 of 2 12 LastLast

Similar Threads

  1. using objects from one jFrame on a different jFrame
    By Taskeen in forum AWT / Java Swing
    Replies: 3
    Last Post: September 14th, 2011, 08:51 AM
  2. connecting jframe to another jframe
    By ajtambalo in forum Member Introductions
    Replies: 2
    Last Post: May 11th, 2011, 11:24 AM
  3. Pop-out box using a JFrame.
    By ShaunB in forum Java Theory & Questions
    Replies: 6
    Last Post: April 28th, 2011, 12:03 PM
  4. JFrame
    By M3ss1ah in forum AWT / Java Swing
    Replies: 3
    Last Post: February 23rd, 2011, 05:00 PM
  5. JFrame
    By M3ss1ah in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 23rd, 2011, 04:24 PM