{"id":806,"date":"2021-01-11T19:15:35","date_gmt":"2021-01-11T19:15:35","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/?page_id=806"},"modified":"2021-01-11T19:22:44","modified_gmt":"2021-01-11T19:22:44","slug":"mousing","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/events\/mousing\/","title":{"rendered":"Mousing"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">A &#8216;Scribble&#8217; Program<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how to use <em>Mouse Events<\/em>.&nbsp; It&#8217;s a simple &#8216;Scribble&#8217; program that uses a combination of the mouse and the keyboard to draw lines and ovals in various colors.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The cursor is changed to a crosshair when inside the application panel.<\/li><li>Mouse dragging is used to draw lines.<\/li><li>Mouse double-clicking is used to draw ovals.<\/li><li>The SHIFT and CTRL keys in combination with the left and right mouse buttons choose the color of the line or oval being drawn.<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [22,23,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105]; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport java.awt.Color;\nimport java.awt.Container;\nimport java.awt.Cursor;\nimport java.awt.Graphics;\nimport java.awt.Point;\n\nimport java.awt.event.InputEvent;\nimport java.awt.event.MouseEvent;\nimport java.awt.event.MouseListener;\nimport java.awt.event.MouseMotionListener;\n\nimport javax.swing.JFrame;\nimport javax.swing.JPanel;\n\nclass MouseScribblePanel extends JPanel\n{\n  \/\/ Constructor\n  public MouseScribblePanel()\n  {\n    addMouseListener( new MouseHandler() );\n    addMouseMotionListener( new MouseMotionHandler() );\n    setBackground(Color.RED);\n  }\n  \n  \/\/ Utility method to determine the color to use\n  private Color getEventColor(MouseEvent e)\n  {\n    Color color = Color.WHITE; \/\/ No modifiers means white\n    \n    int modifiers = e.getModifiers();\n    if ( (modifiers &amp; InputEvent.SHIFT_MASK) != 0)\n    {\n      color = Color.BLACK; \/\/ SHIFT key means black\n    }\n    else if ( (modifiers &amp; InputEvent.CTRL_MASK) != 0)\n    {\n      color = Color.ORANGE; \/\/ CTRL key means orange\n    }\n    else if ( (modifiers &amp; InputEvent.META_MASK) != 0)\n    {\n      color = Color.BLUE; \/\/ Mouse button 2 means blue\n    }\n    return color;\n  }\n  \n  \/\/\/\/\/ Private Data \/\/\/\/\/\n  private Point m_start, m_end;\n  \n  \/\/\/\/\/\/\/\/\/\/ Inner classes \/\/\/\/\/\/\/\/\n  \n  class MouseHandler implements MouseListener\n  {\n    \/\/ MouseListener method\n    public void mousePressed(MouseEvent e)\n    {\n      int x = e.getX();\n      int y = e.getY();\n      m_start = new Point(x, y);\n      m_end = new Point(x, y);\n      int clickCount = e.getClickCount();\n      if (clickCount == 2) \/\/ double click?\n      {\n        Graphics g = getGraphics();\n        g.setColor(getEventColor(e));\n        g.drawOval(m_start.x, m_start.y, 70, 30);\n      }\n    }\n    \n    public void mouseEntered(MouseEvent e)\n    {\n      setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));\n    }\n    \n    public void mouseExited(MouseEvent e)\n    {\n      setCursor(Cursor.getDefaultCursor());\n    }\n    \n    \/\/ Unused MouseListener methods\n    public void mouseClicked(MouseEvent e)\n    {}\n    public void mouseReleased(MouseEvent e)\n    {}\n  }\n  \n  class MouseMotionHandler implements MouseMotionListener\n  {\n    public void mouseDragged(MouseEvent e)\n    {\n      int x = e.getX();\n      int y = e.getY();\n      m_end = new Point(x, y);\n      Graphics g = getGraphics();\n      g.setColor(getEventColor(e));\n      g.drawLine(m_start.x, m_start.y, m_end.x, m_end.y);\n      m_start = new Point(x, y); \/\/ Drag from here next\n    }\n    \n    \/\/ Unused MouseMotionListener methods\n    public void mouseMoved(MouseEvent e)\n    {}\n  }\n}\n\nclass MouseScribbleFrame extends JFrame\n{\n  public MouseScribbleFrame()\n  {\n    setTitle(&quot;Mouse Scribble&quot;);\n    setSize(300, 200);\n    setDefaultCloseOperation(EXIT_ON_CLOSE);\n    Container contentPane = getContentPane();\n    contentPane.add(new MouseScribblePanel());\n  }\n}\n\npublic class MouseScribble\n{\n  public static void main(String&#x5B;] args)\n  {\n    MouseScribbleFrame frame = new MouseScribbleFrame();\n    frame.setVisible(true);\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s the result of&nbsp; a scribble session.&nbsp; Note the <em>crosshair cursor<\/em> at the right center.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"300\" height=\"200\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/MouseScribble.gif\" alt=\"\" class=\"wp-image-810\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:23px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>Note:<\/strong> An important thing to realize about this program: It does not retain any information about what has been drawn.&nbsp; So, if you draw something on the panel, and then occlude the window, or resize it, or iconify and then restore it, all the drawing will be gone.<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>A &#8216;Scribble&#8217; Program Here&#8217;s an example of how to use Mouse Events.&nbsp; It&#8217;s a simple &#8216;Scribble&#8217; program that uses a combination of the mouse and the keyboard to draw lines and ovals in various colors. Note: The cursor is changed to a crosshair when inside the application panel. Mouse dragging is used to draw lines. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":69,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_eb_attr":"","_uag_custom_page_level_css":"","ocean_post_layout":"left-sidebar","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"ocs-course-topics-sidebar","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","footnotes":""},"class_list":["post-806","page","type-page","status-publish","hentry","entry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"ocean-thumb-m":false,"ocean-thumb-ml":false,"ocean-thumb-l":false},"uagb_author_info":{"display_name":"Bryan Higgs","author_link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/author\/bryan\/"},"uagb_comment_info":0,"uagb_excerpt":"A &#8216;Scribble&#8217; Program Here&#8217;s an example of how to use Mouse Events.&nbsp; It&#8217;s a simple &#8216;Scribble&#8217; program that uses a combination of the mouse and the keyboard to draw lines and ovals in various colors. Note: The cursor is changed to a crosshair when inside the application panel. Mouse dragging is used to draw lines.&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/comments?post=806"}],"version-history":[{"count":3,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/806\/revisions"}],"predecessor-version":[{"id":812,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/806\/revisions\/812"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/69"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/media?parent=806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}