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

Thread: simple drawing program

  1. #1
    learningJava1 is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default simple drawing program

    Hey was wondering if you guys could proof my code. I'm trying to write a simple program that lets you draw lines,circles, and polygons. I know that I need to do current state etc, but what I am have trouble with is drawing the circle and polygon. any insight would be nice. Also please let me know why my lines are always connecting to the previous line. here are my codes (sorry as i do not know how to add code with out copying and pasting).

    package SimpleDrawing;
    import java.awt.*;
    import java.util.*;
    import javax.swing.*;

    public class drawingFrame extends JFrame {
    JPanel drawingArea;
    JButton line,circle,polygon,erase;
    JPanel buttonPanel;

    drawingFrame(String title){
    super(title);
    buttonPanel = new JPanel();
    buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER,0,0));
    buttonPanel.add(line = new JButton("LINE"));
    line.setHorizontalAlignment(JLabel.RIGHT);
    buttonPanel.add(circle = new JButton("CIRCLE"));
    circle.setHorizontalAlignment(JLabel.RIGHT);
    buttonPanel.add(polygon = new JButton("POLYGON"));
    circle.setHorizontalAlignment(JLabel.RIGHT);
    buttonPanel.add(erase = new JButton("ERASE"));
    circle.setHorizontalAlignment(JLabel.RIGHT);

    setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
    //creates a studentinfopanel object
    drawingArea = new JPanel();
    //adds both the button panel and the studentinfopanel object to the JDialog, sets it visible
    //and disposes of it on close.
    add(buttonPanel);
    add(drawingArea);

    setSize(800,500);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(false);
    }
    public static void main(String args[]){
    drawingFrame frame = new drawingFrame("Drawing Tool");
    }
    }


    package SimpleDrawing;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.util.*;
    import javax.swing.*;

    public class drawingProgram {
    drawingFrame drawingtest = new drawingFrame("Drawing Tool");
    int x1,y1,x2,y2,width,height,num;

    drawingProgram(){
    drawingtest.setVisible(true);

    drawingtest.line.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    lineButton();
    }

    public void lineButton() {
    // TODO Auto-generated method stub
    drawingtest.drawingArea.addMouseListener(new MouseListener(){
    public void mouseClicked(MouseEvent e) {
    x1 = e.getX();
    y1 = e.getY();
    }
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {
    num = (Integer) e.getSource();
    drawingtest.drawingArea.getGraphics().drawPolygon( null, null, num);
    }
    });
    }
    });

    drawingtest.circle.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    circleButton();
    }

    public void circleButton() {
    // TODO Auto-generated method stub
    drawingtest.drawingArea.addMouseListener(new MouseListener(){
    public void mouseClicked(MouseEvent e) {
    x1 = e.getX();
    y1 = e.getY();
    }
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {
    x2 = e.getX();
    y2 = e.getY();
    drawingtest.drawingArea.getGraphics().drawOval(x1, y1, x2, y2);
    }
    });
    }
    });
    drawingtest.polygon.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    polygonButton();
    }

    public void polygonButton() {
    // TODO Auto-generated method stub
    drawingtest.drawingArea.addMouseListener(new MouseListener(){
    public void mouseClicked(MouseEvent e) {
    x1 = e.getX();
    y1 = e.getY();
    }
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {
    x2 = e.getX();
    y2 = e.getY();
    drawingtest.drawingArea.getGraphics().drawLine(x1, y1, x2, y2);
    }
    });
    }
    });
    drawingtest.erase.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    drawingtest.drawingArea.setVisible(false);
    }});
    }

    public static void main(String args[]){
    drawingProgram dProgram = new drawingProgram();
    }

    }

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

    Default

    See Custom Painting Approaches Java Tips Weblog for some ideas to get your started.

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

    Default

    add code with out copying and pasting)
    To wrap your code in code tags and preserve formatting, use the # icon above the input box.



    What errors do you get when you execute the code? I got this first thing:


    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JPanel cannot be cast to java.lang.Integer
    at DrawingFrame$DrawingProgram$1$1.mouseReleased(Draw ingFrame.java:82)
    Last edited by Norm; 08-12-2011 at 02:39 AM.

  4. #4
    learningJava1 is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default

    Well it had compliation or runtime errors. I'll put it in points as to whaat confuses me.

    - my lines seem to be setting their first point at the last point of the previous line. How do I end one line and begin another separately?
    - if I use getGraphics().drawOval(x1,y1,width,heightt); how do I. Get width and heighht?
    - how do I get number of lines need to form a polygon?

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

    Default

    Don't use getGraphics().
    DarrylBurke likes this.

  6. #6
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Did you try repaint(/*optionally an area*/) and use the Graphics (Graphics2D) supplied by the method paint(Graphics).
    Optionally with double buffering using BufferedImage (using its getGraphics(), or whatever the method is called, maybe it is createGraphics()).
    Last edited by Hibernate; 08-12-2011 at 05:29 AM.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  7. #7
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Quote Originally Posted by camickr View Post
    Don't use getGraphics().
    That was a great suggestion!
    Alternative, reason and everything.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  8. #8
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    @OP
    It would be nice if used code tags (the # symbol) and indents when you post your code (you can edit your post and do so).
    It would be much easier to read the code then.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

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

    Default

    Your approach is quite different for drawing. Most apps do it this way:
    Create a new class that extends JPanel and override the paintComponent method where you'll add code to draw the lines and shapes determined by other code, such as a mouse listener. Add this class to your GUI.
    The mouselistener will determine where the cursor is and save the information needed to draw the shapes. When all the data needed for a shape is obtained, the listener creates instructions for the paintComponent method and calls the repaint method. A short time later the JVM will call the paintComponent method which will look at the instructions saved by the listener code and draw the shape(s) that have been saved.

Similar Threads

  1. Problem with drawing program
    By gameaddict123 in forum New To Java
    Replies: 2
    Last Post: 07-20-2011, 03:55 AM
  2. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 05:12 AM
  3. Drawing simple shapes using loops
    By Quinn in forum New To Java
    Replies: 2
    Last Post: 05-29-2011, 09:05 AM
  4. Drawing simple graphics in NetBeans
    By arifin in forum AWT / Swing
    Replies: 1
    Last Post: 11-12-2010, 10:01 AM
  5. Simple drawing on a JPanel
    By Catfish1 in forum New To Java
    Replies: 2
    Last Post: 10-04-2010, 03:59 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
  •