Results 1 to 5 of 5
  1. #1
    kastraki2000 is offline Member
    Join Date
    Apr 2011
    Location
    Greece/Athens
    Posts
    6
    Rep Power
    0

    Default use of exception

    hi again....:rolleyes: i finish with my applet (with left mouseclick increases a shape and with right click decreases it),but i have one last problem.when a shape grows a lot and enters in an other shape both of them grows with the mouseclick.I thought that usage of an exception is right to solve the problem,but i have never used try and catch in my code.Could anybody help me?i have my code right here
    Java Code:
    package ergasia_java;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.applet.*;
    import java.awt.event.*;
    
    public class MyApplet extends Applet implements MouseListener {
    
        int xtel, ytel;  //συντεταγμένες ποντικιού στην τελευταία θέση
        
        FilledCircle p = new FilledCircle(50, 50, 50, Color.YELLOW);
        FilledCircle c = new FilledCircle(200, 50, 100, Color.ORANGE);
        FilledRectangle q = new FilledRectangle(350, 50, 40, 80, Color.GREEN);
        FilledRectangle s = new FilledRectangle(550, 50, 60, 100, Color.GRAY);
        FilledShape[] array = new FilledShape[4];
        
    
    
        @Override
        public void init() {
            addMouseListener(this);  //προσθέτουμε τον MouseListener στην Applet
    
        }
    
    
        @Override
        public void paint(Graphics g) {
            array[0] = c;
            array[1] = p;
            array[2] = q;
            array[3] = s;
            for (int i = 0; i < array.length; i++) {
    
                g.setColor(array[i].fillColor);
                array[i].draw(g);
           
        }
    
        }
    
        public void mouseClicked(MouseEvent e) {
    
            xtel = e.getX();  // δίνουμε τις συντεταγμένες που βρίσκεται το ποντίκι όταν κάνουμε click
            ytel = e.getY();
    
    
            if (e.getButton() == MouseEvent.BUTTON1) {
    
            for (int i = 0; i < array.length; i++) {
                if (array[i].contains(e.getX(), e.getY())) {
                    array[i].increase(1.1);
                }
               
            }
            repaint();
            }
    
     else if (e.getButton() == MouseEvent.BUTTON3) {
         for (int i = 0; i < array.length; i++) {
                if (array[i].contains(e.getX(), e.getY())) {
                    array[i].decrease(1.1);
                }
             }
    
          repaint();
    
    
    
            }
        }
    
        public void mousePressed(MouseEvent e) {
        }
    
        public void mouseReleased(MouseEvent e) {
        }
    
        public void mouseEntered(MouseEvent e) {
        }
    
        public void mouseExited(MouseEvent e) {
        }
    }
    thanks a lot and sorry for my poor English.!!!:(

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I don't know if an exception is really the rightway to go. Exceptions should be used to catch some exceptional condition and handle it cleanly. You use them for something you can't predict too happen. The two shapes overlapping is something you can predict will happen.

    I'd suggest testing for the overlap before actually growing the shape, if that grow will cause an overlap don't do it.

  3. #3
    kastraki2000 is offline Member
    Join Date
    Apr 2011
    Location
    Greece/Athens
    Posts
    6
    Rep Power
    0

    Default

    thanks a lot my friend it was something i didnt know about exceptions and their usage.thanks i will try to make something with the overlap...:)

  4. #4
    kastraki2000 is offline Member
    Join Date
    Apr 2011
    Location
    Greece/Athens
    Posts
    6
    Rep Power
    0

    Default

    i found the public static boolean isOverlapping but it is only for two shapes.does anybody knows if there is something for more than 2 shapes?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Why do you need it for more than two shapes?
    Any overlap check will involve two shapes...

    If you need to check a shape against lots of other shapes, then run the check once for each shape.

Similar Threads

  1. unreported exception java.lang.Exception
    By Marisabel in forum New To Java
    Replies: 6
    Last Post: 05-01-2011, 03:21 AM
  2. Replies: 5
    Last Post: 02-14-2011, 11:39 PM
  3. Replies: 1
    Last Post: 02-06-2011, 05:38 AM
  4. Replies: 4
    Last Post: 10-25-2010, 07:42 PM
  5. Replies: 1
    Last Post: 03-08-2008, 06:41 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
  •