Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Question Something like "paint" in java

    Here is paint-like program. And i have 2 things missing there.

    1) i need image to stay on "canvas" after resizing or minimizing window (i've read a lot of forums about same problem, but i just can not get it to work)
    2) if anyone could suggest some good "straight" line drawing algorithm (like in paint. when line is drawn from the clicked spot to the pointer and you can move it while lmb is pressed) i would be very grateful, i am new to java and i can not think of anything :(

    (sorry for the poor code quality, as i've already said i am new to java). And thank you all in advance :)

    Java Code:
    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.colorchooser.*;
    import javax.swing.event.*;
    import java.awt.geom.Line2D;
    
    
    public class PaintIt extends JFrame implements ChangeListener,ActionListener{
    
    
    	
    //	public BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
    	//JPanel canvas = new JPanel();
    	
    	
    	JPanel buttonPanel = new JPanel();
    	JColorChooser colorPanel = new JColorChooser();
    	Point lastPos = null;
    	Point startPos = null;
    	Point finishPos = null;
    	Graphics g;
    	JButton drawButton = new JButton("Free");
    	JButton lineButton = new JButton("Line");
    	JButton clearButton = new JButton("Clear");
    	public ColorSelectionModel Model;
    	JPanel canvas = new PaintPanel();
    	int changer = 1;
    	
    	public PaintIt () {
    
    		
    		setLocation(100,100);
    		setSize(900,700);
    		setTitle("Photoshop");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		canvas.setBackground(Color.WHITE);
    		
    		clearButton.addActionListener(this);
    		clearButton.setActionCommand("clear");
    		
    		drawButton.addActionListener(this);
    		drawButton.setActionCommand("draw");
    		
    		lineButton.addActionListener(this);
    		lineButton.setActionCommand("line");
    		
    		//add buttons here
    		buttonPanel.add(drawButton);
    		buttonPanel.add(lineButton);
    		buttonPanel.add(clearButton);
    
    		Model = colorPanel.getSelectionModel();
    		Model.addChangeListener (this);
    		
    		//set the look
    		getContentPane().add(canvas, BorderLayout.CENTER);
    		getContentPane().add(buttonPanel, BorderLayout.NORTH);
    		getContentPane().add(colorPanel, BorderLayout.SOUTH);
    		setVisible(true);
    		
    		g = canvas.getGraphics();
    		g.setColor(Color.BLACK);
    
    		canvas.addMouseMotionListener(new MouseMotionListener () {						
    			public void mouseDragged (MouseEvent m) {
    				Point p = m.getPoint() ;
    				if (changer==1){
    				g.drawLine(lastPos.x, lastPos.y, p.x, p.y) ;
    				}			
    				lastPos = p ;
    
    			}	
    			public void mouseMoved (MouseEvent m) {}
    		});
    						
    		canvas.addMouseListener(new MouseListener () {
    			public void mouseClicked(MouseEvent e) {startPos = e.getPoint();}
    			public void mousePressed(MouseEvent e) {lastPos = e.getPoint();}
    			public void mouseReleased(MouseEvent e) {
    			lastPos = null; 
    			finishPos = e.getPoint(); 
    			startPos = null;}
    			public void mouseEntered(MouseEvent e) {}
    			public void mouseExited(MouseEvent e) {}
    			});
    	
    		
    	}
    	
    	
    	
    	public void actionPerformed(ActionEvent e) {
    		if("clear".equals(e.getActionCommand())) {
    			repaint();
    		}
    		if("draw".equals(e.getActionCommand())) {
    			changer = 1;
    		}
    		if("line".equals(e.getActionCommand())) {
    		    changer = 2;
    		}
    		
    			
    	}
    	
    	
    /*
    	public void captureCanvasImage (){
    		Graphics g = image.createGraphics();
    		canvas.paint(g);
    	}
    */
    	/*
    	@Override
    	public void invalidate() {     
    	super.invalidate(); 
    	
    	this.paint(this.getGraphics()); 
    	}
    */
    	
    	 public void stateChanged(ChangeEvent e) {
    		    Color Choice;
    		    Choice  = colorPanel.getColor();
    		    g.setColor(Choice);
    	} 
    	
    	
    	
    	public static void main (String [] args) {
    		PaintIt p = new PaintIt();
    		p.setVisible(true);
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Don't use getGraphics to get your Graphics object. Instead extend JPanel override its paintComponent method and paint in there. If this sounds Greek to you, then please check out the Sun Swing and graphics tutorials. Hang on and I'll find the links...

    Edit: Ah, here you go:

    Performing Custom Painting in Swing
    2D Graphics Tutorial
    Painting in AWT and Swing

  3. #3
    Arnold is offline Member
    Join Date
    Oct 2009
    Location
    Rotterdam
    Posts
    52
    Rep Power
    0

    Default

    Why don't you put this in the advanced section?
    Seriously this is not intended for beginners.

  4. #4
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Quote Originally Posted by Arnold View Post
    Why don't you put this in the advanced section?
    Seriously this is not intended for beginners.
    Graphics painting itself is a beginners topic, and while a fully fledged paint program is not, this is a simple 'click and draw' program to many of us - I've had to do quite a few of these.

    In answer to the OPs question, you'll somehow have to track what pixels are painted on. Your best bet is to track the points the user paints to, and then just redraw the entire 'picture' every time the paintComponent() method is called. I'd suggest an ArrayList<Point> or some other form of list (a Vector? Seems unnecessary as you don't have multi-threading) Make sure that update() looks something like this, or you may have problems...

    Java Code:
    public void update(Graphics g){
         super.update(g);
         paintComponent(g);
    }
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Quote Originally Posted by Fubarable View Post
    Don't use getGraphics to get your Graphics object. Instead extend JPanel override its paintComponent method and paint in there.
    This deserves its own post! Painting with getGraphics can create problems in programs that are almost impossible to fix without rewriting the whole program. Don't do it!
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

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

    Default

    2) if anyone could suggest some good "straight" line drawing algorithm
    Check out Custom Painting Approaches. The example actually draws rectangles, but it should be easy enough to figure out how to use the drawLine(...) method instead of the drawRect(...) method.

    Make sure that update() looks something like this
    There is no need to override the update() method. That is an old AWT tricke and is completely unnecessary when using Swing.

  7. #7
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    I think it looks good :)

    Though I think you should try using layouts like in microsoft expression, Photoshop etc.

  8. #8
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Quote Originally Posted by camickr View Post
    There is no need to override the update() method. That is an old AWT tricke and is completely unnecessary when using Swing.
    My bad. I've been converting too many AWT painting schemes to Swing and completely missed that.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 04:03 AM
  2. Replies: 1
    Last Post: 11-20-2009, 08:58 PM
  3. Replies: 10
    Last Post: 01-29-2009, 09:00 AM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 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
  •