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

Thread: Clearing of drawn things from Vectors and also from the screen in Paintprogram

  1. #1
    Junior Member
    Join Date
    Nov 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Clearing of drawn things from Vectors and also from the screen in Paintprogram

    Hello I am writing a Paintprogram and I want to remove all drawn things from the Vectors and also from the screen.

    This is PainterUI.java:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.print.*;
    import javax.swing.*;
    import java.util.*;
    import java.io.*;
    public class PainterUI extends JFrame implements Printable, ActionListener{
        //Components
        JMenuBar mBar = new JMenuBar();
        JToolBar tBar = new JToolBar(JToolBar.HORIZONTAL);
        JMenu Filemenu = new JMenu("File");
        JRadioButton line = new JRadioButton("Line",true);
        JRadioButton rectangle = new JRadioButton("Rectangle",false);
        JRadioButton eclipse = new JRadioButton("Eclipse",false);
        JRadioButton arc = new JRadioButton("Arc",false);
        JMenuItem printfile = new JMenuItem("PrintFile");
        JButton clearall = new JButton("clear all");
     
        PainterView painterview = new PainterView();
     
        public PainterUI(){
            painterview.addMouseListener(l1);
     
            // Layout Components
            setJMenuBar(mBar);
    	getContentPane().add(tBar, BorderLayout.NORTH);
    	getContentPane().add(painterview,BorderLayout.CENTER);
     
            mBar.add(Filemenu);
            Filemenu.add(printfile);
    	tBar.add(line);tBar.addSeparator();tBar.add(rectangle);
            tBar.addSeparator();tBar.add(eclipse);tBar.addSeparator();tBar.add(arc);
            tBar.add(clearall);
    	tBar.setFloatable(false);
     
            ButtonGroup g = new ButtonGroup();
            g.add(line);g.add(rectangle);g.add(eclipse);g.add(arc);
     
            printfile.addActionListener(this);
     
            // Finish Window
            setTitle("DK PaintProgram");
            setSize(900,900);
            setVisible(true);
    	setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
        public void actionPerformed(ActionEvent e){
            if(e.getSource() == printfile){
                PrinterJob pj = PrinterJob.getPrinterJob();
                pj.setPrintable(this);
                if(pj.printDialog()){
                    try{ pj.print();}
                    catch(PrinterException exp){
                        JOptionPane.showMessageDialog(null,exp);
                    }
                }
            }
            if(e.getSource() == clearall){
                painterview.clearall();
            }
        }
        public static void main(String[] args) {
            new PainterUI();
        }
     
        public int print(Graphics g, PageFormat pf, int page)
        throws PrinterException {
     
            if(page > 0 ){ // only one page and page is zerobased 
                return NO_SUCH_PAGE;
            }
     
            Graphics2D g2d = (Graphics2D)g;
            g2d.translate(pf.getImageableX(),pf.getImageableY());
           painterview.printAll(g);
     
            return PAGE_EXISTS; //this 
        }
        MouseListener l1 = new MouseAdapter(){
            Point start;
            Point end;
            public void mousePressed(MouseEvent e){
                start = e.getPoint(); // getPoint when you click your mouse
     
            }
            public void mouseReleased(MouseEvent e){
                end = e.getPoint(); // getPoint when you release your mouse
     
                /* Look for the kind of shape that is selected and add the shape 
                 to the Vector in PainterView.java */
                if(line.isSelected()){
                    painterview.addLine(start,end);
     
                }
                if(rectangle.isSelected()){
                    painterview.addRectangle(start,end);
     
                }
                if(eclipse.isSelected()){
                    painterview.addEclipse(start,end);
     
                }
            }
        };
    }

    this is PainterView.java:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    public class PainterView extends JPanel{
        // The Vectors that keep track off all the shapes
        private Vector<LineModel> Lines = new Vector<LineModel>();
        private Vector<RectangleModel> Rectangles = new Vector<RectangleModel>();
        private Vector<EclipseModel> Eclipses = new Vector<EclipseModel>();
     
        public PainterView(){
    	setBackground(Color.WHITE);
        }
     
        public void paintComponent(Graphics g){
            /* Draw all the shapes that are in the vectors in the Window */ 
            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);
     
            }
            for(ListIterator i= Eclipses.listIterator();i.hasNext();){
                ((EclipseModel) i.next()).draw(g);
     
            }
        }
        // Add the selected shape in PainterUI.java to the right Vector
        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();
        }
        public void addEclipse(Point p1, Point p2){
            Eclipses.add(new EclipseModel(p1, p2));
            repaint();
        }
        // clear all the Vectors
        public void clearall(){
            Lines.clear();
            Rectangles.clear();
            Eclipses.clear();
            repaint();
        }
     
    }

    You see that I call the method clearall() and that I remove all the components from the shape Vector. But it doesn't work . Can somebody give me some tips?

    thanks

    keffie91


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: clear Vectors in PaintProgram

    Quote Originally Posted by keffie91 View Post
    You see that I call the method clearall() and that I remove all the components from the shape Vector.
    I don't see where you add an ActionListener to the 'clear all' button.

    Incidentally, is there some particular reason why you use Vector instead of ArrayList?

  3. #3
    Junior Member
    Join Date
    Nov 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: clear Vectors in PaintProgram

    Sorry I forgot that, Why I use Vector? It is almost the same so I don't the advantages above a Vector. And I don't wan't to change my program
    for the same functionality.

    Thanks!

    keffie91
    Last edited by keffie91; November 15th, 2008 at 06:40 AM.

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: clear Vectors in PaintProgram

    You asked for some tips - one is that unless your collection is being accessed by multiple threads, ArrayList is the preferred choice as it doesn't have the synchronization overhead of Vector.