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