Results 1 to 2 of 2
  1. #1
    Chetanji is offline Member
    Join Date
    Apr 2008
    Posts
    1
    Rep Power
    0

    Default FreeHand Draws poorly

    HI to All,
    I changed the hashmap to an ArrayList and was rewarded with better counter use and deleting and printing. But one drawback is the draw is far too slow for the mouse drag.
    I know the ArrayList comes with its own index, but as you can see I deleted the string part of the hashmap's use, but this leaves an additional index.

    Does this make any sense?
    Can someone give me an idea what direction to move in.

    I have tried ArrayList<Points> but get lost quickly.

    I think this is a typical recode from Hashmap to ArrayList.

    This code is executable.

    Thanks
    Chetanji:)

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.RenderingHints;
    import java.awt.geom.Line2D;
    import java.awt.image.BufferedImage;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    import java.util.ArrayList;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSlider;
    import javax.swing.SwingUtilities;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    import javax.swing.event.MouseInputAdapter;
    
    public class FreeHandPanel extends JPanel {
        int FreeHandCount = 0;
        BufferedImage image;
        FreeHandDraw freehand = new FreeHandDraw();
        Color color;
    
        public FreeHandPanel() {
            addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                freehand.setNewFreeHand(true);
                freehand.setFreehandX(e.getX());
                freehand.setFreehandY(e.getY());
    	    arrFreeHand.add(FreeHandCount, freehand);
        }
        public void mouseReleased(MouseEvent e) {
            repaint();
    	FreeHandCount++;
        }
        });
        addMouseMotionListener(new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
                FreeHandDraw free = (FreeHandDraw) arrFreeHand.get(FreeHandCount);
    	    free.setFreehandX(e.getX());
    	    free.setFreehandY(e.getY());
    	    repaint();
            }
         );
         }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
    	g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    				RenderingHints.VALUE_ANTIALIAS_ON);
    	g2.setPaint(color);
            freehand.drawme(g);
    
        }
    
    
        public static void main(String[] args){
            SwingUtilities.invokeLater(new Runnable(){
        	    public void run(){
        	        FreeHandPanel free = new FreeHandPanel();
        		JFrame f = new JFrame();
        		f.getContentPane().add(free);
        		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        		f.setSize(700, 700);
        		f.setLocationRelativeTo(null); // center
        		f.setVisible(true);
        	    }
        	});
        }
    
        public ArrayList arrFreeHand = new ArrayList();
        ArrayList xmlFreeHand = new ArrayList();
        
    public class FreeHandDraw {
    
        int[] freehandX = new int[10000];
    
        int[] freehandY = new int[10000];
    
        boolean newFreeHand = false;
    
        int index = 0;
    
        Color color;
    
        public void drawme(Graphics g) {
            g.setColor(getColor());
    	for (int i = 0; i < index - 1; i++) {
    	    int x = freehandX[i];
    	    int y = freehandY[i];
    	    int x2 = freehandX[i + 1];
    	    int y2 = freehandY[i + 1];
    	    g.drawLine(x, y, x2, y2);
    	}
        }
    
        public void setColor(Color str) {
    	color = str;
        }
    
        public Color getColor() {
    	return color;
        }
    
       /**
        * @return
        */
        public int getIndex() {
            return index;
       }
    
       /**
        * @param is
        */
        public void setFreehandX(int is) {
            freehandX[index] = is;
        }
    
       /**
        * @param is
        */
        public void setFreehandY(int is) {
            freehandY[index++] = is;
        }
    
       /**
        * @param i
        */
        public void setIndex(int i) {
            index = i;
        }
    
        public boolean isNewFreeHand() {
    	return newFreeHand;
        }
    
        public void setNewFreeHand(boolean newFreeHand) {
    	this.newFreeHand = newFreeHand;
        }
    }
    
    }
    Last edited by Chetanji; 04-18-2008 at 10:05 AM.

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

    Default

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.RenderingHints;
    import java.awt.geom.Line2D;
    import java.awt.image.BufferedImage;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionAdapter;
    import java.util.ArrayList;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JSlider;
    import javax.swing.SwingUtilities;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    import javax.swing.event.MouseInputAdapter;
    
    public class FHP extends JPanel {
    //    int FreeHandCount = 0;
    //    BufferedImage image;
        FreeHandDraw freehand = new FreeHandDraw();
        Color color;
        public ArrayList<FreeHandDraw> arrFreeHand = new ArrayList<FreeHandDraw>();
    //    ArrayList xmlFreeHand = new ArrayList();
    
        public FHP() {
            addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    freehand.setNewFreeHand(true);
                    freehand.setFreehandX(e.getX());
                    freehand.setFreehandY(e.getY());
    	        arrFreeHand.add(freehand);
                }
    
                public void mouseReleased(MouseEvent e) {
                    repaint();
    //                FreeHandCount++;
                }
            });
    
            addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseDragged(MouseEvent e) {
                    // Index of last element in list:
                    int lastIndex = arrFreeHand.size()-1;
                    FreeHandDraw free = arrFreeHand.get(lastIndex);
    	        free.setFreehandX(e.getX());
                    free.setFreehandY(e.getY());
                    repaint();
                }
            });  // closing curley brace was missing
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
    	g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
    	g2.setPaint(color);
            freehand.drawme(g);
        }
    
    
        public static void main(String[] args){
            SwingUtilities.invokeLater(new Runnable(){
        	    public void run(){
        	        FHP free = new FHP();
        		JFrame f = new JFrame();
        		f.getContentPane().add(free);
        		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        		f.setSize(700, 700);
        		f.setLocationRelativeTo(null); // center
        		f.setVisible(true);
        	    }
        	});
        }
        
        public class FreeHandDraw {
            int[] freehandX = new int[10000];
            int[] freehandY = new int[10000];
            boolean newFreeHand = false;
            int index = 0;
            Color color;
    
            public void drawme(Graphics g) {
                g.setColor(getColor());
                for (int i = 0; i < index - 1; i++) {
                    int x = freehandX[i];
                    int y = freehandY[i];
                    int x2 = freehandX[i + 1];
                    int y2 = freehandY[i + 1];
                    g.drawLine(x, y, x2, y2);
                }
            }
    
            public void setColor(Color str) { color = str; }
    
            public Color getColor() { return color; }
    
            public int getIndex() { return index; }
    
            public void setFreehandX(int is) {
                freehandX[index] = is;
            }
    
            public void setFreehandY(int is) {
                freehandY[index++] = is;
            }
    
            public void setIndex(int i) { index = i; }
    
            public boolean isNewFreeHand() { return newFreeHand; }
    
            public void setNewFreeHand(boolean newFreeHand) {
                this.newFreeHand = newFreeHand;
            }
        }
    }

Posting Permissions

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