Register FAQ Calendar Search Today's Posts Mark Forums Read

Go Back   Java Programming Forums > Java Programming > Advanced Java

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 14-11-2008, 06:13 AM
Junior Member
 
Join Date: Nov 2008
Posts: 2
keffie91 is on a distinguished road
Default clear Vectors 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:
Java Code:
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:
Java Code:
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
Reply With Quote
  #2 (permalink)  
Old 14-11-2008, 07:55 PM
Junior Member
 
Join Date: Nov 2008
Posts: 8
dlorde is on a distinguished road
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?
Reply With Quote
  #3 (permalink)  
Old 15-11-2008, 11:36 AM
Junior Member
 
Join Date: Nov 2008
Posts: 2
keffie91 is on a distinguished road
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; 15-11-2008 at 11:40 AM.
Reply With Quote
  #4 (permalink)  
Old 15-11-2008, 12:17 PM
Junior Member
 
Join Date: Nov 2008
Posts: 8
dlorde is on a distinguished road
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.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Custom Search
100 most searched terms
Search Cloud
"bad endpoint type" com.sun.xml.internal.messaging.saaj.soapexceptionimpl com.sun.xml.internal.messaging.saaj.soapexceptionimpl: bad endpoint type com.sun.xml.internal.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception com.sun.xml.internal.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: message send failed com.sun.xml.internal.messaging.saaj.soapexceptionimpl: message send failed com.sun.xml.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception com.sun.xml.messaging.saaj.soapexceptionimpl: java.security.privilegedactionexception: com.sun.xml.messaging.saaj.soapexceptionimpl: message send failed convert arraylist to map date_format_now eclipse shortcut keys ejb3 quartz java convert double to binary java forum java forums java jtextarea bold java jtextarea color java jtextarea font java jtextarea font size java programmer forum java programmers forum java programming forum java programming forums java read last line java read last line of file java sendkeys java.security.privilegedactionexception java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: bad response java.security.privilegedactionexception: com.sun.xml.internal.messaging.saaj.soapexceptionimpl: message send failed java.security.privilegedactionexception: com.sun.xml.messaging.saaj.soapexceptionimpl: message send failed javapf javaprogrammingforums jtextarea bold jtextarea font jtextarea font color jtextarea font size programing forums programming forums reset jcombobox saaj0008 saaj0008: bad response; bad request saaj0008: bad response; not found severe: saaj0008 severe: saaj0008: bad response; bad request severe: saaj0008: bad response; not found soap java.security.privilegedactionexception soapexceptionimpl: bad endpoint type textpad java

All times are GMT. The time now is 01:11 PM.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.