Help with button and mouse

hi, every one

my problem is how to write code for the following :

- just i create a Button on aToolBar and Panel in the main frame

when i press the button and go to the pane and click any where on the panel the mouse must draw string at (x , y) for mouse .

please tell me how to make it as i try many times but i can't do it as i'm a java beginner

[380 byte] By [java4alla] at [2007-11-27 2:32:33]
# 1
where is your specific problem?button?toolbar?mouseListener?drawing?post what you've tried, and we'll try to steer you in the right direction
Michael_Dunna at 2007-7-12 2:48:20 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hi

This should help.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Main extends JPanel implements MouseListener{

/** Size of the panel. */

private static final Dimension PANEL_SIZE = new Dimension(200,200);

/** Background colour of the panel. */

private static final Color BACKGROUND_COLOUR = Color.BLACK;

/** String colour. */

private static final Color STRING_COLOUR = Color.YELLOW;

/** Boolean flag which determines when to draw the string. */

private boolean m_DisplayString;

/** Mouse X position. */

private int m_MouseX;

/** Mouse Y position. */

private int m_MouseY;

/** Constructor. */

public Main() {

m_DisplayString = false;

m_MouseX = 0;

m_MouseY = 0;

// Create panel and set size.

setLayout(new BorderLayout());

setPreferredSize(PANEL_SIZE);

// Add a mouse listener to this panel.

addMouseListener(this);

// Create a button and add a listener.

JButton button = new JButton("off");

button.addActionListener(new ButtonListener());

// Create a toolbar and add the button.

JToolBar toolBar = new JToolBar();

toolBar.add(button);

// Add the toolbar to the panel.

add(toolBar, BorderLayout.NORTH);

// Create frame and add this panel.

JFrame frame = new JFrame();

frame.getContentPane().add(this);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

frame.setVisible(true);

}

private class ButtonListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

JButton b = (JButton)e.getSource();

if (b.getText().equals("off")) {

b.setText("on");

m_DisplayString = true;

} else {

b.setText("off");

m_DisplayString = false;

repaint();

}

}

}

protected void paintComponent(Graphics g) {

g.setColor(BACKGROUND_COLOUR);

g.fillRect(0, 0, getWidth(), getHeight());

if (m_DisplayString) {

g.setColor(STRING_COLOUR);

g.drawString("java4all", m_MouseX, m_MouseY);

}

}

public void mousePressed(MouseEvent e) {

m_MouseX = e.getX();

m_MouseY = e.getY();

repaint();

}

// Required by mouseLisener.

public void mouseClicked(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

public static void main(String[] args) {

new Main();

}

}

Regards

Paul

paul_gaina at 2007-7-12 2:48:20 > top of Java-index,Desktop,Core GUI APIs...
# 3

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Main extends JPanel implements MouseListener{

/** Size of the panel. */

private static final Dimension PANEL_SIZE = new Dimension(200,200);

/** Background colour of the panel. */

private static final Color BACKGROUND_COLOUR = Color.BLACK;

/** String colour. */

private static final Color STRING_COLOUR = Color.YELLOW;

/** Boolean flag which determines when to draw the string. */

private boolean m_DisplayString;

/** Mouse X position. */

private int m_MouseX;

/** Mouse Y position. */

private int m_MouseY;

/** Constructor. */

public Main() {

m_DisplayString = false;

m_MouseX = 0;

m_MouseY = 0;

// Set the layout manager.

setLayout(new BorderLayout());

// Set the size of this panel.

setPreferredSize(PANEL_SIZE);

// Add a mouse listener to this panel.

addMouseListener(this);

// Create a button and add a listener.

JButton button = new JButton("off");

button.addActionListener(new ButtonListener());

// Create a toolbar and add the button.

JToolBar toolBar = new JToolBar();

toolBar.add(button);

// Add the toolbar to the panel.

add(toolBar, BorderLayout.NORTH);

// Create frame and add this panel.

JFrame frame = new JFrame();

frame.getContentPane().add(this);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

frame.setVisible(true);

}

private class ButtonListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

JButton b = (JButton)e.getSource();

if (b.getText().equals("off")) {

b.setText("on");

m_DisplayString = true;

} else {

b.setText("off");

m_DisplayString = false;

repaint();

}

}

}

protected void paintComponent(Graphics g) {

g.setColor(BACKGROUND_COLOUR);

g.fillRect(0, 0, getWidth(), getHeight());

if (m_DisplayString) {

g.setColor(STRING_COLOUR);

g.drawString("java4all", m_MouseX, m_MouseY);

}

}

public void mousePressed(MouseEvent e) {

if (m_DisplayString) {

m_MouseX = e.getX();

m_MouseY = e.getY();

repaint();

}

}

// Required by mouseLisener.

public void mouseClicked(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

public static void main(String[] args) {

new Main();

}

}

paul_gaina at 2007-7-12 2:48:20 > top of Java-index,Desktop,Core GUI APIs...