// MovingGraphicalObjectsApplet.java (c) Kari Laitinen // http://www.naturalprogramming.com // 2011-02-10 File created. // 2011-02-14 Latest modification. /* It might be best to use the standard Polygon class to represent a Triangle. The Polygon class has contains() and translate() methods !!!! */ import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; import java.awt.geom.* ; import java.util.ArrayList ; abstract class GraphicalObject { Point.Double center_point ; // object_velocity specifies the number of pixels the object // will be moved in a single movement operation. double object_velocity = 4.0 ; Color object_color = Color.RED ; public Point2D get_center_point() { return center_point ; } public void set_color( Color new_color ) { object_color = new_color ; } public void move_right() { center_point.setLocation( center_point.x + object_velocity, center_point.y ) ; } public void move_left() { center_point.setLocation( center_point.x - object_velocity, center_point.y ) ; } public void move_up() { center_point.setLocation( center_point.x, center_point.y - object_velocity); } public void move_down() { center_point.setLocation( center_point.x, center_point.y + object_velocity); } abstract public boolean contains_point( Point given_point ) ; public void move_relatively( double movement_in_direction_x, double movement_in_direction_y ) { center_point.setLocation( center_point.x + movement_in_direction_x, center_point.y + movement_in_direction_y ) ; } public void move_to_position( double new_position_x, double new_position_y ) { center_point.setLocation( new_position_x, new_position_y ) ; } abstract public void draw( Graphics2D graphics2D ) ; } class Triangle extends GraphicalObject { double side_length ; Polygon triangle_polygon = new Polygon() ; public Triangle( Point.Double given_center_point, double given_side_length ) { center_point = given_center_point ; side_length = given_side_length ; double distance_from_center_point_to_top_point = side_length / ( 2 * Math.cos( Math.PI / 6 ) ) ; double triangle_height = Math.cos( Math.PI / 6 ) * side_length ; double distance_from_center_point_to_bottom_line = triangle_height - distance_from_center_point_to_top_point ; triangle_polygon.addPoint( (int) center_point.x, (int) ( center_point.y - distance_from_center_point_to_top_point ) ) ; triangle_polygon.addPoint( (int) ( center_point.x + side_length / 2 ), (int) ( center_point.y + distance_from_center_point_to_bottom_line ) ) ; triangle_polygon.addPoint( (int) ( center_point.x - side_length / 2 ), (int) ( center_point.y + distance_from_center_point_to_bottom_line ) ) ; } public boolean contains_point( Point given_point ) { return ( triangle_polygon.contains( given_point ) ) ; } public void move_relatively( double movement_in_direction_x, double movement_in_direction_y ) { center_point.setLocation( center_point.x + movement_in_direction_x, center_point.y + movement_in_direction_y ) ; // Here we move the triangle by using the translate() method of // the Polygon class. A more accurate movement might be achieved // by re-creating the Polygon object. triangle_polygon.translate( (int) movement_in_direction_x, (int) movement_in_direction_y ) ; } public void draw( Graphics2D graphics2D ) { graphics2D.setColor( object_color ) ; graphics2D.fill( triangle_polygon ) ; } } class MovingGraphicalObjectsPanel extends JPanel implements MouseListener, MouseMotionListener { int panel_width, panel_height ; GraphicalObject object_being_moved = null ; ArrayList movable_objects = new ArrayList(); Point previous_mouse_position ; public MovingGraphicalObjectsPanel( int given_width, int given_height ) { panel_width = given_width ; panel_height = given_height ; movable_objects.add( new Triangle( new Point.Double( 140, 140 ), 200 ) ) ; movable_objects.add( new Triangle( new Point.Double( 140, 340 ), 200 ) ) ; movable_objects.add( new Triangle( new Point.Double( 340, 340 ), 200 ) ) ; addMouseListener(this); addMouseMotionListener(this); } // The following dummy methods are needed to satisfy listener // interfaces. public void mouseMoved( MouseEvent event ) {} public void mouseClicked( MouseEvent event ) {} public void mouseEntered( MouseEvent event ) {} public void mouseExited( MouseEvent event ) {} public void mousePressed( MouseEvent event ) { Point mouse_position = event.getPoint() ; int object_index = movable_objects.size() - 1 ; boolean search_for_clicked_object_is_ready = false ; while ( search_for_clicked_object_is_ready == false ) { if ( movable_objects.get( object_index ). contains_point( mouse_position ) ) { object_being_moved = movable_objects.get( object_index ); // We'll remove the clicked object from the list. // When we add it to the end of the list, it will be // the last object to be drawn. movable_objects.remove( object_being_moved ) ; movable_objects.add( object_being_moved ) ; //object_being_moved.aktivoi_pallo() ; search_for_clicked_object_is_ready = true ; } if ( object_index > 0 ) { object_index -- ; } else { search_for_clicked_object_is_ready = true ; } } previous_mouse_position = mouse_position ; repaint() ; } public void mouseReleased( MouseEvent event ) { if ( object_being_moved != null ) { //object_being_moved.deactivate_ball() ; object_being_moved = null ; repaint() ; } } public void mouseDragged( MouseEvent event ) { if ( object_being_moved != null ) { Point new_mouse_position = event.getPoint() ; int mouse_movement_x = new_mouse_position.x - previous_mouse_position.x ; int mouse_movement_y = new_mouse_position.y - previous_mouse_position.y ; previous_mouse_position = new_mouse_position ; object_being_moved.move_relatively( mouse_movement_x, mouse_movement_y ) ; repaint() ; } } public void paintComponent( Graphics graphics ) { // The superclass version of paintComponent() must be called first. super.paintComponent( graphics ) ; Graphics2D graphics2D = (Graphics2D) graphics ; for ( GraphicalObject object_to_draw : movable_objects ) { object_to_draw.draw( graphics2D ) ; } // test_triangle.draw( graphics2D ) ; } } public class MovingGraphicalObjectsApplet extends JApplet { public void init() { getContentPane().add( new MovingGraphicalObjectsPanel( getSize().width, getSize().height ) ) ; } } /* A triangle can be drawn as a GeneralPath with the following statements: GeneralPath triangle_path = new GeneralPath() ; double distance_from_center_point_to_top_point = side_length / ( 2 * Math.cos( Math.PI / 6 ) ) ; double triangle_height = Math.cos( Math.PI / 6 ) * side_length ; double distance_from_center_point_to_bottom_line = triangle_height - distance_from_center_point_to_top_point ; triangle_path.moveTo( (float) center_point.x, (float) ( center_point.y - distance_from_center_point_to_top_point ) ) ; triangle_path.lineTo( (float) ( center_point.x + side_length / 2 ), (float) ( center_point.y + distance_from_center_point_to_bottom_line ) ) ; triangle_path.lineTo( (float) ( center_point.x - side_length / 2 ), (float) ( center_point.y + distance_from_center_point_to_bottom_line ) ) ; triangle_path.closePath() ; */