Results 1 to 4 of 4

Thread: finished paint!

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

    Default finished paint!

    I finished paint! I decided not to use the "drag" feature and to instead make my shapes by clicking twice. I'm real proud of myself... I think the algorithm I used was pretty good. I am pretty new to java and this was one of my trickiest projects so far. Anyways always good to get constructive feedback. Here's the fully compilable source code for anyone who would be kind enough to give some advice (the "Image" button has no function, I kind of just threw it in there).

    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 = Color.BLACK;
      
      public Paint() {
        frame.setTitle("HungSoft Paint");
        frame.setSize(new Dimension(500, 300));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.setLocationRelativeTo(null);
        
        pad.setPreferredSize(new Dimension(400, 300));
        
        pencil.addActionListener(this);
        line.addActionListener(this);
        rect.addActionListener(this);
        ellipse.addActionListener(this);
        image.addActionListener(this);
        eraser.addActionListener(this);
        
        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();
        
        pad.addMouseListener(new MouseInputAdapter() {
          public void mousePressed(MouseEvent event) {
            if(pad.getTool() == 0 || pad.getTool() == 5) {
              pad.setTrack(1);
              pad.setPoint(event.getX(), event.getY(), c);
            }
          }
          
          public void mouseClicked(MouseEvent event) {
            if(pad.getTool() != 0 && pad.getTool() != 5) {
              pad.setTrack(pad.getTrack() + 1);
              pad.setPoint(event.getX(), event.getY(), c);
            }
          }
          
          public void mouseReleased(MouseEvent event) {
            if((pad.getTool() == 0 || pad.getTool() == 5)) {
              pad.setTrack(0);
            }
          } 
        });
        
        pad.addMouseMotionListener(new MouseInputAdapter() {
           public void mouseDragged(MouseEvent event) {
            if((pad.getTool() == 0 || pad.getTool() == 5) && pad.getTrack() == 1) {
              pad.setPoint(event.getX(), event.getY(), c);
            }
          }
        });
        
        color.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            Color color = JColorChooser.showDialog(frame, "Please select color.", c);
            if(color != null) {
              c = color;
            }
          }
        });
        
        frame.setVisible(true);
      }
      
      public void actionPerformed(ActionEvent e) {
        if(e.getSource() == pencil) {
          pad.setTool(0);
          System.out.println(pad.getTool());
      }
        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);
      }
    }
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    
    public class PaintPanel extends JPanel {
      private int tool;
      private ColoredPoint p = new ColoredPoint();
      private Point d = new Point();
      private ColoredPoint temp = new ColoredPoint();
      private int track = 0;
      private ArrayList<ColoredShape> sList = new ArrayList<ColoredShape>();
      
      //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);
        setBackground(Color.WHITE);
        setForeground(Color.BLACK);
        Graphics2D g2 = (Graphics2D)g;
        g2.setStroke(new BasicStroke(2));
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        if(tool == 0) {
          if(track == 1){
            sList.add(new ColoredPoint(p.x, p.y, p.c));
          }
        }
        else if(tool == 1) {
          if(track % 2 == 1) {
            temp.setPoint(p.x, p.y, p.c);
          }
          else if(track % 2 == 0) {
            sList.add(new ColoredLine(temp.x, temp.y, p.x, p.y, p.c));
          }
        }
        else if(tool == 2) {
          if(track % 2 == 1) {
            temp.setPoint(p.x, p.y, p.c);
          }
          else if(track % 2 == 0) {
            if(p.x > temp.x && p.y > temp.y) {
              sList.add(new ColoredRectangle(temp.x, temp.y, p.x - temp.x, p.y - temp.y, p.c));
            }
            else if(p.x < temp.x && p.y < temp.y) {
              sList.add(new ColoredRectangle(p.x, p.y, temp.x - p.x, temp.y - p.y, p.c));
            }
            else if(p.x < temp.x && p.y > temp.y) {
              sList.add(new ColoredRectangle(p.x, temp.y, temp.x - p.x, p.y - temp.y, p.c));
            }
            else if(p.x > temp.x && p.y < temp.y) {
              sList.add(new ColoredRectangle(temp.x, p.y, p.x - temp.x, temp.y - p.y, p.c));
            }
         }
        }
        else if(tool == 3) {
          if(track % 2 == 1) {
            temp.setPoint(p.x, p.y, p.c);
          }
          else if(track % 2 == 0) {
            if(p.x > temp.x && p.y > temp.y) {
              sList.add(new ColoredOval(temp.x, temp.y, p.x - temp.x, p.y - temp.y, p.c));
            }
            else if(p.x < temp.x && p.y < temp.y) {
              sList.add(new ColoredOval(p.x, p.y, temp.x - p.x, temp.y - p.y, p.c));
            }
            else if(p.x < temp.x && p.y > temp.y) {
              sList.add(new ColoredOval(p.x, temp.y, temp.x - p.x, p.y - temp.y, p.c));
            }
            else if(p.x > temp.x && p.y < temp.y) {
              sList.add(new ColoredOval(temp.x, p.y, p.x - temp.x, temp.y - p.y, p.c));
            }
         }
        }
        else if(tool == 4) {
        }
        else if(tool == 5) {
          sList.add(new ColoredRectangle(p.x, p.y, 10, 10, Color.WHITE));
        }
        refresh(g2);
      }
      
      private void refresh(Graphics2D g2) {
        for(int i = 0; i < sList.size(); i++) {
          sList.get(i).drawShape(g2);
        }  
      }
      
      public void setTool(int tool) {
        this.tool = tool;
        track = 0;
      }
      
      public int getTool() {
        return tool;
      }
      
      public void setPoint(int x, int y, Color c) {
        p.setPoint(x, y, c);
        repaint();
      }
      
      public void setTrack(int track) {
        this.track = track;
      }
      
      public int getTrack() {
        return track;
      }
    }
    Here's the other classes/interfaces I made for it:

    Java Code:
    import java.awt.*;
    
    public interface ColoredShape {
      public void drawShape(Graphics2D g2);
    }
    
    import java.awt.*;
    
    public class ColoredRectangle extends Rectangle implements ColoredShape{
      public Color c;
      
      public ColoredRectangle(int x, int y, int width, int height, Color c) {
        super(x, y, width, height);
        this.c = c;
      }
      
      public ColoredRectangle() {
        super(0, 0, 0, 0);
        this.c = Color.BLACK;
      }
      
      public void drawShape(Graphics2D g2) {
        g2.setColor(c);
        g2.fillRect(x, y, width, height);
      }
    }
    
    import java.awt.*;
    
    public class ColoredOval implements ColoredShape {
      private int x;
      private int y;
      private int width;
      private int height;
      private Color c;
      
      public ColoredOval(int x, int y, int width, int height, Color c) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.c = c;
      }
      
      public void drawShape(Graphics2D g2) {
        g2.setColor(c);
        g2.fillOval(x, y, width, height);
      }
    }
    
    import java.awt.*;
    
    public class ColoredLine implements ColoredShape {
      private int x1;
      private int y1;
      private int x2;
      private int y2;
      private Color c;
      
      public ColoredLine(int x1, int y1, int x2, int y2, Color c) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
        this.c = c;
      }
      
      public void drawShape(Graphics2D g2) {
        g2.setColor(c);
        g2.drawLine(x1, y1, x2, y2);
      }
    }
    
    import java.awt.*;
    
    public class ColoredPoint extends Point implements ColoredShape{
      public Color c;
      
      public ColoredPoint(int x, int y, Color c) {
        super(x, y);
        this.c = c;
      }
      
      public ColoredPoint() {
        super(0, 0);
        this.c = Color.BLACK;
      }
      
      public void setPoint(int x, int y, Color c) {
        super.setLocation(x, y);
        this.c = c;
      }
      
      public void drawShape(Graphics2D g2) {
        g2.setColor(c);
        g2.fillOval(x, y, 3, 3);
      }
    }

  2. #2
    Gudradain is offline Member
    Join Date
    Nov 2008
    Posts
    44
    Rep Power
    0

    Default

    Nice job. I just test your program and it works fine.

    But how do you make the line, rectangle and ellipse to work?

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

    Default

    thanks for the comment.

    the rectangle, ellipse, line, and image work by pressing twice. the first time you click is one corner, the second time is the second corner. for the line, instead of the corner they are the endpoints. and for the ellipse instead of the corner it is the corner of the rectangle that the ellipse would be in... if that makes any sense

  4. #4
    Gudradain is offline Member
    Join Date
    Nov 2008
    Posts
    44
    Rep Power
    0

    Default

    Ok it work. The problem was that I was moving a little bit while clicking. If I keep my mouse immobile while clicking it's fine.

Similar Threads

  1. How to run a code when a download is finished
    By aneesahamedaa in forum New To Java
    Replies: 4
    Last Post: 10-14-2008, 12:37 PM
  2. passing parameters to paint mtd
    By themburu in forum Java Applets
    Replies: 3
    Last Post: 07-14-2008, 08:15 PM
  3. other than paint repaint
    By amith in forum Java 2D
    Replies: 1
    Last Post: 07-01-2008, 11:39 PM
  4. radio buttons and paint
    By gtraylo in forum Java Applets
    Replies: 1
    Last Post: 04-19-2008, 12:43 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
  •