Java MouseListener 🖱️

preview_player
Показать описание
Java MouseListener mouselistener GUI swing tutorial for beginners

#java #MouseListener #mouselistener #GUI #swing #tutorial #beginners
Рекомендации по теме
Комментарии
Автор

public class Main{

public static void main(String[] args) {

new MyFrame();
}
}
import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;

public class MyFrame extends JFrame implements MouseListener{

JLabel label;

MyFrame(){

this.setSize(500, 500);
this.setLayout(null);

label = new JLabel();
label.setBounds(0, 0, 100, 100);

label.setOpaque(true);


this.add(label);
this.setVisible(true);
}

@Override
public void mouseClicked(MouseEvent e) {
// Invoked when the mouse button has been clicked (pressed and released) on a component
//System.out.println("You clicked the mouse");
}

@Override
public void mousePressed(MouseEvent e) {
// Invoked when a mouse button has been pressed on a component
System.out.println("You pressed the mouse");

}

@Override
public void mouseReleased(MouseEvent e) {
// Invoked when a mouse button has been released on a component
System.out.println("You released the mouse");

}

@Override
public void mouseEntered(MouseEvent e) {
// Invoked when the mouse enters a component
System.out.println("You entered the component");

}

@Override
public void mouseExited(MouseEvent e) {
// Invoked when the mouse exits a component
System.out.println("You exited the component");

}

}

BroCodez
Автор

With each video I watch understanding terms on the next one gets easier. I'll one day rewatch this whole playlist to master all these concepts. Thank you bro <3

bitcoin
Автор

This was fantastic! I love how you incorporated those emoticons with funny explanations! :D

DanielRodrigues-bxlr
Автор

This might be the first Java program that I actually loved since learning about the countdown one.

mahernurhussen
Автор

This is the best Java tutorial for beginners, so you can learn Java and English in one hit. Please keep going! I vote for Java advance tutorial. Thanks a lot Bro

pavelkvasnicka
Автор

Bro, keep the juice flowing 👏. Thanks

monwil
Автор

Really appreciate ya making all these videos! Made a pivot to code after 2 strokes this year and the way you explained the concepts really helps me digest and install the new skills . Are you still open to making more, or is this more or less a completed series? Just curious.

parkerpierce
Автор

gosh im late, great content, love the effort you put in making the videos

deepdaddy
Автор

Gracias que buen proyecto, entendí de la mejor manera, saludos desde colombia espero seguir aprendiendo con tu canal

ShadwAnlirome
Автор

Dude your channel single handedly saved my computer science grade

zoetenuta
Автор

When I saw your cover page of this lesson, I thought you are going to talk about how to make the eyes of the emoji following the cursor .

But I was wrong, therefore I decide to make this function comes true as a challenge to myself .

After a few days of struggling and reviewing the math knowledges, I can't believe that I achieve the goal . Now the eyeball spins to follow my cursor . I can even move the whole face around the frame .

Here's my code . There are 3 parts as the comment gets word count limit .

// Part 1

package testing;

public class Main {
public static void main(String[] args) {
MyFrame frame = new MyFrame() ;
}
}

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MyFrame extends JFrame implements MouseListener, MouseMotionListener {

// For finding the position of two black balls .
MyFrame blackBall ;

// Instantialization of image icon .
ImageIcon faceIcon = new ;
ImageIcon leftWhiteBallIcon = new ;
ImageIcon leftBlackBallIcon = new ;
ImageIcon rightWhiteBallIcon = new ;
ImageIcon rightBlackBallIcon = new ;

// Instantialization of label .
JLabel faceLabel = new JLabel() ;
JLabel leftWhiteBallLabel = new JLabel() ;
JLabel leftBlackBallLabel = new JLabel() ;
JLabel rightWhiteBallLabel = new JLabel() ;
JLabel rightBlackBallLabel = new JLabel() ;

// distance between the cursor and the start point of the face .
double cursorFaceDistanceX, cursorFaceDistanceY ;
// distance between the cursor and the eyeball .
double cursorLeftBlackBallDistanceX, cursorLeftBlackBallDistanceY ;
double cursorRightBlackBallDistanceX, cursorRightBlackBallDistanceY ;
// distance between the start point of face and the orbit center .
final double faceLeftOrbitDistanceX = 150, faceLeftOrbitDistanceY = 180;
final double faceRightOrbitDistanceX = 350, faceRightOrbitDistanceY = 180 ;

// component parameters (frame, face, 2 white balls, 2 black balls, 2 orbits)
final double frameWidth = 1000, frameHeight = 1000, titleBarHeight = 30, screenFrameGap = 7 ;
final double faceWidth = 500, faceHeight = 500 ;
final double leftWhiteBallRadius = 65, rightWhiteBallRadius = 65 ;
final double leftBlackBallRadiusGain = 30, rightBlackBallRadiusGain = 30 ;
final double leftBlackBallRadius = 15 + leftBlackBallRadiusGain, rightBlackBallRadius = 15 + rightBlackBallRadiusGain ;
final double leftOrbitRadius = 50 - leftBlackBallRadiusGain ;
final double rightOrbitRadius = 50 - rightBlackBallRadiusGain ;

// coordination of components under Java's coordinate system (frame, cursor, face, 2 black balls )
double frameAbsoluteX, frameAbsoluteY ;
double cursorRelativeX, cursorRelativeY ;
double faceRelativeX, faceRelativeY ;
double faceCenterX = faceRelativeX + 250, faceCenterY = faceRelativeY + 250 ;
double leftOrbitCenterX = faceLeftOrbitDistanceX, leftOrbitCenterY = faceLeftOrbitDistanceY ;
double rightOrbitCenterX = faceRightOrbitDistanceX, rightOrbitCenterY = faceRightOrbitDistanceY ;
double leftBlackBallCenterX, leftBlackBallCenterY, rightBlackBallCenterX, rightBlackBallCenterY ;

// coordination of component under Cartesian coordinate system (cursor, 2 orbit centers, 2 black balls)
double cursorRelativeCartesianX, cursorRelativeCartesianY ;
double leftOrbitCenterCartesianX = leftOrbitCenterY, leftOrbitCenterCartesianY = leftOrbitCenterX ;
double rightOrbitCenterCartesianX = rightOrbitCenterY, rightOrbitCenterCartesianY = rightOrbitCenterX ;
double leftBlackBallCenterCartesianX, leftBlackBallCenterCartesianY, , ;

// two slopes
double leftCartesianM, rightCartesianM ;

boolean isCursorInLeftObject = false, isCursorInRightObject = false ;
boolean dragFaceState = false ;

public MyFrame()
{
// basic location setting of face, 2 black balls and 2 white balls .
faceLabel.setBounds(0, 0, (int)faceWidth, (int)faceHeight) ;


- leftWhiteBallRadius), (int)(leftOrbitCenterY - leftWhiteBallRadius), (int)(leftWhiteBallRadius*2), (int)(leftWhiteBallRadius*2)) ;


- leftBlackBallRadius), (int)(leftOrbitCenterY - leftBlackBallRadius), (int)(leftBlackBallRadius*2), (int)(leftBlackBallRadius*2)) ;


- rightWhiteBallRadius), (int)(rightOrbitCenterY - rightWhiteBallRadius), (int)(rightWhiteBallRadius*2), ;


- rightBlackBallRadius), (int)(rightOrbitCenterY - rightBlackBallRadius), (int)(rightBlackBallRadius*2), ;


// basic setting of frame .
this.setVisible(true) ;
;
// the start point of setSize is (-15, -8)
, ;
this.setLayout(null) ;
;
;
;
;
this.add(faceLabel) ;
this.addMouseListener(this) ;
;
}

// declare the location of 2 black ball centers returned from "getBlackBallLocation" method .
public MyFrame(double leftBlackBallCenterX, double leftBlackBallCenterY, double rightBlackBallCenterX, double rightBlackBallCenterY)
{
this.leftBlackBallCenterX = leftBlackBallCenterX ;
this.leftBlackBallCenterY = leftBlackBallCenterY ;
this.rightBlackBallCenterX = rightBlackBallCenterX ;
this.rightBlackBallCenterY = rightBlackBallCenterY ;
}

// retrieve the location of 2 black ball centers returned from "getBlackBallLocation" method .
public double getLeftCenterX()
{
return leftBlackBallCenterX ;
}

public double getLeftCenterY()
{
return leftBlackBallCenterY ;
}

public double getRightCenterX()
{
return rightBlackBallCenterX ;
}

public double getRightCenterY()
{
return rightBlackBallCenterY ;
}

// Invoked when a mouse button is pressed on a component(frame) and then dragged.
@Override
public void mouseDragged(MouseEvent e) {
// get the coordination of frame, cursor and face .
getFrameAbsolutePosition() ;
getCursorRelativePosition() ;
getFaceRelativePosition() ;

// if the face area is pressed and then being dragged .
if (dragFaceState)
{
// update the coordination of face .
- cursorFaceDistanceX), (int)(cursorRelativeY - cursorFaceDistanceY)) ;

// update the coordination of the two orbit centers .
leftOrbitCenterX = faceRelativeX + faceLeftOrbitDistanceX ;
leftOrbitCenterY = faceRelativeY + faceLeftOrbitDistanceY ;
rightOrbitCenterX = faceRelativeX + faceRightOrbitDistanceX ;
rightOrbitCenterY = faceRelativeY + faceRightOrbitDistanceY ;
leftOrbitCenterCartesianX = leftOrbitCenterY ;
leftOrbitCenterCartesianY = leftOrbitCenterX ;
rightOrbitCenterCartesianX = rightOrbitCenterY ;
rightOrbitCenterCartesianY = rightOrbitCenterX ;

// update the location of two white balls and two black balls .
- leftWhiteBallRadius), (int)(leftOrbitCenterY - leftWhiteBallRadius)) ;
- rightWhiteBallRadius), (int)(rightOrbitCenterY - rightWhiteBallRadius)) ;
- cursorLeftBlackBallDistanceX), (int)(cursorRelativeY - ;
-, (int)(cursorRelativeY - ;
}
}
// too long, the comment returned error .
// be continue ...

maxwong
Автор

Hope can make a lesson about the MouseMotionListener .

maxwong
Автор

I will like every video of yours I watch

_Anna_Nass_
Автор

Amazing! Much respect! TYVM bro! A perfect 10 tutorial!

shaynazoedeguzman
Автор

Awesome just what I've been looking for. Keep up the great content bro

keelgreene
Автор

I thoroughly enjoyed this video. It was informative very easy to get through. It made me laugh so I felt comfortable which made the subject matter more absorbable and effective. Thank you ..Great work.

NiceChange
Автор

bro code thank you very much now because of you I can code my own java gui application but please am having a little problem with api, am trying to create a music downloader software using api from different website but I can't figure it out so please can you make video about how to use api in creating java swing application please !!! hope to hear from you, I have learnt alot from you and hope to keep on learn

kobby
Автор

Awesome and great and all the good stuff! 👍

kemann
Автор

ho bro! you so good! im realy anjoy in your videos man!!❤❤

NatyTor
Автор

Hello Bro! I like so much your videos. I want ask something about Mouse Event. How i can use the events on objects. Like rectangle or ellipse. It's possible implementen inside the object? You can do some video using events on graphics object? Thanks for all.

elaprendiz