Results 1 to 11 of 11
Like Tree4Likes
  • 1 Post By KevinWorkman
  • 1 Post By gimbal2
  • 1 Post By KevinWorkman
  • 1 Post By DarrylBurke

Thread: Painting program

  1. #1
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Painting program

    Hello all .. i am back from vacations and I decided to make a better(i hope so) version of window's Paint program.. i am in begging and face up a problem which i would like to solve first.. below is my two classes

    Painting Class :

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    
    
    
    @SuppressWarnings("serial")
    public class Painting extends JFrame  {
    	static int WIDTH = 500;
    	static int HEIGHT = 500;
    	
    	public Painting(){
    		unit();
    	}
    	
    	public void unit(){
    		setSize(WIDTH,HEIGHT);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLayout(new BorderLayout());
    		setBackground(Color.GRAY);
    		
    		//-------------------------------------------------------------//
    		//i will do a lot of things with all that staff below later
    		JMenu file = new JMenu("file");
    		JMenuItem New = new JMenuItem("New");
    		file.add(New);
    		
    		JMenuBar menuBar = new JMenuBar();
    		menuBar.add(file);
    		this.add(menuBar,BorderLayout.NORTH);
    		//-------------------------------------------------------------//
    		
    	}
    	
    	public static void main(String [] args){
    		Painting program = new Painting();
    		DrawPanel drawPanel = new DrawPanel();
    		program.add(drawPanel,BorderLayout.CENTER);
    		program.setVisible(true);
    		while(true){
    			try{
    				Thread.sleep(50);
    			}catch(Exception e){
    				e.getStackTrace();
    			}
    			drawPanel.repaint();
    		}
    	}
    }
    2. DrawPanel Class :

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.util.ArrayList;
    
    import javax.swing.JPanel;
    
    // DrawPanel Class 
    public class DrawPanel extends JPanel implements MouseMotionListener , MouseListener{
    	//the cords in which the mouse click to draw something
    	int x = 0;
    	int y = 0;
    	
    	// ArrayList to store the Cords of drawing objects 
    	ArrayList<Integer> drawCordX = new ArrayList<>();
    	ArrayList<Integer> drawCordY = new ArrayList<>();
    	//i use ArrayList because i would like to use unDo and reDo 
    	
    	// Constructor of the DrawPanel Class 
    	public DrawPanel(){
    		//set the background color  to white
    		this.setBackground(Color.WHITE);
    		// add MouseMotionListener
    		addMouseMotionListener(this);
    		//add MouseListener Because i want to draw Staff just with mouse click
    		addMouseListener(this);
    	}
    	
    	//paint method
    	public void paint(Graphics g){
    		// clean the background
    		super.paint(g);
    		// Create 2D graphics
    		Graphics2D g2d = (Graphics2D) g;
    		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    				RenderingHints.VALUE_ANTIALIAS_ON);
    		
    		//Draw all the clicked cords
    		for(int i = 0 ; i < drawCordX.size(); i ++){
    			g2d.fillOval(drawCordX.get(i), drawCordY.get(i), 10, 10);
    		}
    	}
    
    
    	@Override
    	public void mouseDragged(MouseEvent e) {
    		// TODO Auto-generated method stub
    		drawCordX.add(e.getX());
    		drawCordY.add(e.getY());
    	}
    
    	@Override
    	public void mouseMoved(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		// TODO Auto-generated method stub
    		drawCordX.add(e.getX());
    		drawCordY.add(e.getY());
    	}
    
    	@Override
    	public void mousePressed(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mouseReleased(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    }
    now where is the problem? all i am trying to do now is let the user draw a single line while pressing the mouse
    this is happening only if the users is not hurrying but if the user move the mouse very fast this is the result :P :
    Painting program-x3b.png

    but why is that happening? i mean when you move the mouse from (example) x1 = 100 to x2= 200 you didn't pass all the cords between these two cords(x1-x2)? 101,102,103...199,200 ?
    please don't tell me that the problem is in Threed.sleep() ... i just think about it.. i will try a swing timer..

    thx in advance

    PS. Sorry if the correct Section is SWING/AWT .. i am not sure which one is the right for this post
    Last edited by ShadowWalker; 08-14-2013 at 05:34 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Painting program

    The short answer is that no, you don't pass through every point. This is going to be entirely dependent on your OS and your mouse hardware and settings. What you're seeing is exactly what I'd expect. You're going to have to take that into account if you want the user to draw a continuous line.

    I'll move this to the Swing forum for you.
    ShadowWalker likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Painting program

    That's basically it, yes. You move the mouse so quick that between two mouse move events the cursor moved a great number of pixels. The OS will have a certain rate at which it will "sample" the mouse state and generate events for it. Generally you can configure that mouse rate nowadays, but you should assume this behavior is just every day life.

    A decent paint program will "solve" that by actually drawing a straight line between the start and end sampling point if they're too far apart (to do that you'd need to maintain a previous mouse position).

    edit: ha, you win this round KevinWorkman :p
    ShadowWalker likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Painting program

    Also, that loop with the call to sleep and repaint seems unnecessary. Why not just repaint when the user changes something?
    DarrylBurke likes this.
    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
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,252
    Rep Power
    19

    Default Re: Painting program

    Mouse events are reported by the underlying OS at discrete intervals. Nothing you can do about that.

    To 'join the dots' see Path2D.

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

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

    Default Re: Painting program

    You'll also want to learn about Strokes.

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

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,252
    Rep Power
    19

    Default Re: Painting program

    Quote Originally Posted by gimbal2 View Post
    A decent paint program will "solve" that by actually drawing a straight line between the start and end sampling point if they're too far apart
    Path2D.curveTo / quadTo would IMO do a better job. If I move the mouse around in a circle, I don't want to get a polygon.

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

  8. #8
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: Painting program

    "Also, that loop with the call to sleep and repaint seems unnecessary. Why not just repaint when the user changes something?" < -- for now i believe that this is true.. but i am going to do some other thing letter ..

    "A decent paint program will "solve" that by actually drawing a straight line between the start and end sampling point if they're too far apart (to do that you'd need to maintain a previous mouse position)." < -- Great advice

    Thx guys for the replies

    Edit : Path2D.curveTo / quadTo would IMO do a better job. If I move the mouse around in a circle, I don't want to get a polygon. < -- RIGHT :P THX THX THXXXX

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,620
    Rep Power
    5

    Default Re: Painting program

    I also got slightly better performance when I drew to an image and then used clip region when calling repaint. So I write to the image in paintComponent and then draw the image immediately after. I did not need to save all the points because drawing to an image is additive. However, for real fast mouse moves it still leaves significant gaps.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Painting program

    Quote Originally Posted by DarrylBurke View Post
    Path2D.curveTo / quadTo would IMO do a better job. If I move the mouse around in a circle, I don't want to get a polygon.

    db
    Good point, but it probably depends on the gesture you're making what the paint program should intelligently do to 'fill the gaps' for the best results. But that's going very far, I do hope friend ShadowWalker is not aiming to build the next photoshop ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: Painting program

    Quote Originally Posted by gimbal2 View Post
    Good point, but it probably depends on the gesture you're making what the paint program should intelligently do to 'fill the gaps' for the best results. But that's going very far, I do hope friend ShadowWalker is not aiming to build the next photoshop ;)
    i am not going to build new photoshop (Yet) :P
    but i have to admit it.. on a scale from 1 to 10 i am in -5 :P
    my knowledge are still very very small :\
    java has no end :P
    Thx all for the usefull info .. i am going to check all of them :)

Similar Threads

  1. Re-painting
    By tnrh1 in forum New To Java
    Replies: 36
    Last Post: 11-09-2012, 05:31 PM
  2. Painting in jPanels
    By nickrrr in forum AWT / Swing
    Replies: 7
    Last Post: 05-25-2011, 01:36 PM
  3. Painting in SWT
    By jionnet in forum SWT / JFace
    Replies: 9
    Last Post: 09-24-2010, 06:52 AM
  4. Painting
    By xael in forum New To Java
    Replies: 6
    Last Post: 09-06-2010, 05:10 AM
  5. netbeans+swing = painting program?
    By yuriythebest in forum New To Java
    Replies: 3
    Last Post: 12-14-2008, 08:55 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
  •