Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-22-2009, 11:16 AM
Member
 
Join Date: Nov 2009
Posts: 1
Rep Power: 0
Antonio_Rodriguez is on a distinguished road
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

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);
	}
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-22-2009, 04:37 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 5,968
Rep Power: 7
Fubarable is on a distinguished road
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
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-22-2009, 05:24 PM
Member
 
Join Date: Oct 2009
Location: Rotterdam
Posts: 39
Rep Power: 0
Arnold is on a distinguished road
Default
Why don't you put this in the advanced section?
Seriously this is not intended for beginners.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-22-2009, 07:15 PM
Senior Member
 
Join Date: Mar 2009
Posts: 376
Rep Power: 1
Singing Boyo is on a distinguished road
Default
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...

Code:
public void update(Graphics g){
     super.update(g);
     paintComponent(g);
}
__________________
So you came along and found Java? Randomly?
Well then, you're just like me!
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-22-2009, 07:19 PM
Senior Member
 
Join Date: Mar 2009
Posts: 376
Rep Power: 1
Singing Boyo is on a distinguished road
Default
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!
__________________
So you came along and found Java? Randomly?
Well then, you're just like me!
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-22-2009, 07:26 PM
Senior Member
 
Join Date: Jul 2009
Posts: 231
Rep Power: 1
camickr is on a distinguished road
Default
Quote:
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.

Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-23-2009, 08:05 AM
Senior Member
 
Join Date: Jan 2009
Posts: 103
Rep Power: 0
MuslimCoder is on a distinguished road
Default
I think it looks good

Though I think you should try using layouts like in microsoft expression, Photoshop etc.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-23-2009, 09:07 PM
Senior Member
 
Join Date: Mar 2009
Posts: 376
Rep Power: 1
Singing Boyo is on a distinguished road
Default
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.
__________________
So you came along and found Java? Randomly?
Well then, you're just like me!
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 shantimudigonda New To Java 1 11-20-2009 08:58 PM
Java, Military Format using "/" and "%" Operator!! sk8rsam77 New To Java 5 03-13-2009 08:29 AM
[SOLVED] pls help :S . &quot;Exception in thread &quot;AWT-EventQueue-0&quot; java.lang.NullPointerE ara New To Java 10 01-29-2009 09:00 AM
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan") soc86 New To Java 2 01-24-2009 07:56 PM
the dollar sign "$", prints like any other normal char in java like "a" or "*" ? lse123 New To Java 1 10-20-2008 08:35 AM


All times are GMT +2. The time now is 06:42 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org