The frame opens and close normally but mouse click doesn't work.
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//Create a frame window that responds to mouse click
public class AWT3 extends Frame {
String Mmsg="";
int mouseX=0, mouseY=0;
public AWT3() {
addWindowListener(new MyWindowwAdapter(this));
addMouseListener(new MyMouseeAdapter(this));
}
public void paint(Graphics g){
g.drawString(Mmsg, mouseX, mouseY);
}
public static void main(String args[]){
AWT3 awt3 = new AWT3();
awt3.setSize(new dimension(500, 500));
awt3.setTitle("Window framee");
awt3.setVisible(true);
}
}
class MyWindowwAdapter extends WindowAdapter{
AWT3 awt3;
public MyWindowwAdapter(AWT3 awt3) {
this.awt3=awt3;
}
public void windowClosing(WindowEvent we){
awt3.setVisible(false);
}
}
class MyMouseeAdapter extends MouseAdapter{
AWT3 awt3;
public MyMouseeAdapter(AWT3 awt3) {
this.awt3=awt3;
}
public void MouseClicked(MouseEvent me){
awt3.Mmsg="the mouse is clicked";
awt3.mouseX= me.getX();
awt3.mouseY=me.getY();``
awt3.repaint();
}
}
From what it looks like, this code won't compile. You have an error that you need to fix:
awt3.setSize(new dimension(500, 500));
to
awt3.setSize(new Dimension(500, 500));
and add the proper import java.awt.Dimension as pointed out by others.
Another mistake is that MouseClicked(MouseEvent me) is not overriding the super class method from MouseAdapter as its syntactically wrong (super class method starts with small case). Change it to mouseClicked(MouseEvent me) (add the optional #Override annotation if you wish).
The method name should be public void mouseClicked(MouseEvent me)
instead of public void MouseClicked(MouseEvent me).
mouseClicked() is when the mouse button has been pressed and released.
mousePressed() is when the mouse button has been pressed.
Your code is working. tested on java 1.7. only the problem I saw, was with out importing the java.awt.Dimension class you are trying to create a new dimension(500, 500); although the class name is in simple form you can fix this error and try the code.
Related
Working with JMapViewer, a strange behavior of the component was recognized. I am using DefaultMapController to get the map position (lat, lon).
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import org.openstreetmap.gui.jmapviewer.DefaultMapController;
import org.openstreetmap.gui.jmapviewer.JMapViewer;
public class Test extends JMapViewer{
public Test()
{
addMouseListener(new DefaultMapController(this) {
public void mouseClicked(MouseEvent e){
Point start = e.getPoint();
System.out.println(e.getPoint());
}
});
}
protected void paintComponent(Graphics g){super.paintComponent(g);}
public static void main (String [] args){
JFrame jf = new JFrame();
jf.setSize(800, 600);
Test t= new Test();
jf.add(t);
jf.setVisible(true);
}
}
Running the code, after the left mouse button is pressed, the method mouseClicked() gets called multiple times (2x). After the replacement
addMouseListener(new DefaultMapController(this) {
with
addMouseListener(new MouseAdapter() {
the code works correctly, the method gets called only 1x. Where is the problem? Is it a bug inside the library or the syntax is wrong or unsafe? How to avoid this issue? Thanks for your help.
Your Test extends JMapViewer, adding a MouseListener in an instance initializer block. As a consequence, the "default constructor will call the no-argument constructor of the superclass." The superclass, JMapController, adds your MouseListener—you guessed it—a second time.
public JMapController(JMapViewer map) {
this.map = map;
if (this instanceof MouseListener)
map.addMouseListener((MouseListener) this);
…
}
Instead, create a new JMapController or DefaultMapController, as shown here, and use it to construct your JMapViewer.
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import org.openstreetmap.gui.jmapviewer.DefaultMapController;
import org.openstreetmap.gui.jmapviewer.JMapViewer;
/**
* #see https://stackoverflow.com/a/39461854/230513
*/
public class TestMapController {
private void display() {
JFrame f = new JFrame("TestMapController");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMapViewer map = new JMapViewer();
new DefaultMapController(map) {
#Override
public void mouseClicked(MouseEvent e) {
System.out.println(e.getPoint());
}
};
f.add(map);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new TestMapController()::display);
}
}
I am following the examples from Java : The complete reference 8th edition (JDK 7) on AWT and I cannot succeed to display a string on the window that appears. The size and title are set correctly and the window shows up. If I output a string on the console in the paint() method I see that it actually gets called a few times but the string does not appear on the window of my application. I can't see where I diverged from the example; I actually have a bit less code (they added a mouse listener and a key listener) :\
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Main {
public static void main(String[] args) {
Application app = new Application();
app.setSize(new Dimension(640, 480));
app.setTitle("This is a test");
app.setVisible(true);
}
}
class MyWindowAdapter extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
class Application extends Frame {
public Application() {
addWindowListener(new MyWindowAdapter());
}
public void paint(Graphics g) {
System.out.println("Hey hey !");
g.drawString("Test", 10, 10);
}
}
The problem you're having is the fact that you are painting directly on top of the frame. The frame also includes the frame border, so position 0, 0 (or in your case 10, 10) is actually hidden UNDER the frame border.
You can see more about that here.
Instead, you should draw onto a Canvas and add that to the frame
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class BadFrame {
public static void main(String[] args) {
new BadFrame();
}
public BadFrame() {
Application app = new Application();
app.setSize(new Dimension(640, 480));
app.setTitle("This is a test");
app.setLayout(new BorderLayout());
app.add(new MyCanvas());
app.setVisible(true);
}
class MyWindowAdapter extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
public class MyCanvas extends Component {
#Override
public void paint(Graphics g) {
super.paint(g);
System.out.println("Hey hey !");
g.drawString("Test", 10, 10);
}
}
class Application extends Frame {
public Application() {
addWindowListener(new MyWindowAdapter());
}
}
}
The next question that comes to mind is, why AWT? The API has being moth balled in favor of Swing. If nothing else, it's automatically double buffered ;)
ps- You may also find 2D Graphics of some interest, especially the discussion on text
Your string gets drawn but is hidden under the title bar of the window. Just use e.g.
g.drawString("Test", 10, 200);
and you'll see it appear
Ok, I am trying to use MouseListener for the first time, but I'm not having much luck. My program compiles fine but the MouseListener Events don't seem to do anything. Here is my code:
import java.awt.color.*;
import java.awt.font.*;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class yo implements MouseListener {
Image image;
JFrame frame = new JFrame();
JLabel heloo = new JLabel("yo");
JPanel panel = new JPanel()
{
#Override
public void paintComponent(Graphics g)
{
//super.paintComponent(g);
//ImageIcon i = new ImageIcon("hi.jpg");
//image = i.getImage();
//g.drawImage(image,150,150,null);
//g.drawString("Hello",100,100);
//g.drawString("Hi",50,50);
}
};
public yo()
{
frame.add(panel);
frame.setTitle("Hello");
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
panel.add(heloo);
}
public void mouseClicked (MouseEvent Event)
{
heloo.setText("Hi");
System.out.println("Hi");
}
public void mouseEntered (MouseEvent Event)
{System.out.println("Hi");}
public void mouseExited (MouseEvent Event)
{}
public void mousePressed (MouseEvent Event)
{}
public void mouseReleased (MouseEvent Event)
{}
public static void main(String[] args)
{
new yo();
}
}
By not doing anything I mean that the system doesn't output text to command line or change the JLabel.
Any help on how to get it to work would be great, thanks.
p.s. I'm a noob so, be nice.
Read the Swing tutorial on How to Write a MouseListener.
You didn't add the listener to any component.
put
frame.addMouseListener(this);
in the constructor
You made a yo a MouseListener, but you didn't add it to anything.
You need to use .addMouseListener(this); on each component you want to listen to.
e.g.
frame.addMouseListener(this), or if in a static method frame.addMouseListener(myInstanceOfYo);
try
public yo()
{
frame.add(panel);
frame.setTitle("Hello");
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
panel.add(heloo);
frame.addMouseListener(this);
}
Edit:
I would also suggest you change your test text in each MouseListener method to be unique, so it's easier to see which was called, and when. Also, make the parameter name start with a lower case letter (Event becomes event), it's just good practice.
i.e.
public void mouseClicked (MouseEvent event)
{
heloo.setText("Hi");
System.out.println("Clicked.");
}
public void mouseEntered (MouseEvent event)
{
System.out.println("Entered.");
}
public void mouseExited (MouseEvent event)
{
System.out.println("Exited.");
}
public void mousePressed (MouseEvent event)
{
System.out.println("Pressed.");
}
public void mouseReleased (MouseEvent event)
{
System.out.println("Released.");
}
I have a JComponent, and I want it do preform a piece of code when the component is pressed. Can someone help me?
JComponent comp = new JPanel();
comp.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
// Place your code here
}
});
Assuming you have a Class that extends JComponent
import java.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyClass extends JComponent{
public MyClass(){
//Other code
addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
// Place your code here
}
});
//Other code
}
}
I'm writing a game in java. The problem here is I wrote my game to run in a JFrame, not thinking that i would want to add menus and a results screen and all that good stuff. The game itself runs great in the JFrame. What i decided to do, though, was turn my JFrame into a JPanel, create a separate class for my JFrame and then just add my JPanel to the frame. Everything works just peachy except my MouseListener no longer does a darn thing. Can someone tell me how to make this work or a different idea of how this can be done?
/////UPDATE
So apparently i found an answer while recreating the problem.... I just need the figure out the difference between my game code and the test code.
Here is the example i wrote up to try and reproduce the problem.. Oddly enough this works. Now I'm even more confused. So apparently this is ok:
//Class for the JFrame
package mousetest;
import java.awt.Color;
import javax.swing.JFrame;
public class MouseTest extends JFrame{
public static void main(String[] args) {
MouseTest test = new MouseTest();
}
public MouseTest(){
//create teh board
Board game = new Board();
//framestuff
setSize(406, 630);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
setBackground(Color.black);
add(game); // add it
}
}
========================================================================
//Class for the JPanel that my game is in
package mousetest;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Board extends JPanel{
JLabel testlabel = new JLabel("testtext");
//CONSTRUCTOR
public Board(){
setBackground(Color.WHITE);
testlabel.addMouseListener(new Mousehandle());
setVisible(true);
add(testlabel);
}
// control ALLTHECLICKS!!!!!
class Mousehandle implements MouseListener{
public Mousehandle(){
}
public void mouseClicked(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
if(e.getSource() == testlabel){
System.out.println("mouse down");
}
}
public void mouseReleased(MouseEvent e) {
if(e.getSource() == testlabel){
System.out.println("mouse up");
}
}
public void mouseEntered(MouseEvent e) {
if(e.getSource() == testlabel){
System.out.println("rollover");
}
}
public void mouseExited(MouseEvent e) {
if(e.getSource() == testlabel){
System.out.println("roll off");
}
}
public void mouseDragged(MouseEvent e){
}
}
}