Results 1 to 10 of 10

Thread: PaintProgram

  1. #1
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default PaintProgram

    Hello I am developing a simple PaintProgram.
    Main.java
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    public class Main extends JFrame{
        JMenuBar mBar = new JMenuBar();
        JToolBar tBar = new JToolBar(JToolBar.HORIZONTAL);
        
        JMenu Filemenu = new JMenu("File");
         public JRadioButton line = new JRadioButton("Line");
        JRadioButton rectangle = new JRadioButton("Rectangle");
        JRadioButton eclipse = new JRadioButton("Eclipse");
        JRadioButton arc = new JRadioButton("Arc");
        public Main(){
            setTitle("DK PaintProgram");
            setJMenuBar(mBar);
    	getContentPane().add(tBar, BorderLayout.NORTH);
    	getContentPane().add(new Painter(),BorderLayout.CENTER);
            
            mBar.add(Filemenu);
            
    	tBar.add(line);tBar.addSeparator();tBar.add(rectangle);
            tBar.addSeparator();tBar.add(eclipse);tBar.addSeparator();tBar.add(arc);
    	tBar.setFloatable(false);
            
            ButtonGroup g = new ButtonGroup();
            g.add(line);g.add(rectangle);g.add(eclipse);g.add(arc);
            
            setSize(900,900);
            setVisible(true);
    	setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
        public static void main(String[] args) {
            new Main();
        }
    
    }
    Painter.java
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    public class Painter extends JPanel {
    	private Vector Lines = new Vector();
            private Vector Rectangles = new Vector();
    	private Line newL;
            private Rectangle newR;
            
            public Painter(){
    		setBackground(Color.WHITE);
    		addMouseListener(l1);
            }
    
            public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		for(ListIterator i=Lines.listIterator();i.hasNext();){
    			((Line) i.next()).draw(g);
    			g.setColor(Color.black);
    		}
    	}
    	MouseListener l1 = new MouseAdapter(){
    			public void mousePressed(MouseEvent e){
    				newL = new Line(e.getPoint(),e.getPoint());
    			}
    			public void mouseReleased(MouseEvent e){
    				newL.p2 = e.getPoint();
    				Lines.add(newL);
    				repaint();
    			}
            };
    }
    RectanglePainter.java
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    public class RectanglePainter extends JPanel {
    	private Vector Rectangles = new Vector();
    	private Rectangle newR;
    	
    	public RectanglePainter(){
    		setBackground(Color.WHITE);
    		addMouseListener(l1);
    	}
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		for(ListIterator i=Rectangles.listIterator();i.hasNext();){
    			((Rectangle) i.next()).draw(g);
    			
    		}
    	}
    	MouseListener l1 = new MouseAdapter(){
    		public void mousePressed(MouseEvent e){
    			newR = new Rectangle(e.getPoint(),e.getPoint());
    		}
    		public void mouseReleased(MouseEvent e){
    			newR.p2 = e.getPoint();
    			Rectangles.add(newR);
    			repaint();
    		}
    	};
    	
    }
    Rectangle.java
    Java Code:
    import java.awt.*;
    import java.io.*;
    public class Rectangle implements Serializable {
    	public Point p1, p2;
    	
    	public Rectangle(){
    		p1 = new Point();
    		p2 = new Point();
    	}
    	public Rectangle(Point a, Point b){
    		p1 = new Point(a);
    		p2 = new Point(b);
    	}
    	public void draw(Graphics g){
    		g.drawRect(p1.x,p1.y,p2.x-p1.x,p2.y-p1.y);
    	}
    }
    Line.java
    Java Code:
    import java.awt.*;
    import java.io.*;
    public class Line implements Serializable {
    	public Point p1, p2;
    	
    	public Line(){
    		p1 = new Point();
    		p2 = new Point();
    	}
    	public Line(Point a, Point b){
    		p1 = new Point(a);
    		p2 = new Point(b);
    	}
    	public void draw(Graphics g){
    		g.drawLine(p1.x, p1.y, p2.x, p2.y);
    	}
    }
    I want to merge Painter and RectanglePainter so that you You can choose from the RadioButtonGroup what you want to draw. I thaught of an Actionlistener inside Painter so that it see when you want to draw a line or a rectangle.

    My question is: Is it possible to merge Painter and RectanglePainter in Painter and can I do that with an actionlistener inside Painter?

    if not, please give some possibilities of how i can do it without an actionlistener.

    thanks keffie91

    sorry if I post in the wrong topic.
    Never give up! ;)

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    It'd be easier to work with the code if all of it was in a single file.

    What do these classes you've posted do? Can you comment them so those reading the code know what they do?

    In case you haven't read that part of the text yet, comments can be in included inside of /* comment */
    or at the end of a line following // comment
    Last edited by Norm; 10-19-2008 at 04:22 PM.

  3. #3
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default

    This is the code again with comments, I work with seperate files because that is transparent for me.

    This is the code:
    Main.java
    Painter.java
    RectanglePainter.java
    Rectangle.java
    Line.java

    note that I already started to merge RectanglePainter and Painter but I don't know how to go further.

    keffie91
    Never give up! ;)

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Separate might be easier for you, but it means I have to download 5 files which is 4 too many.

    Plus when I copy the code I get numbers which cause compile errors:
    1.
    import javax.swing.*;
    2.
    import java.awt.*;
    3.
    ...
    Last edited by Norm; 10-19-2008 at 06:20 PM.

  5. #5
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default

    all the code

    Are you happy now?
    Never give up! ;)

  6. #6
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default

    What is wrong with importing unused packages? And they aren't not used.
    I can run the program normally without errors.
    Never give up! ;)

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    I get the following when I select, copy and paste:
    1.
    import java.awt.*;
    2.
    import javax.swing.*;
    3.
    public class Main extends JFrame{
    4.
    ...
    I can't compile it with the line numbers!

    Remember you have an advantage over me. I only understand English. The instructions on the site where you posted the code is not English and I skipped over it. I found the link to download the code as .txt and will take a look at it. See below for my comments. It be nice if it were all in one place. Your questions are here and the source is there.

    What is wrong with importing unused packages
    Do you get compiler error messages? Please copy and paste them here.

    Java Code:
    /* >>> This program is to ......        including lines rectangles, arcs and eclipse ...
      type chosen by Radio Buttons ...
    
    
    Things to DO: add action listeners for buttons, merge Painter and RectanglePainter code
    Design logic and comment code.
    */
    
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.awt.event.*;
    
    public class PaintProgram2 extends JFrame{
        //Main Program that represents the GUI
        JMenuBar mBar = new JMenuBar();
        JToolBar tBar = new JToolBar(JToolBar.HORIZONTAL);
        
        JMenu Filemenu = new JMenu("File");
         public JRadioButton line = new JRadioButton("Line");
        JRadioButton rectangle = new JRadioButton("Rectangle");
        JRadioButton eclipse = new JRadioButton("Eclipse");
        JRadioButton arc = new JRadioButton("Arc");
    
        public PaintProgram2(){
            setTitle("DK PaintProgram");
            setJMenuBar(mBar);
    	getContentPane().add(tBar, BorderLayout.NORTH);
    	getContentPane().add(new Painter(),BorderLayout.CENTER);
            
            mBar.add(Filemenu);
            
    	tBar.add(line);tBar.addSeparator();tBar.add(rectangle);
            tBar.addSeparator();tBar.add(eclipse);tBar.addSeparator();tBar.add(arc);
    	tBar.setFloatable(false);
            
            ButtonGroup g = new ButtonGroup();
            g.add(line);g.add(rectangle);g.add(eclipse);g.add(arc);
            
    //        setSize(900,900);
            setSize(600,400);   // leave room to see console beneath window
            setVisible(true);
    	setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
        // Start here ------------------------------------
        public static void main(String[] args) {
            new PaintProgram2();
        }
    
    
    // >>> What is this unused class for?
    class Painter extends JPanel {
    	private Vector Lines = new Vector(); /* keep track of all the lines that are drawn */
            private Vector Rectangles = new Vector();/* keep track of all the rectangles that are drawn*/
    	private Line newL; // Instantiate a new Line see Line.java
            private Rectangle newR;// Instantiate a new Rectangle see Rectangle.java
            
            public Painter(){
    		setBackground(Color.WHITE);
    		addMouseListener(l1);
            }
    
            public void paintComponent(Graphics g){
                /* draw all the lines in the Vector */
    		super.paintComponent(g);
    		for(ListIterator i=Lines.listIterator();i.hasNext();){
    			((Line) i.next()).draw(g);
    			
                    }
    	}
       // >>> What does this listener do?
    	MouseListener l1 = new MouseAdapter(){
    			public void mousePressed(MouseEvent e){
    				newL = new Line(e.getPoint(),e.getPoint());/*
                                     start a new Line and get the point where you
                                     pressed the mouseButton*/
    			}
    			public void mouseReleased(MouseEvent e){
    				newL.p2 = e.getPoint();/* get the point where you released the mouse*/
    				Lines.add(newL);/*add the new line to the 
                                     vector so that it can keep track of it*/
    				repaint();/* draw all the lines again */
    			}
            };
    }
    
    // >>> What is this unused class for?
    class RectanglePainter extends JPanel {
    	private Vector Rectangles = new Vector();/* Vector to keep track of 
             all the rectangles that are drawn*/
    	private Rectangle newR;// Instantiate a new Rectangle see Rectangle.java
    	
    	public RectanglePainter(){
    		setBackground(Color.WHITE);
    		addMouseListener(l1);
    	}
    	public void paintComponent(Graphics g){
                /* draw all the rectangles on the screen */
    		super.paintComponent(g);
    		for(ListIterator i=Rectangles.listIterator();i.hasNext();){
    			((Rectangle) i.next()).draw(g);
    			
    		}
    	}
       // >>> What does this listener do?
       // >> For example:
       // Use initial mouse press point as the upper left corner of a rectangle 
       // and the release point for the lower right cornner of a rectangle.
       // The rectangle is saved in Rectangles
    	MouseListener l1 = new MouseAdapter(){
    		public void mousePressed(MouseEvent e){
    			newR = new Rectangle(e.getPoint(),e.getPoint());/*
                                     start a new Rectangle and get the point where you
                                     pressed the mouseButton*/
    		}
    		public void mouseReleased(MouseEvent e){
    			newR.p2 = e.getPoint();/* get the point where
                                     you released the mouse*/
    			Rectangles.add(newR);/*add the new Rectangle to the 
                                     vector so that it can keep track of it*/
    			repaint();/*repaint, draw all the lines again */
    		}
    	};
    	
    }
    
    //>>> What does this class do?
    // FOR EXAMPLE:
    // Hold the end points for a line to be drawn by paintComponent in Painter
    class Line { //implements Serializable {
    	public Point p1, p2;/* new Points, the points where you pressed and 
             released the mouse*/
    	
    	public Line(){
    		p1 = new Point();
    		p2 = new Point();
    	}
    	public Line(Point a, Point b){
    		p1 = new Point(a);
    		p2 = new Point(b);
    	}
    	public void draw(Graphics g){
                //draw the line 
                g.drawLine(p1.x, p1.y, p2.x, p2.y);
    	}
    }
    
    class Rectangle {// implements Serializable {
    	public Point p1, p2;/* new Points, the points where you pressed and 
             released the mouse*/
    	
    	public Rectangle(){
    		p1 = new Point();
    		p2 = new Point();
    	}
    	public Rectangle(Point a, Point b){
    		p1 = new Point(a);
    		p2 = new Point(b);
    	}
    	public void draw(Graphics g){
                //draw the rectangle
                g.drawRect(p1.x,p1.y,p2.x-p1.x,p2.y-p1.y);
    	}
    }
    } // end class
    Last edited by Norm; 10-19-2008 at 10:29 PM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I agree that you should import only the specific classes that are used since if you don't this can be a source of pernicious and hard to debug errors. Other suggestions:
    1) Don't use Vectors, use a Collections such as ArrayLists.
    2) Use the generic version of ArrayList.
    3) Have the ArrayList hold Shape objects which can be Line2D, Rectangle2D, ....

  9. #9
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    I want to merge Painter and RectanglePainter so that you You can choose from the RadioButtonGroup what you want to draw.
    Okay.

    I thaught of an Actionlistener inside Painter so that it see when you want to draw a line or a rectangle.
    There are many ways to put these kind of things together. You could have Painter listen for changes in/to the radioButton state in Main but it would be better to have Painter, as a graphics component, do graphics/drawing only and not be concerned with event code. Keep the event code in another class such as Main and communicate with the Painter class from this event–code–containing class.

    My question is: Is it possible to merge Painter and RectanglePainter in Painter and can I do that with an actionlistener inside Painter?
    Yes, you probably could. I suggest a slightly different approach: set up your graphics component (Painter) so that at any moment it can draw its current state, viz, the objects in its Vectors. You can add more state such as boolean flags to further control what is drawn and set/change these flags from your event code as required. The idea is that the graphics component can be configured according to user–input received/gathered in your controller/event–code class and draw what the user wants.

    if not, please give some possibilities of how i can do it without an actionlistener.
    Okay, here's one way you could do something like this in java.
    I put all classes in a single file because I'm lazy and changed the names so you can run it as–is without name–clashing.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.Serializable;
    import java.util.*;
    import javax.swing.*;
    
    public class PainterUI extends JFrame{
        JMenuBar mBar = new JMenuBar();
        JToolBar tBar = new JToolBar(JToolBar.HORIZONTAL);
        
        JMenu Filemenu = new JMenu("File");
        JRadioButton line = new JRadioButton("Line");
        JRadioButton rectangle = new JRadioButton("Rectangle");
        JRadioButton eclipse = new JRadioButton("Eclipse");
        JRadioButton arc = new JRadioButton("Arc");
    
        PainterView painterView = new PainterView();
    
        public PainterUI(){
            painterView.addMouseListener(l1);
            setTitle("DK PaintProgram");
            setJMenuBar(mBar);
    	getContentPane().add(tBar, BorderLayout.NORTH);
    	getContentPane().add(painterView,BorderLayout.CENTER);
            
            mBar.add(Filemenu);
            
    	tBar.add(line);tBar.addSeparator();tBar.add(rectangle);
            tBar.addSeparator();tBar.add(eclipse);tBar.addSeparator();tBar.add(arc);
    	tBar.setFloatable(false);
            
            ButtonGroup g = new ButtonGroup();
            g.add(line);g.add(rectangle);g.add(eclipse);g.add(arc);
            
            setSize(900,600);
            setVisible(true);
    	setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
            new PainterUI();
        }
    
        // Keep event code separate from graphics code.
        // It is easy to encapsulate it here in your gui class
        // but you can just as easily encapsulate in a separate
        // class by itself or with other event code. The general
        // guideline is that it makes sense to you and is easy
        // for others to follow and understand.
        MouseListener l1 = new MouseAdapter(){
            Point start;
    
            public void mousePressed(MouseEvent e){
                start = e.getPoint();
            }
    
            public void mouseReleased(MouseEvent e){
                Point end = e.getPoint();
                if(line.isSelected()) {
                    painterView.addLine(start, end);
                }
                if(rectangle.isSelected()) {
                    painterView.addRectangle(start, end);
                }
            }
        };
    }
    
    class PainterView extends JPanel {
        // Generic parameters used in j2se 1.5+
        private Vector<LineModel> Lines = new Vector<LineModel>();
        private Vector<RectangleModel> Rectangles = new Vector<RectangleModel>();
    
        public PainterView(){
            setBackground(Color.WHITE);
        }
    
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            for(ListIterator i=Lines.listIterator();i.hasNext();){
                ((LineModel) i.next()).draw(g);
            }
            for(ListIterator i=Rectangles.listIterator();i.hasNext();){
                ((RectangleModel) i.next()).draw(g);
            }
        }
    
        // Here's how your event code communicates
        // with (this) graphics component.
        public void addLine(Point p1, Point p2) {
            Lines.add(new LineModel(p1, p2));
            repaint();
        }
    
        public void addRectangle(Point p1, Point p2) {
            Rectangles.add(new RectangleModel(p1, p2));
            repaint();
        }
    }
    
    class RectangleModel implements Serializable {
        public Point p1, p2;
    
        public RectangleModel(){
            p1 = new Point();
            p2 = new Point();
        }
    
        public RectangleModel(Point a, Point b){
            p1 = new Point(a);
            p2 = new Point(b);
        }
    
        public void draw(Graphics g){
            g.drawRect(p1.x,p1.y,p2.x-p1.x,p2.y-p1.y);
        }
    }
    
    class LineModel implements Serializable {
        public Point p1, p2;
    
        public LineModel(){
            p1 = new Point();
            p2 = new Point();
        }
    
        public LineModel(Point a, Point b){
            p1 = new Point(a);
            p2 = new Point(b);
        }
    
        public void draw(Graphics g){
            g.drawLine(p1.x, p1.y, p2.x, p2.y);
        }
    }

  10. #10
    keffie91's Avatar
    keffie91 is offline Member
    Join Date
    Jun 2008
    Location
    The Netherlands
    Posts
    35
    Blog Entries
    1
    Rep Power
    0

    Default

    Thanks for the reactions, I have changed the code and it works. The next time I write a program now, I'm gonna try to write good comment between my code. I'm gonna add printing functionality.

    Thanks keffie91
    Never give up! ;)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •