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 7 of 7

Thread: Problem with a simple drawing program

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with a simple drawing program

    Hi!

    I'm creating a program that is going to work as "paint". I have tried to make it possible to press/hold the mouse from one point and release it on another point.
    Between these points I'm trying to draw a line, though it does'nt work as I want to. If I try to draw a line, nothing happens. BUT! if I change the size of the whole program window, the line I've just drawn will appear.

    I want this line to appear without having to change the size of the window... what have I made wrong???
    Can someone please tell me what I've done wrong?


    package Projekt;
     
    public class Main {
        public static void main(String[] args){
            Window window = new Window();
            window.setSize(1000,800);
            window.setVisible(true);
        }
    }
     
     
    //Second class
    package Projekt;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
     
    public class Window extends JFrame{
     
        public int x;
        public int y;
        public int x_2;
        public int y_2;
        public JPanel p1;
        public JButton button;
        public JButton button2;
        public Painting temp;
        public Vector vector = new Vector();
     
        public Window(){
     
            super("Ritprogram");
     
            p1 = new JPanel();
            p1.setSize(1000, 100);
            p1.setBackground(Color.WHITE);
            button = new JButton("Save");
            button2 = new JButton("Open");
            temp = new Painting(0,0,0,0);
     
            getContentPane().add(p1, "North");
            getContentPane().add(temp, "Center");
            p1.add(button);
            p1.add(button2);
     
            Listener myListener = new Listener();
            temp.addMouseListener(myListener);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
     
            pack();
        }
     
        private class Listener implements MouseListener, ActionListener{
            public void mouseClicked(MouseEvent e) {        
            }
            public void mousePressed(MouseEvent e) {
                x = e.getX();
                y = e.getY();
            }
            public void mouseReleased(MouseEvent e) {
                x_2 = e.getX();
                y_2 = e.getY();
     
                temp = new Ritmetoder(x, x_2, y, y_2);
                vector.add(temp);
                getContentPane().add(temp, "Center");
                repaint();              
            }
            public void mouseEntered(MouseEvent e) {         
            }
            public void mouseExited(MouseEvent e) {
            }
            public void actionPerformed(ActionEvent ae){
            }
        }
    }
     
     
    //Third Class
    package Projekt;
     
    import java.awt.*;
    import javax.swing.*;
     
    public class Painting extends JPanel{
     
        public int x;
        public int y;
        public int x_2;
        public int y_2; 
     
        public Painting(int x1, int x2, int y1, int y2){
        x = x1;
        x_2 = x2;
        y = y1;
        y_2 = y2;
        setBackground(Color.GRAY);
        setPreferredSize(new Dimension(800, 800));
        }
     
        public void paintComponent(Graphics g){
            g.drawLine(x, y, x_2, y_2);
        }    
    }
    Last edited by Saiimon; May 24th, 2012 at 02:34 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem with a simple drawing program

    You are attempting to add components to a container after it has been realized (in this case, after the JFrame is visible). Either add it before, or call revalidate on the JPanel or validate on the JFrame. I am not sure what you full end goal is, but you might consider a redesign in which your panel is first added, and the mouse events create objects that are drawn by that panel (as opposed to the mouse events creating new Panels)
    Last edited by copeg; May 24th, 2012 at 03:00 PM. Reason: clarification

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with a simple drawing program

    I have just tried to follow the designs that we're using in our books from school.

    I didn't understand what you ment by call revalidate on the JFrame. I'm a beginner at this and all I have to my help is my book...
    Could you try to explain better?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem with a simple drawing program

    Quote Originally Posted by Saiimon View Post
    I didn't understand what you ment by call revalidate on the JFrame. I'm a beginner at this and all I have to my help is my book...
    Could you try to explain better?
    I would recommend expanding your resources beyond just your book. In fact, the java API should be at the top of your list to become familiar with. The JFrame class has a method named validate (it is a method defined in Container - a parent class of JFrame and inherited by JFrame). To quote the API:
    The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.
    You add things to a container after its displayed - call validate after you do so.

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with a simple drawing program

    So i should write it like this instead?? Because it seems to work

    public void mouseReleased(MouseEvent e) {
                x_2 = e.getX();
                y_2 = e.getY();
     
                temp = new Painting(x, x_2, y, y_2);
                vector.add(temp);
                getContentPane().add(temp, "Center");
                validate();
                repaint();
            }

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Problem with a simple drawing program

    Quote Originally Posted by Saiimon View Post
    So i should write it like this instead?? Because it seems to work
    Yes, presuming you do not wish to redesign as I recommended above.

  7. #7
    Junior Member
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with a simple drawing program

    Sorry... I will be watching tutorials and searching for more info about; JFrame, Javax.swing and Java API. My problem is that the program I'm trying to make is going to be my final project in my programming course. And since we haven't made ANYTHING AT ALL in JFrame and such, the teacher will regard all minor design issues and such.

    Therefore I just need to be able to make a program that is having some kind of graphics... And this will probably be enough

    Thanks for your help
    I am now able to continue with my project

Similar Threads

  1. Drawing activation stack for program with Exception
    By InfiinteSound in forum Exceptions
    Replies: 9
    Last Post: March 4th, 2012, 03:16 PM
  2. [SOLVED] problem in simple program, new user
    By musterdplug12 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2011, 04:11 PM
  3. [SOLVED] Really simple program problem
    By Jacksontbh in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 29th, 2011, 10:15 PM
  4. Image Drawing Problem
    By Matta in forum AWT / Java Swing
    Replies: 8
    Last Post: June 11th, 2011, 06:06 AM
  5. Line Drawing Program
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 13th, 2010, 03:54 PM