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: Drawing on a swing frame

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    47
    My Mood
    Amused
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Drawing on a swing frame

    Hello everyone!


    I've recently tried to get swing frames working but the obstacle now is that I want to be able to move a rectangle on the screen.

    My first step was creating a rectangle which got drawn at the start on mouse x and y position:

    import javax.swing.SwingUtilities;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.BorderFactory;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.*; 
    import java.awt.MouseInfo;
     
    public class MainWindowtestagain {
     
     
        public static void main(String[] args){
     
     
     
                    SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
     
     
     
            }
                        private static void createAndShowGUI(){
                  System.out.println("Created GUI on EDT? "+
                   SwingUtilities.isEventDispatchThread());
                   JFrame f = new JFrame("Swing Paint Demo");
                  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  f.add(new MyPanel());
                  f.pack();
                  f.setVisible(true);
                    }
    }
     
     
    class MyPanel extends JPanel {
     
        public MyPanel() {
            setBorder(BorderFactory.createLineBorder(Color.blue));
        }
     
        public Dimension getPreferredSize() {
            return new Dimension(640,480);
        }
     
     
        ////
     
     
        ////
     
     
     
     
     
        public void paintComponent(Graphics g) {
            super.paintComponent(g);    
            int rectx=77;
            int recty=66;
     
            // Draw Text
          //g.drawString("("+xpos+","+ypos+")",xpos,ypos);
     
                PointerInfo a = MouseInfo.getPointerInfo();
    Point b = a.getLocation();
    int x = (int) b.getX();
    int y = (int) b.getY();
            g.setColor(Color.black);
            g.drawString("This is my custom Panel!",10,20);
            g.setColor(Color.red);
            //g.drawRect(x/*x1*/, y/*y1*/, 32/*x2*/, 32 /*x2*/);
            g.drawLine(x, y-32, x+32, y-32);
            g.drawLine(x, y, x, y-32);
            g.drawLine(x+32, y, x+32, y-32);
            g.drawLine(x, y, x+32, y);
     
            g.setColor(Color.cyan);
            g.drawLine(140, 90, 86, 349);
            g.drawString(x+"  "+y,32,16);
     
     
        }
     
        }
        }

    but then I wanted it to happen every step so I tried to create a loop:
    import javax.swing.SwingUtilities;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.BorderFactory;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
     
    import java.awt.*; 
    import java.awt.MouseInfo;
     
    public class MainWindowtestagain {
     
     
        public static void main(String[] args){
     
     
     
                    SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
     
     
     
            }
                        private static void createAndShowGUI(){
                  System.out.println("Created GUI on EDT? "+
                   SwingUtilities.isEventDispatchThread());
                   JFrame f = new JFrame("Swing Paint Demo");
                  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  f.add(new MyPanel());
                  f.pack();
                  f.setVisible(true);
                    }
    }
     
     
    class MyPanel extends JPanel {
     
        public MyPanel() {
            setBorder(BorderFactory.createLineBorder(Color.blue));
        }
     
        public Dimension getPreferredSize() {
            return new Dimension(640,480);
        }
     
     
        ////
     
     
        ////
     
     
     
     
     
        public void paintComponent(Graphics g) {
            super.paintComponent(g);    
            int rectx=77;
            int recty=66;
     
            // Draw Text
          //g.drawString("("+xpos+","+ypos+")",xpos,ypos);
            int ii = 0;
            while (ii < 234823)
            {
                PointerInfo a = MouseInfo.getPointerInfo();
    Point b = a.getLocation();
    int x = (int) b.getX();
    int y = (int) b.getY();
            g.setColor(Color.black);
            g.drawString("This is my custom Panel!",10,20);
            g.setColor(Color.red);
            //g.drawRect(x/*x1*/, y/*y1*/, 32/*x2*/, 32 /*x2*/);
            g.drawLine(x, y-32, x+32, y-32);
            g.drawLine(x, y, x, y-32);
            g.drawLine(x+32, y, x+32, y-32);
            g.drawLine(x, y, x+32, y);
     
            g.setColor(Color.cyan);
            g.drawLine(140, 90, 86, 349);
            g.drawString(x+"  "+y,32,16);
     
            ii+=1;
     
            }
        }
     
        }

    but the program refused to show the images until i closed it, must be some sort of lag.

    What should I do?


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

    Default Re: Drawing on a swing frame

    The code in the paintComponent() method should draw what its been told to draw and then exit as quickly as possible for the GUI thread to then get control back and make what was drawn visible on the screen.
    There should NOT be a long running loop in the paintComponent() method holding up returning control to the GUI drawing code in the jvm.

    See the tutorial about custom painting:Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Swing, Hexagons, and odd drawing O MY
    By Souls512 in forum AWT / Java Swing
    Replies: 5
    Last Post: April 8th, 2012, 10:48 PM
  2. Java Frame Buffer Line drawing
    By tcmei in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 17th, 2011, 06:55 AM
  3. Drawing a selection box using swing
    By edi233 in forum AWT / Java Swing
    Replies: 4
    Last Post: May 9th, 2011, 09:50 AM
  4. IE browser in Java swing/AWT Frame
    By gafaec in forum AWT / Java Swing
    Replies: 0
    Last Post: April 10th, 2011, 05:16 AM
  5. Drawing image on JPanel from another frame
    By jeryslo in forum AWT / Java Swing
    Replies: 3
    Last Post: December 8th, 2009, 04:01 PM