Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By camickr

Thread: Paint

  1. #1
    ninjaturtlez is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Paint

    I am attempting to make a paint tpye program for a school project, it is an applet. The current problem i am haveing is that the mouse cant move very quickly before spaces become between them. Is there any way to make my program run faster to get rid of these???

    here she is
    (Try moving your mouse very quickly to see what i mean)

    Java Code:
    package paintEmulator;
    
    import java.applet.Applet;	
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    
    public class Main extends Applet implements ActionListener, MouseMotionListener, MouseListener {
    
    	Graphics bufferGraphics; 
    	Image offscreen; 
    	Dimension dim; 
    	int dimX = 600;
    	int dimY = 600;
    	
    	
    	Button erase = new Button ("Eraser");
    	Button square = new Button ("Square");
    	Button pencil = new Button ("Pencil");
    	Button size = new Button ("+");
    	Button size2 = new Button ("-");
    	
    	Color color = Color.CYAN;
    	Color black = Color.black;
    	int mpressedX;
    	int mpressedY;
    	int moveY;
    	int moveX;
    	
    	int w = 15;
    	int c = 0;
    	int d = 0;
    	int type = 3;
    	 boolean mEnter = false;
    	
    	
    	
    	public void init(){
    		
    	setLayout (new FlowLayout(FlowLayout.CENTER));
    	setSize (600,600);
    	
    	dim = getSize();
    	offscreen = createImage(dim.width,dim.height);
    	bufferGraphics = offscreen.getGraphics();
    	
    	
    	add (pencil);
    	add (erase);
    	add (square);
    	add(size2);
    	add(size);
    
    	
    	size.addActionListener(this);
    	size2.addActionListener(this);
    	pencil.addActionListener(this);
    	erase.addActionListener(this);
    	square.addActionListener(this);
    	
    	addMouseMotionListener(this);
    	addMouseListener(this);
    	
    	}
    	
    	public void paint (Graphics g){
    
    		bufferGraphics.setColor(Color.lightGray);	
    		
    		
    		bufferGraphics.fillRect(0,0,600,30);	
    		bufferGraphics.fillRect(0,0,20,600);
    		bufferGraphics.fillRect(580,0,20,600);
    		bufferGraphics.fillRect(0,580,600,20);
    		
    		String posX = Integer.toString(moveX);
    		String posY = Integer.toString(moveY);
    		
    		bufferGraphics.setColor(Color.black);	
    		
    		
    		if (mEnter){
    			
    			bufferGraphics.setColor(Color.CYAN);
    			bufferGraphics.drawRect(0, 0, 600,2);
    			bufferGraphics.fillRect(0,0, 2,600);
    			bufferGraphics.fillRect(598,0,2,600);
    			bufferGraphics.fillRect(0,598,600,2);
    		}
    		
    		if (c > d){
    			bufferGraphics.setColor(black);
    
    			bufferGraphics.drawString(posX+", " + posY, 540, 590);
    			c = 0;
    			d= 0;
    		}else if(d>c){
    			bufferGraphics.setColor(black);
    
    			bufferGraphics.drawString(mpressedX+", " + mpressedY, 540, 590);
    			c = 0;
    			d = 0;	
    		}
    		
    		
    		if (type == 1){
    		
    			bufferGraphics.setColor(color);	
    			bufferGraphics.fillRect(mpressedX, mpressedY,w, w);	
    		
    		}else if (type == 2){
    			bufferGraphics.setColor(Color.WHITE);
    			bufferGraphics.fillRect(mpressedX, mpressedY, w, w);
    			
    		}else if (type == 3){
    			bufferGraphics.setColor(color);	
    			bufferGraphics.fillOval(mpressedX, mpressedY,4, 4);	
    			
    		}
    		
    		
    		g.drawImage(offscreen,0,0,this);
    
    	}
    	
    	
    	public void update(Graphics g){
    		paint(g);
    	}
    	
    	
    	
    	//Mouse Listener
    	@Override
    	public void mouseDragged(MouseEvent ev) {
    		mpressedX = ev.getX();
    		mpressedY = ev.getY();
    		d++;
            repaint(); 	
    	}
    
    	@Override
    	public void mouseMoved(MouseEvent ev) {
    	moveX = ev.getX();
    	moveY = ev.getY();
    	c++;
    	repaint();
    	}
    
    	
    	//Action Listener
    	@Override
    	public void actionPerformed(ActionEvent ev) {
    		if (ev.getSource() == pencil){
    			type = 3;
    		}else if (ev.getSource() == erase){
    			type = 2;
    		}else if(ev.getSource()== square){
    			type = 1;
    		}else if (ev.getSource()== size){
    			w +=2;
    			if (w >= 30){
    				
    				w = 30;
    			}
    		}else if (ev.getSource()== size2){
    			w-=2;
    			if (w<= 3){
    				
    				w = 3;
    			}
    			
    		}
    			
    
    	}
    	
    	
    	//Mouse Click Listener
    
    	@Override
    	public void mouseClicked(MouseEvent ev) {
    		
    		
    		
    	}
    	@Override
    	public void mouseEntered(MouseEvent ev) {
    		mEnter  = true;
    		repaint();
    	}
    
    	@Override
    	public void mouseExited(MouseEvent ev) {
    		
    		mEnter = false;
    		repaint();		
    	}
    
    	@Override
    	public void mousePressed(MouseEvent ev) {
            mpressedX = ev.getX(); 
            mpressedY = ev.getY(); 
            repaint(); 		
    		
    	}
    
    	@Override
    	public void mouseReleased(MouseEvent ev) {
    
    	}
    
    	
    	
    	
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Paint

    Try adding some debug code to display/print out the locations as they are received by the listeners.
    Do you get more positions in the listeners than are being drawn?
    Does the program draw all the positions it gets in the listener?

  3. #3
    ninjaturtlez is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: Paint

    It is putting out the square/pencil on every coordinate but it is not getting them fast enough. Is there a way to get the coordinates faster and print them faster?

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Paint

    You will never get an event generated for each pixel of movement (unless as you noted you move really slow). This is simple the way event generation works.

    One solution is to keep track of the previous/current event and then do an interpolation between the two points and then paint each point.

    Why are you still using AWT? Schools should be teaching Swing painting concepts which are different than AWT.
    Last edited by camickr; 12-17-2011 at 01:44 AM.
    Norm likes this.

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

    Default Re: Paint

    Moved from 'New to Java'

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

Similar Threads

  1. JFrame cannot paint?
    By pandaman0212 in forum AWT / Swing
    Replies: 7
    Last Post: 05-04-2013, 08:19 AM
  2. Paint????
    By seanfmglobal in forum New To Java
    Replies: 3
    Last Post: 02-15-2011, 09:00 AM
  3. paint with JScrollBar
    By shakeel in forum Java 2D
    Replies: 5
    Last Post: 12-23-2010, 05:35 PM
  4. how to add more than one paint method
    By gautham in forum Java 2D
    Replies: 2
    Last Post: 04-06-2010, 08:07 AM
  5. paint() and paintComponent()
    By goldhouse in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 04:43 AM

Posting Permissions

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