JFrame Display - Mouse Listeners - java

I am currently developing a game in java.When I start my game I get to chose between Full Screen and Window Mode, by clicking somewhere in the frame.The Window Mode works perfectly, but I have some issues at the full screen mode.So if I have in my main() just a simple initialization of the game, like new FullscreenMode(), the game works perfect, the listeners are working.In the code bellow if I launch the full screen Mode frame from the constructor it works perfect, but if I launch it from a function that belongs to my class, all the listeners won't work...And when I say it's not working properly, I mean the listeners are not responding, everywhere I click I get no response from the game, but if the full screen Mode Frame is launched from the constructor everything works perfectly.Thank you in advance!
public class ResolutionChoser extends JFrame implements MouseMotionListener,
MouseListener {
private static final long serialVersionUID = 1L;
private BufferedImageLoader loader;
private BufferedImage rezImg = null;
public ResolutionChoser() {
super("Welcome!");
requestFocus();
loader = new BufferedImageLoader();
rezImg = loader.loadImage("/RezImg.png");
this.setPreferredSize(new Dimension(Game.WIDTH * Game.SCALE + 2,
Game.HEIGHT * Game.SCALE + 2));
this.setMaximumSize(new Dimension(Game.WIDTH * Game.SCALE + 2,
Game.HEIGHT * Game.SCALE + 2));
this.setMinimumSize(new Dimension(Game.WIDTH * Game.SCALE + 2,
Game.HEIGHT * Game.SCALE + 2));
this.pack();
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
addMouseMotionListener(this);
addMouseListener(this);
JLabel jl = new JLabel() {
private static final long serialVersionUID = 1L;
#Override
public void paintComponent(Graphics g) {
g.drawImage(rezImg, 0, 0, null);
}
};
setContentPane(jl);
//Works Perfectly if I do it like so
// launchFullScreen();
}
//Method that launches my fullscreen game
public void launchFullScreen() {
removeMouseMotionListener(this);
removeMouseListener(this);
MultiBufferTest.main(null);
}
public static void main(String[] args) {
ResolutionChoser rc = new ResolutionChoser();
}
#Override
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseClicked(MouseEvent arg0) {
System.out.println("CLICKED AT: " + arg0.getX() + "--" + arg0.getY());
launchFullScreen();
//not working properly, the listeners for my game aren't responding
}
#Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}

Well, you are removing the listeners in your launchFullScreen() method:
public void launchFullScreen() {
removeMouseMotionListener(this);
removeMouseListener(this);
MultiBufferTest.main(null);
}
How do you expect the listeners to respond?

Don't attach Mouseisteners directly to a frame, there is a JRootPane and content pane (and possibly a glass pane) between the frame the user, any one of theses could be stealing mouse events, which will prevent your frame from receiving mouse events.
Instead, attach the MouseListener to the top level component directly
Also...
JLabel jl = new JLabel() {
private static final long serialVersionUID = 1L;
#Override
public void paintComponent(Graphics g) {
g.drawImage(rezImg, 0, 0, null);
}
};
this is not only a bad idea (you've broken the paint chain), it's also not required, as JLabel is capable of displaying images.
See Painting in AWT and Swing,
Performing Custom Painting and
How to Use Labels for more details

Related

MouseListener not triggering for custom JLabel

I want to make a Swing program with icon tiles that play certain sounds when you click on them. For this I created the class:
public class Item extends JLabel implements MouseListener {
public Item(String s) {
// constructor setting background icon and private field with the sound to play
}
public void playSound(); //plays the sound
#Override
public void mouseClicked(MouseEvent e) {
System.out.println("Clicked");
this.playSound();
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("Pressed");
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
Then I create a custom JFrame class:
public class Frame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 9221468315661092752L;
public static final int DEFAULT_FRAME_WIDTH=400;
public static final int DEFAULT_FRAME_HEIGHT=400;
private GridLayout gridLayout;
private ArrayList<Item> tiles=new ArrayList<Item>();
public Frame() {
super("Title");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(new Dimension(DEFAULT_FRAME_WIDTH,DEFAULT_FRAME_HEIGHT));
gridLayout=new GridLayout(4,4,10,10);
setLayout(gridLayout);
addItems();
}
public void addItems() {
Item item=new Item("name");
Item item2=new Item("name");
tiles.add(item);
tiles.add(item2);
this.add(item);
this.add(item2);
}
public static void main(String[] args) {
Frame frame=new Frame();
frame.setVisible(true);
}
The tiles show normally in the grid layout, however the problem is that the mouse listener doesn't work. The println() I put at mouseClicked() and mousePressed() is not called.
I could try to add a MouseListener to the Item class, so that Item has a MouseListener, but I wonder what is wrong with the above code.
You have two problems. One, your JLabels do not show (although I'm not sure if that was intentionally left out). Two, you never add a mouse listener. You can accomplish this by adding two statements to the constructor, without making any extra classes:
public Item(String s) {
super(s); //initializes the text and display using JLabel's constructor
addMouseListener(this); //uses the reference of this Item as a MouseListener
}

Java mouseClicked complication

Code works, my bad. But I'm still open to suggestions on how to improve or make the code more elegant.
I have created this layout and I want to be able to draw a circle whenever the user clicks on the white area.
Couldn't post an image, so here is the link
The white area is basically a rectangle. But something with my code isn't working, it just doesn't respond to mouse clicks. When I tried to see if it responds to mouseDragged it worked perfectly but this isn't what I need.
Here is my code, some "tests" are put as /comments/ but neither of them work as intended.
I would be very grateful for help. Here is my code:
import java.awt.*;
import java.awt.Graphics;
import javax.swing.*;
public class CitiesMapPanel extends JPanel implements MouseListener {
private JButton cmdAddWay, cmdFindPath, cmdClearMap, cmdClearPath;
private JLabel lblFrom, lblTo;
private JTextField txtFrom, txtTo;
public CitiesMapPanel() {
cmdAddWay = new JButton("Add Way");
cmdFindPath = new JButton("Find Path");
cmdClearMap = new JButton("Clear Map");
cmdClearPath = new JButton("Clear Path");
lblFrom = new JLabel("From");
lblTo = new JLabel("To");
txtFrom = new JTextField(6);
txtTo = new JTextField(6);
this.addMouseListener(this);
setLayout(new BorderLayout());
add(buildGui(), BorderLayout.SOUTH);
}
private JPanel buildGui() {
JPanel buttonsBar = new JPanel();
//The "south" of the BorderLayout consist of a (2,4) GridLayout.
buttonsBar.setLayout(new GridLayout(2,4));
buttonsBar.add(lblFrom);
buttonsBar. add(txtFrom);
buttonsBar.add(lblTo);
buttonsBar.add(txtTo);
buttonsBar.add(cmdAddWay);
buttonsBar.add(cmdFindPath);
buttonsBar.add(cmdClearMap);
buttonsBar.add(cmdClearPath);
return buttonsBar;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.white);
g.fillRect(0, 0, this.getSize().height, this.getSize().width);
}
public static void main(String[] args) {
JFrame frame = new JFrame("layout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(530, 550);
CitiesMapPanel gui = new CitiesMapPanel();
frame.add(gui);
frame.setVisible(true);
}
/*abstract private class MyMouseListner implements MouseListener{
public void mouseClicked(MouseEvent e){
int x = e.getX();
int y = e.getY();
Graphics g = getGraphics();
g.setColor(Color.black);
g.fillOval(x,y,15,15);
}
}*/
#Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
Graphics g = getGraphics();
g.setColor(Color.black);
g.fillOval(x,y,15,15);
System.out.println("test");
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
The click listener is not the problem. Your approach to painting is simply wrong. You can't do a getGraphic, paint on it, and expect the result to be presented. In Swing (AWT) things work fundamentally different. You need to either create an off screen image that you paint to and that you then present on screen in your paintComponent method, or you need to track the objects you want to paint in a data structure and paint those in your paintComponent method. You can trigger a repaint in your click listener by calling repaint so the UI subsystems knows about the changed state that requires a repaint of your component.
Read more about the basics in the Swing painting tutorial.

Java - How to make the images disappear

Hi I want to make my JPanel disappear so I wrote these lines of code
removeAll();
updateUI();
revalidate();
That only made the JComponents and JButtons disappear. I would like to make the images that I have displayed with the paint method disappear also. If I do setVisible(false), then I cannot add another JPanel behind it.
This is my class:
package screens;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class menuScreen extends JPanel implements MouseListener{
private static final long serialVersionUID = 1L;
//-------------VARIABLES---------------//
Image wallpaper = (Image)Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/wallpaper.jpg"));
Image title_text = (Image)Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/title-text.png"));
ImageIcon startGameimg = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/startGame.png")));
ImageIcon optionsimg = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("images/options.png")));
//JButton start = new JButton(basketball);
JLabel options = new JLabel(optionsimg);
JLabel startGame = new JLabel(startGameimg);
gameScreen gS = new gameScreen();
CardLayout scenechange = new CardLayout();
JPanel scenechange1 = new JPanel (scenechange);
//-------------PAINT FUNCTION----------//
public void paintComponent(Graphics g){
g.drawImage(wallpaper,0,0,this);
g.drawImage(title_text,0,0,this);
//g.drawImage(basketball1,110,180,this);
}
//-------------CONSTRUCTOR-------------//
public menuScreen(){
scenechange.addLayoutComponent(this,"menuScreen");
scenechange.addLayoutComponent(gS,"gameScreen");
//scenechange.show(this,"menuScreen");
this.setLayout(null);
this.add(options);
this.add(startGame);
startGame.setBounds(110,180,110,110);
options.setBounds(110,300,110,110);
startGame.addMouseListener(this);
options.addMouseListener(this);
}
public void mouseClicked(MouseEvent e) {
if(e.getSource() == (startGame)){
removeAll();
revalidate();
add(gS);
}
if(e.getSource() == (options)){
setVisible(false);
}
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}//END OF CLASS startingScreen
Thanks in advance.
First, don't call updateUI, it's related to the Look and Feel and not (directly) to updating your components.
If you have provided a custom paint routine within in your panel, then you need away to stop it from painting the images (without preventing it from painting it's own content). removeXxx will remove child components that you have previously added to the container.
A little more code would be useful
UPDATE
Fisrt, the images your painting aren't components of you container, they are been "stamped", you need some way to tell the component not to the paint the images
public void paintComponent(Graphics g){
super.paintComponent(g); // this is super important
if (paintImages){ // you need to define and set this flag
g.drawImage(wallpaper,0,0,this);
g.drawImage(title_text,0,0,this);
}
}
Now, this will stop the images from been painted.
If, however, you no longer want to use the component (ie, you want to remove it from the screen so you can place a new component on the screen in its place), you need to remove this component from it's parent, which Code-Guru has suggested (so I won't steal his answer ;))
UPDATE
Okay, you had a kernel of an idea but either didn't quite know how to implement it or decided to discard it.
Basically, from the looks of your code, you were either trying to, or had, implement a CardLayout, unfortunately, you kind of got the wrong idea with it.
With CardLayout, you need to "controller", a component that is responsible for switching the screens...
public class ScreenController extends JPanel {
private static final long serialVersionUID = 1L;
//-------------VARIABLES---------------//
MenuScreen ms = new MenuScreen();
GameScreen gs = new GameScreen();
CardLayout sceneChange;
//-------------CONSTRUCTOR-------------//
public ScreenController() {
sceneChange = new CardLayout();
this.setLayout(sceneChange);
add(ms, "menuScreen");
add(gs, "gameScreen");
sceneChange.show(this, "menuScreen");
ms.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("startgame")) {
sceneChange.show(ScreenController.this, "gameScreen");
}
}
});
}
}//END OF CLASS startingScreen
Then you have your menu and game screens...
public class MenuScreen extends JPanel implements MouseListener {
private static final long serialVersionUID = 1L;
//-------------VARIABLES---------------//
//JButton start = new JButton(basketball);
JLabel options = new JLabel("Options");
JLabel startGame = new JLabel(" >> Start << ");
// gameScreen gS = new gameScreen();
BufferedImage wallpaper;
//-------------PAINT FUNCTION----------//
#Override
public void paintComponent(Graphics g) {
System.out.println("paint");
super.paintComponent(g);
if (wallpaper != null) {
g.drawImage(wallpaper, 0, 0, this);
}
}
//-------------CONSTRUCTOR-------------//
public MenuScreen() {
// Please handle your exceptions better
try {
wallpaper = ImageIO.read(getClass().getResource("/Menu.png"));
setPreferredSize(new Dimension(wallpaper.getWidth(), wallpaper.getHeight()));
} catch (IOException ex) {
ex.printStackTrace();
}
setLayout(new GridBagLayout());
Cursor cusor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
options.setCursor(cusor);
startGame.setCursor(cusor);
Font font = UIManager.getFont("Label.font").deriveFont(Font.BOLD, 48);
options.setFont(font);
startGame.setFont(font);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
this.add(options, gbc);
gbc.gridy++;
this.add(startGame, gbc);
startGame.addMouseListener(this);
options.addMouseListener(this);
}
public void mouseClicked(MouseEvent e) {
if (e.getSource() == (startGame)) {
fireActionPerformed("startGame");
}
if (e.getSource() == (options)) {
fireActionPerformed("gameOptions");
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void addActionListener(ActionListener listener) {
listenerList.add(ActionListener.class, listener);
}
public void removeActionListener(ActionListener listener) {
listenerList.remove(ActionListener.class, listener);
}
protected void fireActionPerformed(String cmd) {
ActionListener[] listeners = listenerList.getListeners(ActionListener.class);
if (listeners != null && listeners.length > 0) {
ActionEvent evt = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, cmd);
for (ActionListener listener : listeners) {
listener.actionPerformed(evt);
}
}
}
}
Menu Screen...
And when you click start...the game screen...
Now this is an EXAMPLE. Please try and take the time to understand what it going on in the code before you march ahead and implement it. I used by own images, you'll need to get your own..
There are several ways to stop your JPanel from "appearing" depending on exactly what you want to accomplish. One was it to to call setOpaque(false);. I'm not entirely sure how this affects custom painting, though.
Another posibility is
Container parent = getParent().remove(this);
parent.validate();
A third posibility is to add a flag in your class which is set when you click on a JLabel (or better yet a JButton -- see comments below). Then in your paintComponent() method you can check the flag and draw accordingly.
Note:
You are incorrectly using a JLabel and mouse events to respond to user input. Typically in a Swing application, we use JButtons and ActionListeners to accomplish what you are trying to do here. One advantage of this is that you only have to implement one method called onActionPerformed() and don't need to worry about adding all the mouse event handlers that you don't want to even respond to.

Moving undecorated window by clicking on JPanel

Is there a possibility to move window by clicking on one of the panels in the window when that window is undecorated?
I have a main panel with matte border 40 pixels size, and few panels with controls inside, and I would like to move the window when clicking on that border. Is that possible?
You can place another panel over the panel with the border, leaving the border visible.Use the following code to move your window.
public class MotionPanel extends JPanel{
private Point initialClick;
private JFrame parent;
public MotionPanel(final JFrame parent){
this.parent = parent;
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
initialClick = e.getPoint();
getComponentAt(initialClick);
}
});
addMouseMotionListener(new MouseMotionAdapter() {
#Override
public void mouseDragged(MouseEvent e) {
// get location of Window
int thisX = parent.getLocation().x;
int thisY = parent.getLocation().y;
// Determine how much the mouse moved since the initial click
int xMoved = e.getX() - initialClick.x;
int yMoved = e.getY() - initialClick.y;
// Move window to this position
int X = thisX + xMoved;
int Y = thisY + yMoved;
parent.setLocation(X, Y);
}
});
}
}
I've been working with this code for a while now to make a custom titlebar for undecorated windows.
P.S.:You can generalize this example by extending JComponent instead of JPanel.
I have a main panel with matte border 40 pixels size, and few panels with controls inside, and I would like to move the window when clicking on that border
I think that ComponetMover by #camickr is right class for you
Yes, it is very possible. You need a MouseListener to listen on mouse events. you start moving on mousedown and stop moving on mouseup. Then you simply translate the window position by the same amount the mouse translates during that phase (calculate the delta bewteen old mouse position and new mouse position and add that to the frames position). You should be able to do this with a mouse listener fairly easily.
I have a simple solution from my project. Here is my undecorated JDialog class.
public class TimerDialog extends JDialog {
// some fields here
private Point mouseClickPoint; // Will reference to the last pressing (not clicking) position
private TimerDialog() {
initComponents();
addEventsForDragging();
}
private void addEventsForDragging() {
// Here is the code does moving
addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
mouseClickPoint = e.getPoint(); // update the position
}
});
addMouseMotionListener(new MouseAdapter() {
#Override
public void mouseDragged(MouseEvent e) {
Point newPoint = event.getLocationOnScreen();
newPoint.translate(-mouseClickPoint.x, -mouseClickPoint.y); // Moves the point by given values from its location
setLocation(newPoint); // set the new location
}
});
}
private void initComponents() {
setLayout(new FlowLayout());
// adding components
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setAlwaysOnTop(true);
setUndecorated(true);
setResizable(false);
pack();
}
}
int xMoved = (thisX + e.getX()) - (thisX + initialClick.x);
thisX + -thisX = 0
int xMoved = e.getX()-initialClick.x;
What i'm using.
public class MouseLiestenerX implements MouseListener,MouseMotionListener{
private theFrame;
public MouseLiestenerX(Frame theFrame){
this.theFrame = theFrame;
}
private Point startClick;
public void mouseDragged(MouseEvent e) {
int deltaX = e.getX()-startClick.x;
int deltaY = e.getY()-startClick.y;
Core.getSp().setLocation(theFrame.getLocation().x+deltaX, theFrame.getLocation().y+deltaY);
}
public void mousePressed(MouseEvent e) {
startClick = e.getPoint();
}
public void mouseMoved(MouseEvent e){
}
#Override
public void mouseClicked(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
}
}
and in your Frame constructor
MouseLiestenerX IMove = new MouseLiestenerX(this);
addMouseListener(IMove);
addMouseMotionListener(IMove);

getting JSlider bar to move on mouse click event

I have a JSlider which shows bet sizes (for a poker game) I am trying to achieve the effect that when a mouse click occurs the slider jumps forward by a bet amount (i.e. a big blind amount) rather than just incrementing by one. If the mouse click happens to the left of the bar i want it to decrement by a fixed amount else increment. I looked into attaching a mouse listener, but do not know how I can use the event to find out on what side of the bar the mouse was clicked.
Any ideas?
You just need to change your perspective on the problem.
Don't view the clicks as being to the 'left' or 'right' (below or above) the current bet.
Rather, you simply store the old tick, and look at what the new tick is. The difference will tell you if the user has tried to increase (positive delta) or decrease (negative delta).
Then you can increment by your desired 'fixed bet' amount.
I think you need to write a custom UI for this. This should get you started:
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;
import javax.swing.plaf.metal.*;
public class SliderScroll extends JFrame
{
public SliderScroll()
{
final JSlider slider = new JSlider(0, 50, 20);
slider.setMajorTickSpacing(10);
slider.setMinorTickSpacing(5);
slider.setExtent(5);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
getContentPane().add( slider );
slider.setUI( new MySliderUI() );
}
class MySliderUI extends MetalSliderUI
{
public void scrollByUnit(int direction)
{
synchronized(slider)
{
int oldValue = slider.getValue();
int delta = (direction > 0) ? 10 : -5;
slider.setValue(oldValue + delta);
}
}
}
public static void main(String[] args)
{
JFrame frame = new SliderScroll();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible( true );
}
}
This is how i accomplish the MouseClickEvent
MoveSlider = new JSlider(JSlider.HORIZONTAL, 0, 0, 0);
MoveSlider.addMouseListener(new MouseListener()
{
public void mousePressed(MouseEvent event) {
//Mouse Pressed Functionality add here
}
#Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
});

Categories

Resources