java programming - ClickMe_appl

NOTICE: The content of this document should contain java related topics.

This is the java-ClickMe_appl document

Table of contents 1

ClickMe_appl............................................................................................................... 2 1.1

ClickMe.java............................................................................................................2

1.2

ClickMeApp.java..................................................................................................... 3

1.3

Spot.java...................................................................................................................4

This is a legal notice, so it is important.

Copyright © 2002-2012 hvanbelle All rights reserved.

java programming - ClickMe_appl

Note: Hopefolly their are not to many errors in the code examples.

1. ClickMe_appl Hint: This section contains java-ClickMe_appl examples

1.1. ClickMe.java /** * ClickMe.java is used by ClickMeApp.java. ClickMe is a * component that puts a spot wherever you click. It requires * another file: Spot.java. */ import javax.swing.BorderFactory; import javax.swing.JComponent; import java.awt.*; import java.awt.event.*; public class ClickMe extends JComponent implements MouseListener { private Spot spot = null; private static final int RADIUS = 7; private Color spotColor = new Color(107, 116, 2); //olive /** Creates and initializes the ClickMe component. */ public ClickMe() { addMouseListener(this); //Hint at good sizes for this component. setPreferredSize(new Dimension(RADIUS * 30, RADIUS * 15)); setMinimumSize(new Dimension(RADIUS * 4, RADIUS * 4)); //Request a black line around this component. setBorder(BorderFactory.createLineBorder(Color.BLACK)); } /** * Paints the ClickMe component. This method is * invoked by the Swing component-painting system. */ public void paintComponent(Graphics g) { /** * Copy the graphics context so we can change it. * Cast it to Graphics2D so we can use antialiasing. */ Graphics2D g2d = (Graphics2D)g.create(); //Turn on antialiasing so that painting is smooth. g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //Paint the background. g2d.setColor(Color.WHITE);

This is a legal notice, so it is important.

Page 2

Copyright © 2002-2012 hvanbelle All rights reserved.

java programming - ClickMe_appl

g2d.fillRect(0, 0, getWidth() - 1, getHeight() - 1); //Paint the spot. if (spot != null) { int radius = spot.getSize(); g2d.setColor(spotColor); g2d.fillOval(spot.x - radius, spot.y - radius, radius * 2, radius * 2); } } //Methods required by the MouseListener interface. public void mousePressed(MouseEvent event) { if (spot == null) { spot = new Spot(); spot.setSize(RADIUS); } spot.x = event.getX(); spot.y = event.getY(); repaint(); } public void mouseClicked(MouseEvent event) {} public void mouseReleased(MouseEvent event) {} public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {} }

1.2. ClickMeApp.java /** * ClickMeApp.java is an application that compiles and runs * under J2SE v5.0. It requires two additional files: * ClickMe.java * Spot.java */ import import import import

javax.swing.SwingUtilities; javax.swing.JLabel; javax.swing.JFrame; java.awt.BorderLayout;

public class ClickMeApp implements Runnable { /** * This constructor creates an instance of ClickMeApp, * which creates and shows a window containing a * ClickMe component. */ public ClickMeApp() { /* * Tells the event-dispatching thread (used to * display and handle events of a Swing GUI) to * call the run method of "this" (the ClickMeApp * object this constructor created). The * argument to invokeLater must implement the * Runnable interface, which guarantees that * it defines the run method. */

This is a legal notice, so it is important.

Page 3

Copyright © 2002-2012 hvanbelle All rights reserved.

java programming - ClickMe_appl

SwingUtilities.invokeLater(this); } /** * Creates and shows the GUI. This method should be * invoked on the event-dispatching thread. */ public void run() { createAndShowGUI(); } /** * Brings up a window that contains a ClickMe component. * For thread safety, this method should be invoked from * the event-dispatching thread. */ private static void createAndShowGUI() { //Make sure we have nice window decorations. JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window. JFrame frame = new JFrame("ClickMeApp"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set up the layout manager. frame.setLayout(new BorderLayout()); //Add the ClickMe component. ClickMe component = new ClickMe(); frame.add(component, BorderLayout.CENTER); //Add an explanatory label. frame.add(new JLabel("Click within the rectangle."), BorderLayout.SOUTH); //Display the window. frame.pack(); frame.setVisible(true); } //This method is automatically executed. public static void main(String[] args) { //Create an instance of ClickMeApp. new ClickMeApp(); } }

1.3. Spot.java /** * Spot.java is used by ClickMe.java. */ public class Spot { //instance variables private int size; public int x, y;

This is a legal notice, so it is important.

Page 4

Copyright © 2002-2012 hvanbelle All rights reserved.

java programming - ClickMe_appl

//constructor public Spot() { x = -1; y = -1; size = 1; } //methods for access to the size instance variable public void setSize(int newSize) { if (newSize >= 0) { size = newSize; } } public int getSize() { return size; } } This is a legal notice, so it is important.

This is a legal notice, so it is important.

Page 5

Copyright © 2002-2012 hvanbelle All rights reserved.