Results 1 to 6 of 6
  1. #1
    timo0060 is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Question Mouse Lag on A JApplet Paint program.

    As the title says, I'm having an issue with my very basic painting program I'm making for a school assignment. If I move the mouse slowly, it is a solid line, but if I move the mouse quicker, the circles are a lot further apart and gives a polka dot look. How can this be fixed so that it's not the polka dot spread? Here's my code (It's my first post sorry if anything is missing):

    Java Code:
    import java.awt.*;
    
    import java.awt.event.*;
    
    import javax.swing.*;
    
    
    
    public class Paint{
    	
    
            public static void main(String[] args){
    
                    Icon iconB = new ImageIcon("blue.png");
    
                    Icon iconM = new ImageIcon("magenta.png");
    
                    Icon iconR = new ImageIcon("red.png");
    
                    Icon iconBl = new ImageIcon("black.png");
    
                    Icon iconG = new ImageIcon("green.png");
                    
                    Icon iconY = new ImageIcon("yellow.png");
                    
                    Icon iconO = new ImageIcon("orange.png");
                    
                    Icon iconGR = new ImageIcon("gray.png");
    
                    Icon iconW = new ImageIcon("white.jpg");
                    
                    Icon iconA = new ImageIcon("aqua.png");
                    
                    Icon iconBucket = new ImageIcon("bucket.png");
                    
                   Icon blank = new ImageIcon("blank.png");
                    
                    
                    
    
                    JFrame frame = new JFrame("Paint It");
                    JLabel label = new JLabel(blank);
                    JLabel label2 = new JLabel(blank);
                    JLabel label3 = new JLabel(blank);
                    frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    
    
                    
    
                    Container content = frame.getContentPane();
    
                    //Creates a new container
    
                    content.setLayout(new BorderLayout());
    
                    //sets the layout
    
                    
    
                    final PadDraw drawPad = new PadDraw();
    
                    //creates a new padDraw, which is pretty much the paint program
    
                    
    
                    content.add(drawPad, BorderLayout.CENTER);
    
                    //sets the padDraw in the center
    
                    
    
                    JPanel panel = new JPanel(new GridBagLayout());
                    panel.setBackground(Color.darkGray);
    
                    //creates a JPanel
    
                    panel.setPreferredSize(new Dimension(200, 40));
    
                    panel.setMinimumSize(new Dimension(200, 40));
    
                    panel.setMaximumSize(new Dimension(200, 40));
    
                    //This sets the size of the panel
    
                    
    
                    JButton clearButton = new JButton("Clear");
    
                    //creates the clear button and sets the text as "Clear"
    
                    clearButton.addActionListener(new ActionListener(){
    
                            public void actionPerformed(ActionEvent e){
    
                                    drawPad.clear();
    
                            }
    
                    });
                    
                    JButton bucketButton = new JButton(iconBucket);
    
                    //creates the red button and sets the icon we created for red
    
                    bucketButton.addActionListener(new ActionListener(){
    
                            public void actionPerformed(ActionEvent e){
    
                                    drawPad.bucket();
    
                            }
    
    
                    });
    
                    //this is the clear button, which clears the screen.  This pretty
    
                    //much attaches an action listener to the button and when the
    
                    //button is pressed it calls the clear() method
    
                    
    
                    JButton redButton = new JButton(iconR);
    
                    //creates the red button and sets the icon we created for red
    
                    redButton.addActionListener(new ActionListener(){
                    	
    
                            public void actionPerformed(ActionEvent e){
    
                                    drawPad.red();
    
                            }
    
    
                    });
                   
    
                    //when pressed it will call the red() method.  So on and so on =]
    
                    
    
                    JButton blackButton = new JButton(iconBl);
    
                    //same thing except this is the black button
    
                    blackButton.addActionListener(new ActionListener(){
    
                            public void actionPerformed(ActionEvent e){
    
                                    drawPad.black();
    
                            }
    
                    });
    
                    
    
                    JButton magentaButton = new JButton(iconM);
    
                    //magenta button
    
                    magentaButton.addActionListener(new ActionListener(){
    
                            public void actionPerformed(ActionEvent e){
    
                                    drawPad.magenta();
    
                            }
    
                    });
    
                    
    
                    JButton blueButton = new JButton(iconB);
    
                    //blue button
    
                    blueButton.addActionListener(new ActionListener(){
    
                            public void actionPerformed(ActionEvent e){
    
                                    drawPad.blue();
    
                            }
    
                    });
    
                    
    
                    JButton greenButton = new JButton(iconG);
    
                    //green button
    
                    greenButton.addActionListener(new ActionListener(){
    
                            public void actionPerformed(ActionEvent e){
    
                                    drawPad.green();
    
                            }
    
                    });
                    
                    JButton yellowButton = new JButton(iconY);
    
                    //creates the red button and sets the icon we created for red
    
                    yellowButton.addActionListener(new ActionListener(){
    
                            public void actionPerformed(ActionEvent e){
    
                                    drawPad.yellow();
    
                            }
    
    
                    });
                    
                    JButton orangeButton = new JButton(iconO);
    
                    //creates the red button and sets the icon we created for red
    
                    orangeButton.addActionListener(new ActionListener(){
    
                            public void actionPerformed(ActionEvent e){
    
                                    drawPad.orange();
    
                            }
    
    
                    });
                    
                    JButton grayButton = new JButton(iconGR);
    
                    //creates the red button and sets the icon we created for red
    
                    grayButton.addActionListener(new ActionListener(){
    
                            public void actionPerformed(ActionEvent e){
    
                                    drawPad.gray();
    
                            }
    
    
                    });
                    
                    JButton aquaButton = new JButton(iconA);
    
                    //creates the red button and sets the icon we created for red
    
                    aquaButton.addActionListener(new ActionListener(){
    
                            public void actionPerformed(ActionEvent e){
    
                                    drawPad.aqua();
    
                            }
    
    
                    });
                    
                    JButton whiteButton = new JButton(iconW);
    
                    //creates the red button and sets the icon we created for red
    
                    whiteButton.addActionListener(new ActionListener(){
    
                            public void actionPerformed(ActionEvent e){
    
                                    drawPad.white();
    
                            }
    
    
                    });
                    
                    
    
                    blackButton.setPreferredSize(new Dimension(16, 16));
                    
                    whiteButton.setPreferredSize(new Dimension(16, 16));
    
                    magentaButton.setPreferredSize(new Dimension(16, 16));
    
                    redButton.setPreferredSize(new Dimension(16, 16));
    
                    blueButton.setPreferredSize(new Dimension(16, 16));
                    
                    yellowButton.setPreferredSize(new Dimension(16, 16));
    
                    greenButton.setPreferredSize(new Dimension(16,16));
                    
                    orangeButton.setPreferredSize(new Dimension(16,16));
                    
                    aquaButton.setPreferredSize(new Dimension(16,16));
                    
                    grayButton.setPreferredSize(new Dimension(16,16));
                    
                    bucketButton.setPreferredSize(new Dimension(32,32));
    
                    //sets the sizes of the buttons
                    
                    
    
                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.insets = new Insets(5,1,1,1);
    
                    
                    gbc.gridx = 0;
                    gbc.gridy = 0;
                    
                    panel.add(blackButton, gbc);
                    gbc.gridx = 1;
                    panel.add(grayButton, gbc);
                    gbc.gridx = 2;
                    panel.add(whiteButton, gbc);
                    gbc.gridx = 3;
                    panel.add(yellowButton, gbc);
                    gbc.gridx = 4;
                    panel.add(orangeButton, gbc);
                    gbc.gridx = 5;
                    panel.add(redButton, gbc);
                    gbc.gridx = 6;
                    panel.add(magentaButton, gbc);
                    gbc.gridx = 7;
                    panel.add(blueButton, gbc);
                    gbc.gridx = 8;
                    panel.add(aquaButton, gbc);
                    gbc.gridx = 9;
                    panel.add(greenButton, gbc);
                    gbc.gridx = 10;
                    panel.add(label,gbc);
                    gbc.gridx = 11;
                    panel.add(label2,gbc);
                    gbc.gridx = 12;
                    panel.add(bucketButton, gbc);
                    gbc.gridx = 13;
                    panel.add(label3,gbc);
                    gbc.gridx = 14;
                    panel.add(clearButton, gbc);
    
                    //adds the buttons to the panel
    
                    
    
                    content.add(panel, BorderLayout.SOUTH);
    
                    //sets the panel to the bottom
    
                    
    
                    frame.setSize(800, 700);
    
                    //sets the size of the frame
    
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
                    //makes it so you can close
    
                    frame.setVisible(true);
    
                    //makes it so you can see it
    
            }
    
    }
    
    
    
    @SuppressWarnings("serial")
    class PadDraw extends JComponent{
    
    		int color = 0;
    	
            Image image;
            
            
            //this is gonna be your image that you draw on
    
            Graphics2D graphics2D;
    
            //this is what we'll be using to draw on
    
            int currentX, currentY, oldX, oldY;
    
            //these are gonna hold our mouse coordinates
    
    
            //Now for the constructors
    
            public PadDraw(){
            	
    
                    setDoubleBuffered(false);
    
                    addMouseListener(new MouseAdapter(){
    
                            public void mousePressed(MouseEvent e){
    
                                    oldX = e.getX();
    
                                    oldY = e.getY();
    
                            }
    
                    });
    
                    //if the mouse is pressed it sets the oldX & oldY
    
                    //coordinates as the mouses x & y coordinates
    
                    addMouseMotionListener(new MouseMotionAdapter(){
    
                            public void mouseDragged(MouseEvent e){
    
                            	repaint(currentX, currentY, 20, 20);
                            	currentX = e.getX();
                                currentY = e.getY();
                                repaint(currentX, currentY, 20, 20);
    
                                    if(graphics2D != null)
                                    	
                                    graphics2D.fillOval(currentX, currentY, 5,5);
    
                                    repaint();
    
                                    oldX = currentX;
    
                                    oldY = currentY;
    
                            }
    
    
                    });
    
                    //while the mouse is dragged it sets currentX & currentY as the mouses x and y
    
                    //then it draws a line at the coordinates
    
                    //it repaints it and sets oldX and oldY as currentX and currentY
    
            }
            
            public void bucket(){
    
            	if(color == 1){        	
            		graphics2D.setPaint(Color.red);
            	}
            	
            	if(color == 2){            	
            		graphics2D.setPaint(Color.black);
            	}
            	
            	if(color == 3){            	
            		graphics2D.setPaint(Color.magenta);
            	}
            	
            	if(color == 4){            	
            		graphics2D.setPaint(Color.blue);
            	}
            	
            	if(color == 5){            	
            		graphics2D.setPaint(Color.green);
            	}
    
            	if(color == 6){            	
            		graphics2D.setPaint(Color.yellow);
            	}
            	
            	if(color == 7){            	
            		graphics2D.setPaint(Color.orange);
            	}
            	
            	if(color == 8){            	
            		graphics2D.setPaint(Color.gray);
            	}
            	
            	if(color == 9){            	
            		graphics2D.setPaint(Color.cyan);
            	}
    
    
            	if(color == 10){            	
            		graphics2D.setPaint(Color.white);
            	}
    
    
                graphics2D.fillRect(0, 0, getSize().width, getSize().height);
    
                repaint();
    
            }
    
    
            public void paintComponent(Graphics g){
    
                    if(image == null){
    
                            image = createImage(getSize().width, getSize().height);
    
                            graphics2D = (Graphics2D)image.getGraphics();
    
                            graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    
                            clear();
    
    
                    }
    
                    g.drawImage(image, 0, 0, null);
    
            }
    
            //this is the painting bit
    
            //if it has nothing on it then
    
            //it creates an image the size of the window
    
            //sets the value of Graphics as the image
    
            //sets the rendering
    
            //runs the clear() method
    
            //then it draws the image
    
    
    
            public void clear(){
    
                    graphics2D.setPaint(Color.white);
    
                    graphics2D.fillRect(0, 0, getSize().width, getSize().height);
    
                    graphics2D.setPaint(Color.black);
                    
    
                    repaint();
    
            }
            
            public void white(){
    
                graphics2D.setPaint(Color.white);
                
                color = 10;
    
                repaint();
    
            }
            
          
            //this is the clear
    
            //it sets the colors as white
    
            //then it fills the window with white
    
            //thin it sets the color back to black
    
            public void red(){
    
                    graphics2D.setPaint(Color.red);
                    
                    color = 1;
    
                    repaint();
    
            }
    
            //this is the red paint
    
            public void black(){
    
                    graphics2D.setPaint(Color.black);
                    
                    color = 2;
    
                    repaint();
    
            }
    
            //black paint
    
            public void magenta(){
    
                    graphics2D.setPaint(Color.magenta);
                    
                    color = 3;
    
                    repaint();
    
            }
    
            //magenta paint
    
            public void blue(){
    
                    graphics2D.setPaint(Color.blue);
                    
                    color = 4;
    
                    repaint();
    
            }
    
            //blue paint
    
            public void green(){
    
                    graphics2D.setPaint(Color.green);
                    
                    color = 5;
    
                    repaint();
    
            }
    
            //green paint
            
            public void yellow(){
    
                graphics2D.setPaint(Color.yellow);
                
                color = 6;
    
                repaint();
    
            }
            
            public void orange(){
    
                graphics2D.setPaint(Color.orange);
                
                color = 7;
    
                repaint();
    
            }
            
            public void gray(){
    
                graphics2D.setPaint(Color.gray);
                
                color = 8;
    
                repaint();
    
            }
            
            public void aqua(){
    
                graphics2D.setPaint(Color.cyan);
                
                color = 9;
    
                repaint();
    
            }
            
    
    
    }
    Thank you for any help, and please put any solutions in the easiest to understand format. Thank you all once again.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Mouse Lag on A JApplet Paint program.

    MouseEvents are generated by the underlying OS at discrete intervals, so you'll never get the continuity you want with that code (which is too long to read, so I didn't). Look into using a Path2D and its curveTo/QuadTo methods.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Mouse Lag on A JApplet Paint program.

    Moved from Applets.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default Re: Mouse Lag on A JApplet Paint program.

    That is waay too much code to ask somebody else to wade through for you. You should post code in the form of an SSCCE, not hundreds of lines of code with tons of extra information that doesn't have anything to do with your main problem.

    But think about it. The faster the mouse moves, the more pixels it moves between steps, right? So if you move it slowly, maybe its speed is only 1 pixel per step. But if you move it faster, maybe it moves 10 pixels, or 100, per step instead. You don't get a continuous update for every pixel the mouse crosses over (nor would you want to). So you have to connect the previous position to the current position. How you do that depends on what you want the end result to look like, but an easy way to do it is to draw lines instead of points or circles.
    Last edited by KevinWorkman; 04-30-2012 at 09:06 PM.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    timo0060 is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: Mouse Lag on A JApplet Paint program.

    Thank you for your replies. I'll certainly look into the Path2D. Also, I apologies for posting so much code. It was my first post and I wasn't sure how much to post. Next time I'll remember to only post the relevant stuff. Thanks once again KevinWorkman and DarrylBurke

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Mouse Lag on A JApplet Paint program.

    It's not just the sheer volume of code. The extra blank lines just make it more difficult to read.

    Learn the coding conventions: Code Conventions for the Java Programming Language: Contents

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 1
    Last Post: 07-12-2011, 09:45 AM
  2. Paint program
    By aarti in forum AWT / Swing
    Replies: 14
    Last Post: 05-06-2011, 07:55 AM
  3. paint program
    By dewdadamnthang in forum New To Java
    Replies: 4
    Last Post: 03-30-2011, 12:40 PM
  4. Help with paint program
    By michcan in forum Java 2D
    Replies: 1
    Last Post: 02-04-2011, 07:26 AM
  5. Paint Program Help
    By ngiannini in forum AWT / Swing
    Replies: 12
    Last Post: 05-10-2010, 05:24 PM

Posting Permissions

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