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 a square in a window

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

    Post Drawing a square in a window

    Hello everyone!

    I am trying to get the hold on Jframes and I can so far create a simple window:
    package javvv;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    //package javvv;
     
    public class Javvv {
    /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("FrameDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            JLabel emptyLabel = new JLabel("");
            emptyLabel.setPreferredSize(new Dimension(640, 480));
            frame.getContentPane().add(emptyLabel, BorderLayout.CENTER);
     
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
     
       public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
     
     
     
     
     
     
     
     
     
                }
            });
        }
     
     
    //Ignore this for now.
     
       public void paint (Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
     
        int x = 0;
        int y = 0;
     
     
     
    }
    }

    And that works fine.
    But I want to be able to draw for example a rectangle on it so I tried to create a class for that which takes the inputs of x, y and the sides.

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package Square_class;
     
    import javax.swing.JFrame;
     
     
     
    public class Square {
        private int x;     
        private int y;     
        private int side1;  
        private int side2; 
     
        /**The variables for the rectangle
         */
        public Square(int x, int y, int side) {
            this.x = x;
            this.y = y;
            this.side1 = side1;
            this.side2 = side2;
        }
        /* Draw the rectangle in the window*/
        public void draw(JFrame frame){
            frame.g2.draw(new Rectangle2D.Double(x, y,
                                   side1,
                                   side2));
     
        }
     
     
    }
    NOTE: I took the code for creating a rectangle from a websire.

    But I dont know what to do from now on.
    I want to be able to just write, after I have called the Square class:

    Square(the x-position,the y-position, the side1, the side2)


    My code might be a bit messy... But I hope you understand what I need help with.



    thank you in advance!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,199
    Thanks
    49
    Thanked 2,174 Times in 2,147 Posts

    Default Re: Drawing a square in a window

    You should look at the tutorial on custom drawing:
    Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Bascially define a class that extends JPanel, add an instance of the class to the jframe, override the jpanel's paintComponent() method and do the drawing there.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Applet viewer window is diplaying in fron of the current window every time
    By jsreddy99 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 24th, 2013, 06:16 AM
  2. Drawing a square with stars and dots
    By steph.harrison in forum Java Theory & Questions
    Replies: 1
    Last Post: October 27th, 2012, 09:32 AM
  3. Replies: 2
    Last Post: August 27th, 2012, 09:19 PM
  4. Replies: 1
    Last Post: December 17th, 2011, 02:32 AM
  5. URgh, drawing a square
    By BITmixit in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 25th, 2011, 12:49 PM

Tags for this Thread