Results 1 to 6 of 6
  1. #1
    sskc248 is offline Member
    Join Date
    Nov 2014
    Posts
    3
    Rep Power
    0

    Default Java 2d application help

    Java 2d application  help-media_66c_66c17637-4507-49db-a98f-6bf81075b991_phphap6ie.png

    Questions at end

    Java 2D Drawing Application. The application will contain the following elements:

    a) an Undo button to undo the last shape drawn.
    b) a Clear button to clear all shapes from the drawing.
    c) a combo box for selecting the shape to draw, a line, oval, or rectangle.
    d) a checkbox which specifies if the shape should be filled or unfilled.
    e) a checkbox to specify whether to paint using a gradient.
    f) two JButtons that each show a JColorChooser dialog to allow the user to choose the first and second color in the gradient.
    g) a text field for entering the Stroke width.
    h) a text field for entering the Stroke dash length.
    I) a checkbox for specifying whether to draw a dashed or solid line.
    j) a JPanel on which the shapes are drawn.
    k) a status bar JLabel at the bottom of the frame that displays the current location of the mouse on the draw panel.


    If the user selects to draw with a gradient, set the Paint on the shape to be a gradient of the two colors chosen by the user. If the user does not chosen to draw with a gradient, the Paint with a solid color of the 1st Color.

    Note: When dragging the mouse to create a new shape, the shape should be drawn as the mouse is dragged.

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.Point;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.util.ArrayList;
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    public class MyPanel extends JPanel implements MouseListener,
            MouseMotionListener {
        static ArrayList<String> itemsDrawn;
        static String shape, color;
        static JCheckBox fillBox;
        public static void main(String[] args) {
            JFrame frame = new JFrame("Java 2D Drawing");
            frame.setSize(500, 500);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            BorderLayout borderLayout = new BorderLayout();
            frame.setLayout(borderLayout);
            final JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(0, 5, 0, 0));
            JButton clear = new JButton("Clear");
            panel.add(clear);
            JButton undo = new JButton("Undo");
            panel.add(undo);
            String[] itemTypes = { "Oval", "Rectangle", "Line" };
            JComboBox<String> shapeChooser = new JComboBox<>(itemTypes);
            panel.add(shapeChooser);
            shape = "Oval";
            String[] colors = { "Red", "Green", "Blue", "Black" };
            JComboBox<String> colorChooser = new JComboBox<>(colors);
            panel.add(colorChooser);
            color = "Red";
            fillBox = new JCheckBox("Fill");
            panel.add(fillBox);
            frame.add(panel, BorderLayout.PAGE_START);
            final MyPanel myPanel = new MyPanel();
            frame.add(myPanel, BorderLayout.CENTER);
            shapeChooser.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    JComboBox<String> cb = (JComboBox<String>) e.getSource();
                    shape = (String) cb.getSelectedItem();
                }
            });
            colorChooser.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    JComboBox<String> cb = (JComboBox<String>) e.getSource();
                    color = (String) cb.getSelectedItem();
                }
            });
            clear.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    // TODO Auto-generated method stub
                    itemsDrawn = new ArrayList<>();
                    myPanel.repaint();
                }
            });
            undo.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    // TODO Auto-generated method stub
                    if (itemsDrawn.size() != 0) {
                        itemsDrawn.remove(itemsDrawn.size() - 1);
                        myPanel.repaint();
                    }
                }
            });
            frame.setVisible(true);
        }
        /**
         * 
         */
        private static final long serialVersionUID = 5509155261502497671L;
        Point start, end;
        public MyPanel() {
            start = end = null;
            addMouseListener(this);
            addMouseMotionListener(this);
            itemsDrawn = new ArrayList<>();
        }
        @Override
        public void paint(Graphics g) {
            // TODO Auto-generated method stub
            super.paint(g);
            int counter;
            String[] temp;
            for (counter = 0; counter < itemsDrawn.size(); counter++) {
                temp = itemsDrawn.get(counter).split(" ");
                if (temp[1].equals("Red")) {
                    g.setColor(Color.RED);
                } else if (temp[1].equals("Green")) {
                    g.setColor(Color.GREEN);
                } else if (temp[1].equals("Blue")) {
                    g.setColor(Color.BLUE);
                } else if (temp[1].equals("Black")) {
                    g.setColor(Color.BLACK);
                }
                if (temp[0].equals("Rectangle")) {
                    if (Boolean.parseBoolean(temp[6])) {
                        g.fillRect(
                                Integer.parseInt(temp[2]) > Integer
                                        .parseInt(temp[4]) ? Integer
                                        .parseInt(temp[4]) : Integer
                                        .parseInt(temp[2]),
                                Integer.parseInt(temp[3]) > Integer
                                        .parseInt(temp[5]) ? Integer
                                        .parseInt(temp[5]) : Integer
                                        .parseInt(temp[3]), Math.abs(Integer
                                        .parseInt(temp[4])
                                        - Integer.parseInt(temp[2])), Math
                                        .abs(Integer.parseInt(temp[5])
                                                - Integer.parseInt(temp[3])));
                    } else {
                        g.drawRect(
                                Integer.parseInt(temp[2]) > Integer
                                        .parseInt(temp[4]) ? Integer
                                        .parseInt(temp[4]) : Integer
                                        .parseInt(temp[2]),
                                Integer.parseInt(temp[3]) > Integer
                                        .parseInt(temp[5]) ? Integer
                                        .parseInt(temp[5]) : Integer
                                        .parseInt(temp[3]), Math.abs(Integer
                                        .parseInt(temp[4])
                                        - Integer.parseInt(temp[2])), Math
                                        .abs(Integer.parseInt(temp[5])
                                                - Integer.parseInt(temp[3])));
                    }
                } else if (temp[0].equals("Oval")) {
                    if (Boolean.parseBoolean(temp[6])) {
                        g.fillOval(
                                Integer.parseInt(temp[2]) > Integer
                                        .parseInt(temp[4]) ? Integer
                                        .parseInt(temp[4]) : Integer
                                        .parseInt(temp[2]),
                                Integer.parseInt(temp[3]) > Integer
                                        .parseInt(temp[5]) ? Integer
                                        .parseInt(temp[5]) : Integer
                                        .parseInt(temp[3]), Math.abs(Integer
                                        .parseInt(temp[4])
                                        - Integer.parseInt(temp[2])), Math
                                        .abs(Integer.parseInt(temp[5])
                                                - Integer.parseInt(temp[3])));
                    } else {
                        g.drawOval(
                                Integer.parseInt(temp[2]) > Integer
                                        .parseInt(temp[4]) ? Integer
                                        .parseInt(temp[4]) : Integer
                                        .parseInt(temp[2]),
                                Integer.parseInt(temp[3]) > Integer
                                        .parseInt(temp[5]) ? Integer
                                        .parseInt(temp[5]) : Integer
                                        .parseInt(temp[3]), Math.abs(Integer
                                        .parseInt(temp[4])
                                        - Integer.parseInt(temp[2])), Math
                                        .abs(Integer.parseInt(temp[5])
                                                - Integer.parseInt(temp[3])));
                    }
                } else if (temp[0].equals("Line")) {
                    g.drawLine(Integer.parseInt(temp[2]),
                            Integer.parseInt(temp[3]), Integer.parseInt(temp[4]),
                            Integer.parseInt(temp[5]));
                }
            }
            if (start != null && end != null) {
                if (color.equals("Red")) {
                    g.setColor(Color.RED);
                } else if (color.equals("Green")) {
                    g.setColor(Color.GREEN);
                } else if (color.equals("Blue")) {
                    g.setColor(Color.BLUE);
                } else if (color.equals("Black")) {
                    g.setColor(Color.BLACK);
                }
                if (shape.equals("Oval")) {
                    if (fillBox.isSelected()) {
                        g.fillOval(start.x > end.x ? end.x : start.x,
                                start.y > end.y ? end.y : start.y,
                                Math.abs(end.x - start.x),
                                Math.abs(end.y - start.y));
                    } else {
                        g.drawOval(start.x > end.x ? end.x : start.x,
                                start.y > end.y ? end.y : start.y,
                                Math.abs(end.x - start.x),
                                Math.abs(end.y - start.y));
                    }
                } else if (shape.equals("Rectangle")) {
                    if (fillBox.isSelected()) {
                        g.fillRect(start.x > end.x ? end.x : start.x,
                                start.y > end.y ? end.y : start.y,
                                Math.abs(end.x - start.x),
                                Math.abs(end.y - start.y));
                    } else {
                        g.drawRect(start.x > end.x ? end.x : start.x,
                                start.y > end.y ? end.y : start.y,
                                Math.abs(end.x - start.x),
                                Math.abs(end.y - start.y));
                    }
                } else if (shape.equals("Line")) {
                    g.drawLine(start.x, start.y, end.x, end.y);
                }
            }
        }
        @Override
        public void mouseDragged(MouseEvent arg0) {
            // TODO Auto-generated method stub
            end = arg0.getPoint();
            repaint();
        }
        @Override
        public void mouseMoved(MouseEvent arg0) {
            // TODO Auto-generated method stub
        }
        @Override
        public void mouseClicked(MouseEvent arg0) {
            // TODO Auto-generated method stub
        }
        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub
        }
        @Override
        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub
        }
        @Override
        public void mousePressed(MouseEvent arg0) {
            // TODO Auto-generated method stub
            start = arg0.getPoint();
        }
        @Override
        public void mouseReleased(MouseEvent arg0) {
            // TODO Auto-generated method stub
            if (start != null && end != null) {
                itemsDrawn.add(shape + " " + color + " " + start.x + " " + start.y
                        + " " + end.x + " " + end.y + " " + fillBox.isSelected());
            }
            start = null;
            end = null;
        }
    }
    Missing things for the code check boxes for color gradients, line width and dashed.

    would help if answer was included in code.

    thank you
    Last edited by sskc248; 11-04-2014 at 08:50 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Java 2d application help

    Please edit your post and wrap your code with highlight tags:
    [code]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    Do you have any specific java programming questions?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    sskc248 is offline Member
    Join Date
    Nov 2014
    Posts
    3
    Rep Power
    0

    Default Re: Java 2d application help

    Hi,
    I just need help with fixing this: Missing things for the code (check boxes for color gradients, line width and dashed)

    It would be great help if you can fix that for me in program.

    Thanks

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Java 2d application help

    What have you tried?

    Work on the items in your list one at a time. If you have problems, post your code and any error messages or questions here.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    sskc248 is offline Member
    Join Date
    Nov 2014
    Posts
    3
    Rep Power
    0

    Default Re: Java 2d application help

    So I'm trying to fix color gradients, line width and dashed lines so when ever I try to fix them they say it cannot understand what I'm trying to do.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Java 2d application help

    they say it cannot understand what I'm trying to do.
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: 08-26-2012, 01:49 AM
  2. Replies: 2
    Last Post: 06-05-2012, 02:34 PM
  3. Replies: 0
    Last Post: 03-24-2012, 04:22 AM
  4. Replies: 0
    Last Post: 11-25-2011, 06:16 AM
  5. Replies: 3
    Last Post: 12-04-2009, 12:10 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
  •