Results 1 to 4 of 4
  1. #1
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default just started paint program. am i going the right direction?

    Hey all. For practice, I am making a very very simple "paint" type program. The only features will be changeable color and different types of drawable shapes. The shape/line will then be created with one corner/end point at the spot where you clicked and the other following the mouse and changing in size/location as you move it and being formed when you click again, just like in paint.. Right now I have the GUI layout set up and I implemented an ActionEvent in the client. The actionPerformed method sends a different integer to the drawing pad (called PaintPanel) depending on which button I clicked on, which will then interpret it accordingly in the paintComponent method when I click/drag on the drawing area. Just posting it here to see if you veterans have any tips on my organization/coding... just a newbie here. Also if you think this will be a good way to do it.

    Here's the client code:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class Paint implements ActionListener {
      public static void main(String[] args) {
        Paint paint = new Paint();
      }
      
      JFrame frame = new JFrame();
      JPanel west = new JPanel(new GridLayout(9, 1));
      JButton color = new JButton("Color");
      JButton pencil = new JButton("Pencil");
      JButton line = new JButton("Line");
      JButton rect = new JButton("Rectangle");
      JButton ellipse = new JButton("Ellipse");
      JButton image = new JButton("Image");
      JButton eraser = new JButton("Eraser");
      PaintPanel pad = new PaintPanel();
      Color c;
      
      //Creates layout.
      public Paint() {
        frame.setTitle("HungSoft Paint");
        frame.setSize(new Dimension(500, 300));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        
        pad.setPreferredSize(new Dimension(400, 300));
        pad.setBackground(Color.WHITE);
        
        west.setBackground(Color.GRAY);
        west.add(color);
        west.add(pencil);
        west.add(line);
        west.add(rect);
        west.add(ellipse);
        west.add(image);
        west.add(eraser);
        
        frame.add(west, BorderLayout.WEST);
        frame.add(pad, BorderLayout.CENTER);
        frame.pack();
        
        frame.setVisible(true);
      }
      
      
     //Tells the panel which tool to use.
      public void actionPerformed(ActionEvent e) {
        if(e.getSource() == pencil)
          pad.setTool(0);
        if(e.getSource() == line)
          pad.setTool(1);
        if(e.getSource() == rect)
          pad.setTool(2);
        if(e.getSource() == ellipse)
          pad.setTool(3);
        if(e.getSource() == image)
          pad.setTool(4);
        if(e.getSource() == eraser)
          pad.setTool(5);
      }
    }
    Here's the PaintPanel code:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class PaintPanel extends JPanel {
      private int tool;
      private Color c = Color.BLACK;
      
      //Will later put in if statements to choose tool based on the last selected tool, sent to the object through the setTool method.
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
      }
      
      public void setTool(int tool) {
        this.tool = tool;
      }
      
      public void setColor(Color c) {
        this.c = c;
      }
    }
    Last edited by diggitydoggz; 12-30-2008 at 09:30 PM. Reason: typo

  2. #2
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default

    Also - does anybody have ideas on how I would be able to drag out shapes and not have them leave behind their marks on the pad? By this I mean the same way that you'd draw in microsoft paint. Like when you draw a line, you just click and the first endpoint is there and then the line just follows your pointer until you click again and then it is drawn in that spot. I don't have any idea of how I'd do this... just recently started learning about GUI's.
    Last edited by diggitydoggz; 12-30-2008 at 09:31 PM.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Here's an idea about dragging shapes onto your drawing surface.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.AffineTransform;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.*;
    
    public class DragOut extends JComponent {
        ShapePanel shapePanel = new ShapePanel(this);
        DropPanel dropPanel = new DropPanel();
        Shape shape;
        Point offset = new Point();
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            if(shape != null) {
                g2.setPaint(Color.red);
                g2.draw(shape);
            }
        }
    
        public void setShape(Shape shape, Point p) {
            this.shape = shape;
            Rectangle r = shape.getBounds();
            offset.x = p.x - r.x;
            offset.y = p.y - r.y;
            setVisible(true);
            repaint();
        }
    
        public void moveShape(Point p) {
            Rectangle r = shape.getBounds();
            double x = p.x - r.x - offset.x;
            double y = p.y - r.y - offset.y;
            AffineTransform at = AffineTransform.getTranslateInstance(x, y);
            shape = at.createTransformedShape(shape);
            repaint();
        }
    
        public void dropShape(Point p) {
            Point xp = SwingUtilities.convertPoint(shapePanel, p, dropPanel);
            double x = xp.x - p.x;
            double y = xp.y - p.y;
            AffineTransform at = AffineTransform.getTranslateInstance(x, y);
            dropPanel.add(at.createTransformedShape(shape));
            setVisible(false);
            repaint();
        }
    
        private JPanel getContent() {
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(shapePanel, "Before");
            panel.add(dropPanel);
            return panel;
        }
    
        public static void main(String[] args) {
            DragOut test = new DragOut();
            test.setVisible(false);
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setGlassPane(test);
            f.add(test.getContent());
            f.setSize(500,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }
    
    class ShapePanel extends JPanel {
        DragOut glassPane;
        Shape[] shapes;
    
        public ShapePanel(DragOut glassPane) {
            this.glassPane = glassPane;
            addMouseListener(ma);
            addMouseMotionListener(ma);
        }
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            int w = getWidth();
            int h = getHeight();
            if(shapes == null) {
                int R = Math.min(w, h)/3;
                int[] sides = { 3, 5, 6 };
                shapes = new Shape[sides.length];
                for(int i = 0; i < shapes.length; i++) {
                    int[][] xy = getPolygonArrays(w/2, (i+1)*h/4, R, sides[i]);
                    shapes[i] = new Polygon(xy[0], xy[1], sides[i]);
                }
            }
            g2.setPaint(Color.red);
            g2.drawLine(w-1, 0, w-1, h-1);
            g2.setPaint(Color.blue);
            for(int i = 0; i < shapes.length; i++) {
                g2.draw(shapes[i]);
            }
        }
    
        public Dimension getPreferredSize() {
            return new Dimension(100,300);
        }
    
        private int[][] getPolygonArrays(int cx, int cy, int R, int sides) {
            int[] x = new int[sides];
            int[] y = new int[sides];
            double thetaInc = 2*Math.PI/sides;
            double theta = (sides % 2 == 0) ? thetaInc : -Math.PI/2;
            for(int j = 0; j < sides; j++) {
                x[j] = (int)(cx + R*Math.cos(theta));
                y[j] = (int)(cy + R*Math.sin(theta));
                theta += thetaInc;
            }
            return new int[][]{ x, y };
        }
    
        /** Use MouseInputAdapter for j2se 1.5- */
        private MouseAdapter ma = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                Point p = e.getPoint();
                for(int i = 0; i < shapes.length; i++) {
                    if(shapes[i].contains(p)) {
                        glassPane.setShape(shapes[i], p);
                        break;
                    }
                }
            }
    
            public void mouseDragged(MouseEvent e) {
                glassPane.moveShape(e.getPoint());
            }
    
            public void mouseReleased(MouseEvent e) {
                glassPane.dropShape(e.getPoint());
            }
        };
    }
    
    class DropPanel extends JPanel {
        List<Shape> list = new ArrayList<Shape>();
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setPaint(Color.green.darker());
            for(int i = 0; i < list.size(); i++) {
                g2.draw(list.get(i));
            }
        }
    
        public void add(Shape shape) {
            list.add(shape);
            repaint();
        }
    }

  4. #4
    diggitydoggz is offline Member
    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    0

    Default

    holy crap. understanding that code is going to be a whole 'nother project on it's own (not because of the way it's written but because I'm new). thanks a bunch for making that though. i'm going to get started on trying to understand it.

Similar Threads

  1. [SOLVED] Need direction...
    By hotice1027 in forum New To Java
    Replies: 5
    Last Post: 11-28-2008, 09:03 AM
  2. I need direction too. output control
    By bornwithnoname in forum New To Java
    Replies: 0
    Last Post: 11-23-2008, 03:57 AM
  3. Replies: 14
    Last Post: 09-18-2008, 04:41 AM
  4. other than paint repaint
    By amith in forum Java 2D
    Replies: 1
    Last Post: 07-01-2008, 11:39 PM
  5. paint() and paintComponent()
    By goldhouse in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 03:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •