Page 1 of 2 12 LastLast
Results 1 to 20 of 28
Like Tree7Likes

Thread: A simple paint program

  1. #1
    fatima25 is offline Member
    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

    Question A simple paint program

    Hi
    This is a simple Paint program in java, at first a user clicks on a button (for instance Box button) next when she clicks on the yellow panel the Box will be drown.
    Can anybody help me?


    A simple paint program-j2.jpg
    Last edited by fatima25; 07-02-2013 at 08:41 AM.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    936
    Rep Power
    2

    Default Re: A simple paint program

    First of all, nobody is going to email or DM you. We keep these discussions in this forum, since other people might benefit form this as well.

    Now, what do you mean with "I don't want to use Main class"?

  3. #3
    fatima25 is offline Member
    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

    Default Re: A simple paint program

    for example I use this to draw a line on palne:

    private void jButtonlineActionPerformed(java.awt.event.ActionEv ent evt) {

    Graphics g = jpanel1.getGraphics();

    g.drawLine(20, 20, 100, 100);

    but I want to combine it with following event:

    private void jpanel1MouseClicked(java.awt.event.MouseEvent evt) {

    }

    I want a user fisrt select button and after click on panel a shape will be drawn on yellow panel.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: A simple paint program

    Quote Originally Posted by fatima25 View Post
    private void jButtonlineActionPerformed(java.awt.event.ActionEv ent evt) {
    Scrap the visual editor, it isn't a beginners' tool. Learn to write Swing code by hand -- that's much easier.
    Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    Never use getGraphics() of a Component. Go through the section of that tutorial that deals with custom painting and learn how to do it correctly.

    Also, learn how to format code on the forum:
    Guide For New Members
    BB Code List - Java Programming Forum - Learn Java Programming

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

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    936
    Rep Power
    2

    Default Re: A simple paint program

    Your class should be getting an extra String or something, so you can remember what shape the user wants to draw. Then move the shape drawing code to jpanel1MouseClicked. For example:

    Java Code:
    public class YourClass {
       private String chosenShape;
    ...
       private void jButtonlineActionPerformed(java.awt.event.ActionEv ent evt) { 
          chosenShape = "line";
       }
    
       private void jpanel1MouseClicked(java.awt.event.MouseEvent evt) { 
          if ( chosenShape.equals("line") ) {
              //draw the line here, you can use the x/y coordinates from the event
          }
       }
    ...
    }
    fatima25 likes this.

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

    Default Re: A simple paint program

    Quote Originally Posted by SurfMan View Post
    Java Code:
          if ( chosenShape.equals("line") ) {
              //draw the line here, you can use the x/y coordinates from the event
          }
    Absolutely not. Alter state that determines where the line is drawn and call repaint(). Draw the line in a painting method override.

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

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    936
    Rep Power
    2

    Default Re: A simple paint program

    Quote Originally Posted by DarrylBurke View Post
    Scrap the visual editor, it isn't a beginners' tool. Learn to write Swing code by hand -- that's much easier.
    Never use getGraphics() of a Component. Go through the section of that tutorial that deals with custom painting and learn how to do it correctly.
    db
    Good points, although I always feel that suggesting them to extend JComponent or one of its subclasses pushes them further in the pile of you-know-what that they already have... It's probably something their professor has cooked up. I wish these professors would let go of JDK1.2 :(

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    936
    Rep Power
    2

    Default Re: A simple paint program

    Quote Originally Posted by DarrylBurke View Post
    Absolutely not. Alter state that determines where the line is drawn and call repaint(). Draw the line in a painting method override.

    db
    Again, you're right. But I'll be surprised if that was actually their assignment :)

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: A simple paint program

    suggesting them to extend JComponent or one of its subclasses pushes them further in the pile of you-know-what
    And correctly invoking getGraphics() of a Component and using the returned Graphics reference correctly requires advanced understanding of the painting mechanism, which even fairly advanced GUI programmers often don't possess.

    For 'A simple paint program' the first step is to learn and take to heart the correct way to perform custom painting. Which is explained very well in the Tutorial, along with sample codes to play with.

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

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: A simple paint program

    Quote Originally Posted by SurfMan View Post
    I'll be surprised if that was actually their assignment :)
    I'd be surprised if the assignment was to do things the wrong way.

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

  11. #11
    fatima25 is offline Member
    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

    Default Re: A simple paint program

    I want to get mouse position to draw line and other shapes, what should I do?
    Can I define a point like this:

    Point p1 = new point();

    And use it like this:

    g.drawline ( p1.x , p1.y, 100, 100) ;

    is it true ?

  12. #12
    fatima25 is offline Member
    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

    Default Re: A simple paint program

    can anybody help me about erase button?

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: A simple paint program

    I gave you a link to a tutorial. Go through it instead of playing guessing games.

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

  14. #14
    fatima25 is offline Member
    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

    Default Re: A simple paint program

    I am a new member, please treat me better !

  15. #15
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    936
    Rep Power
    2

    Default Re: A simple paint program

    Uh oh...

  16. #16
    fatima25 is offline Member
    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

    Default Re: A simple paint program

    thanks a lot

  17. #17
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default Re: A simple paint program

    Dear Fatima25,
    please do somework.Then we will be able to help you.
    Start from here..
    1. Create a class file named box.java in which there will be code of drawing box
    2. Create a class file named circle.java in which there will be code of drawing circle
    3. Create a class file named line.java in which there will be code of drawing line
    4. Study about Swing and Paint method
    5. Implement your target code.too much simple.


    You can ask anything which you dont understand.
    fatima25 likes this.
    Don't Forget to try yourself before asking others help.....
    Press REP, if you find their advices/solutions effective.

  18. #18
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: A simple paint program

    I posted three links at #4. If you can't be bothered to go through the linked pages, why should anyone here take the trouble to try and help you?

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

  19. #19
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: A simple paint program

    And for the last time, do not use getGraphics() of a Component.

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

  20. #20
    fatima25 is offline Member
    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

Page 1 of 2 12 LastLast

Similar Threads

  1. Trying to create a simple Paint application
    By driiper in forum Java 2D
    Replies: 2
    Last Post: 05-02-2012, 07:27 PM
  2. paint error, with a simple boolean
    By PRW56 in forum New To Java
    Replies: 16
    Last Post: 04-05-2012, 09:08 AM
  3. Paint program
    By aarti in forum AWT / Swing
    Replies: 14
    Last Post: 05-06-2011, 06:55 AM
  4. Help with paint program
    By michcan in forum Java 2D
    Replies: 1
    Last Post: 02-04-2011, 06:26 AM
  5. Simple Paint program question
    By StressaJune in forum New To Java
    Replies: 1
    Last Post: 03-30-2009, 08:46 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
  •