Results 1 to 8 of 8
Thread: Something like "paint" in java
- 11-22-2009, 10:16 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 1
- Rep Power
- 0
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); } }
-
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
- 11-22-2009, 04:24 PM #3
Member
- Join Date
- Oct 2009
- Location
- Rotterdam
- Posts
- 52
- Rep Power
- 0
Why don't you put this in the advanced section?
Seriously this is not intended for beginners.
- 11-22-2009, 06:15 PM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 11-22-2009, 06:19 PM #5
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 11-22-2009, 06:26 PM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.2) if anyone could suggest some good "straight" line drawing algorithm
There is no need to override the update() method. That is an old AWT tricke and is completely unnecessary when using Swing.Make sure that update() looks something like this
- 11-23-2009, 07:05 AM #7
Senior Member
- Join Date
- Jan 2009
- Posts
- 119
- Rep Power
- 0
I think it looks good :)
Though I think you should try using layouts like in microsoft expression, Photoshop etc.
- 11-23-2009, 08:07 PM #8
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Similar Threads
-
Java, Military Format using "/" and "%" Operator!!
By sk8rsam77 in forum New To JavaReplies: 11Last Post: 02-26-2010, 03:03 AM -
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11-20-2009, 07:58 PM -
[SOLVED] pls help :S . "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerE
By ara in forum New To JavaReplies: 10Last Post: 01-29-2009, 08:00 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks