I have a small program, that creates a something, that could be considered a joystick. Everything works, but there is a glitch, I can't work out how to fix. If mouse moves too fast, the JLabel that I'm dragging around, sticks, as mouse has moved outside of the drawn box. I could increase the size of Jlabel, but then the "O" is offset too much from mouse. (I'd rather even decrease the size, but with this implementation, maximum mouse speed is too low).
Any ideas how to fix this?
This is the whole code, btw, anyone can go ahead and compile, to see what is the problem exactly, when mouse moves too fast.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainClass implements ActionListener, MouseListener, MouseMotionListener
{
int labelSize = 20;
int screenOffsetX = 58;
int screenOffsetY = 130;
JFrame frame;
JLabel xAxis;
JLabel move;
JLabel drag;
JLabel yAxis;
JLabel xAxisDrag;
JLabel yAxisDrag;
JLabel radio;
JLayeredPane panel;
Robot rob;
public static void main(String[] args)
{
new MainClass();
}
public MainClass()
{
frame = new JFrame("app");
frame.setLayout(null);
frame.setBounds(20, 20, 400, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
move = new JLabel("Movement");
move.setBounds(150, 0, 100, 15);
frame.add(move);
xAxis = new JLabel("X");
xAxis.setBounds(100, 20, 50, 15);
frame.add(xAxis);
yAxis = new JLabel("Y");
yAxis.setBounds(200, 20, 50, 15);
frame.add(yAxis);
drag = new JLabel("Dragging");
drag.setBounds(150, 40, 100, 15);
frame.add(drag);
xAxisDrag = new JLabel("X");
xAxisDrag.setBounds(100, 60, 50, 15);
frame.add(xAxisDrag);
yAxisDrag = new JLabel("Y");
yAxisDrag.setBounds(200, 60, 50, 15);
frame.add(yAxisDrag);
radio = new JLabel("O");
radio.setBounds(0, 0, labelSize, labelSize);
radio.setOpaque(false);
radio.setEnabled(false);
panel = new JLayeredPane();
panel.setBounds(50, 100, 257, 257);
panel.setLayout(null);
panel.setBackground(new Color((float)1.0,(float)1.0,(float)1.0));
panel.setOpaque(true);
panel.add(radio);
radio.setLocation(128, 128);
frame.add(panel);
panel.addMouseMotionListener(this);
panel.addMouseListener(this);
frame.revalidate();
frame.repaint();
}
#Override
public void actionPerformed(ActionEvent e) {}
#Override
public void mouseDragged(MouseEvent e)
{
if(!(e.getPoint().x<0 || e.getPoint().y<0 || e.getPoint().x>257 || e.getPoint().y>257))
{
xAxis.setText("X: "+((e.getPoint().x/4)-32));
yAxis.setText("Y: "+((e.getPoint().y/4)-32));
}
if(e.getPoint().x<0 || e.getPoint().y<0 || e.getPoint().x>257 || e.getPoint().y>257)
{ //Neļauj iziet ārpus paneļa;
try
{
rob = new Robot();
if(e.getPoint().x<0)
rob.mouseMove((screenOffsetX+frame.getX()), (e.getPoint().y+screenOffsetY+frame.getY()));
if(e.getPoint().y<0)
rob.mouseMove((e.getPoint().x+screenOffsetX+frame.getX()), (screenOffsetY+frame.getY()));
if(e.getPoint().x>257)
rob.mouseMove((257+screenOffsetX+frame.getX()), (e.getPoint().y+screenOffsetY+frame.getY()));
if(e.getPoint().y>257)
rob.mouseMove((e.getPoint().x+screenOffsetX+frame.getX()), (257+screenOffsetY+frame.getY()));
}
catch (AWTException e1)
{
e1.printStackTrace();
}
}
if( (e.getPoint().x>=radio.getX() && e.getPoint().x<=radio.getX()+labelSize) &&
(e.getPoint().y>=radio.getY() && e.getPoint().y<=radio.getY()+labelSize)
)
{ //Ja kursors ir uz JLabel, tad pārvieto;
xAxisDrag.setText("X: "+((e.getPoint().x/4)-32));
yAxisDrag.setText("Y: "+((e.getPoint().y/4)-32));
radio.setBounds(e.getPoint().x-(labelSize/2), e.getPoint().y-(labelSize/2), labelSize, labelSize);
}
}
#Override
public void mouseMoved(MouseEvent e)
{
xAxis.setText("X: "+((e.getPoint().x/4)-32));
yAxis.setText("Y: "+((e.getPoint().y/4)-32));
}
#Override
public void mouseClicked(MouseEvent arg0) {}
#Override
public void mouseEntered(MouseEvent e) {}
#Override
public void mouseExited(MouseEvent e) {}
#Override
public void mousePressed(MouseEvent e) {}
#Override
public void mouseReleased(MouseEvent e)
{
radio.setLocation(124, 124);
xAxisDrag.setText("X: "+0);
yAxisDrag.setText("Y: "+0);
}
}
Replace
if( (e.getPoint().x>=radio.getX() && e.getPoint().x<=radio.getX()+labelSize) &&
(e.getPoint().y>=radio.getY() && e.getPoint().y<=radio.getY()+labelSize)
)
{ //Ja kursors ir uz JLabel, tad pārvieto;
xAxisDrag.setText("X: "+((e.getPoint().x/4)-32));
yAxisDrag.setText("Y: "+((e.getPoint().y/4)-32));
radio.setBounds(e.getPoint().x-(labelSize/2), e.getPoint().y-(labelSize/2), labelSize, labelSize);
}
by:
if( (e.getPoint().x>=radio.getX() && e.getPoint().x<=radio.getX()+labelSize) &&
(e.getPoint().y>=radio.getY() && e.getPoint().y<=radio.getY()+labelSize)
)
{
//Has the mouse been clicked inside the radio before grabbing started ?
if(grab==0) //if it is the first iteration of the function mousedragged
grab=2; //proceed to "grab"
}
if(grab==2 && (e.getPoint().x>=0 && e.getPoint().x<panel.getWidth()) &&
(e.getPoint().y>=0 && e.getPoint().y<panel.getHeight())
)
{ //Ja kursors ir uz JLabel, tad pārvieto;
System.out.println(radio.getX()+" "+radio.getY());
xAxisDrag.setText("X: "+((e.getPoint().x/4)-32));
yAxisDrag.setText("Y: "+((e.getPoint().y/4)-32));
radio.setBounds(e.getPoint().x-(labelSize/2), e.getPoint().y-(labelSize/2), labelSize, labelSize);
}
if(grab!=2) // if grabbing is not started
grab=1; // first iteration is over
Update MouseReleased function to:
#Override
public void mouseReleased(MouseEvent e)
{
//this a new line
//grabbing is over
grab=0; // go back to idle state
radio.setLocation(124, 124);
xAxisDrag.setText("X: "+0);
yAxisDrag.setText("Y: "+0);
}
Finally , add a new field called grab to your class MainClass:
private int grab=0; //idle state
The meaning of grab:
1. grab=0 => It is the first time that the function MouseDragged is executed and "grabbing" is not started yet. In fact when you drag the mouse this function is iterated like an infinite loop until you release the mouse. So it is important to know when it has started.
2. grab=1 => Iteration has started but there is no "grabbing"
3. grab=2 => "grabbing" has started. You can notice the condition grab==0 before the affectation. In fact I tried to avoid the case when the mouse is dragged from somewhere else and goes through "radio" and then the radio is "grabbed". That's why I checked that it is the first iteration. In this case we have grab=1 => the radio is not grabbed
Related
Okay, I want to create a Tic Tac Toe(X-O) game with GUI, I have drawn it but I'm somehow unable to function it. I do know how to use .addActionListener, and I can set a certain button to "X" or "O", but how do I make it so the first button click turns it to X, the second to O and so on. .addActionListener wont work properly because I have to use it on a certain button, but I don't know which button will be pressed.
Here's the code I've written.
public class XO {
public static void main(String[] args) {
JFrame myForm=new JFrame("X-O");
myForm.setSize(255, 300);
myForm.setLocation(0, 0);
JButton []buttons=new JButton[10];
int x=40, y=0;
for(int i=0;i<10;i++)
{
if(i%3==0)
{
y+=50; x=40;
}
buttons[i]=new JButton((i+1)+"");
buttons[i].setSize(50, 50);
buttons[i].setLocation(x, y);
myForm.add(buttons[i]);
x+=50;
}
buttons[9].setText("Start Over");
buttons[9].setLocation(80, 205);
buttons[9].setSize(70, 50);
buttons[9].setMargin(new Insets(0, 0, 0, 0));
buttons[0].addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
buttons[0].setText("X");
buttons[0].setForeground(Color.BLUE);
buttons[0].setFont(new Font("Arial", Font.PLAIN, 30));
buttons[0].setMargin(new Insets(0, 0, 0, 0));
buttons[0].setEnabled(false);
}
});
myForm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myForm.setLayout(null);
myForm.setVisible(true);
}
}
As you can see, I will have to use .setActionListener on a specific button, but I dont know which button will be pressed. I hope you get what I mean.
Thanks in advance.
Outside of your main method, you can declare a static counter to determine whose move it is:
private static int turnCount = 0;
Add the action listeners for each of your buttons within your loop, and determine what to set the button text as based on the counter. Be sure to increment your turnCount variable within the action listener:
for(int i=0;i<10;i++)
{
if(i%3==0)
{
y+=50; x=40;
}
buttons[i]=new JButton((i+1)+"");
buttons[i].setSize(50, 50);
buttons[i].setLocation(x, y);
int butNum = i;
buttons[i].addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String currentPlayer = turnCount % 2 == 0 ? "X" : "O";
buttons[butNum].setText(currentPlayer);
buttons[butNum].setForeground(Color.BLUE);
buttons[butNum].setFont(new Font("Arial", Font.PLAIN, 30));
buttons[butNum].setMargin(new Insets(0, 0, 0, 0));
buttons[butNum].setEnabled(false);
turnCount++;
}
});
myForm.add(buttons[i]);
x+=50;
}
You can get the button that was pressed from the ActionEvent via the getSource method - just cast it to a JButton.
for (JButton button : buttons) { // one listener per button
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource(); // which button
button.setText(getPlayer()); // get X or O
button.setForeground(Color.BLUE);
button.setFont(new Font("Arial", Font.PLAIN, 30));
button.setMargin(new Insets(0, 0, 0, 0));
button.setEnabled(false);
togglePlayer(); // toggle between X and O
}
});
}
I moved the body of your code to a run method and added methods to get the player and toggle the player.
public static void main(String[] args) {
XO game = new XO();
game.run();
}
private String player = "X";
private String getPlayer() {
return player;
}
private void togglePlayer() {
player = "X".equals(player) ? "O" : "X";
}
public void run() {
JFrame myForm = new JFrame("X-O");
// rest of your code
// plus my actionListener changes
// ...
}
I'm making a small java game for fun to practice with my GUI programming. I want to have the center of my content pane's borderLayout be an image, and I would like to put "invisible" buttons on top of the image in specific places (to be placed later, I just want to get one working for now). My issue is getting the button to actually be invisible, it seems to leave a white square where it is now. I looked around but the only things that seem to be suggested were the .setOpaque, .setContentAreaFilled, and .setBorderPainted. (game is space related, explains the names)
galaxyButton1 = new JButton();
galaxyButton1.setFont(starSystem);
galaxyButton1.setBorder(BorderFactory.createEmptyBorder(25,25,25,25) );
galaxyButton1.setOpaque(false);
galaxyButton1.setContentAreaFilled(false);
galaxyButton1.setBorderPainted(false);
Color invis = new Color(Color.TRANSLUCENT);
galaxyButton1.setForeground(invis);
galaxyButton1.setBackground(invis);
galaxyButton1.addActionListener( new ButtonHandler() );
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BorderLayout());
JPanel buttons = new JPanel();
buttons.setLayout(new GridLayout( 1,0,5,5 ) );
buttons.setOpaque(false);
buttons.add(galaxyButton1);
centerPanel.add(buttons,BorderLayout.CENTER);
centerImg.setLayout(new GridBagLayout());
centerImg.add(centerPanel);
contentPane.add(centerImg, BorderLayout.CENTER);
Here is a code outline of how to make your own button. You will need to set up a the actual mouse in some other class.
public class InvisibleButton implements MouseListener{
private final Rectangle rectangle;
public InvisibleButton(int x, int y, int width, int height){
rectangle = new Rectangle(x,y,width,height);
}
#Override
public void mouseClicked(MouseEvent e) {
int x = 0; // Set this to Mouse X
int y = 0; // Set this to Mouse Y
if(rectangle.contains(x,y)){
//Set Something to True or do action here
}
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
}
I'm trying to use a timer to change the position of a JLabel, from one spot on my JPanel to another. I'm not sure if I could use say .getLocation(), then change only the horizontal x value, and finally use .setLocation() to effectively modify the JLabel. I've also used .getBounds and .setBounds, but am still unsure how I can obtain the old horizontal x value to change and reapply to the new x value.
The code I tried looks something like this, but neither is a valid way to change the position of the JLabel.
// mPos is an arraylist of JLabels to be moved.
for(int m = 0; m < mPos.size(); m++){
mPos.get(m).setLocation(getLocation()-100);
}
or
for(int m = 0; m < mPos.size(); m++){
mPos.get(m).setBounds(mPos.get(m).getBounds()-100);
}
If I could just get the position of the horizontal x value I can change the position of the label.
Try with Swing Timer if you are looking for some animation.
Please have a look at How to Use Swing Timers
Here is the sample code:
int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//...Perform a task...
}
};
new Timer(delay, taskPerformer).start();
Find a Sample code here
Sample code: (Move Hello World message 10px horizontally left to right at interval of 200 ms)
private int x = 10;
...
final JPanel panel = new JPanel() {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("Hello World", x, 10);
}
};
int delay = 200; // milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
x += 10;
if (x > 100) {
x = 10;
}
panel.repaint();
}
};
new Timer(delay, taskPerformer).start();
I made a similar example just so you can get the basic jest of it, try copy pasting this in a new class called "LabelPlay" and it should work fine.
import java.awt.EventQueue;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class LabelPlay {
private JFrame frame;
private JLabel label;
private Random rand;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LabelPlay window = new LabelPlay();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public LabelPlay() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 659, 518);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
label = new JLabel("YEEEHAH!");
label.setBounds(101, 62, 54, 21);
frame.getContentPane().add(label);
JButton btnAction = new JButton("Action!");
rand = new Random();
btnAction.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int a = rand.nextInt(90)+10;
int b = rand.nextInt(90)+10;
int c = rand.nextInt(640)+10;
int d = rand.nextInt(500)+10;
label.setBounds(a, b, c, d);
}
});
btnAction.setBounds(524, 427, 89, 23);
frame.getContentPane().add(btnAction);
}
}
If you want this to happen in a loop at certain times, you can just put it in a loop and use Thread.sleep(amount of miliseconds) in the loop before you run the code.
Why don't you create a JLabel at position a, set it to visible, and another one at position b, setting it to not visible? After the timer is up, hide the first and show the second.
Are you planning on creating some moving imageIcon for some type of game? Or some label that moves pretty much everywhere ?
I would use an Absolute Layout and set Location manually everytime.
myPanel.setLayout(null);
// an initial point
int x = 100;
int y =100;
while (
//some moving pattern
x++; // 1 pixel per loop
y+=2; // 2 pixels per loop
myLabel.setLocation(x,y);
}
I am trying to make this program for minecraft, and now im just getting started. I want that if you click a label, it will check what label is it and will do something.
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
System.out.println(me.getX()+", "+me.getY()+".");
Object source = me.getSource();
int intx = me.getX();
int inty = me.getY();
if(me.getX()>=1 && me.getY()>=1 && me.getX()<=70 && me.getY()<=45){
permissionsframe.setLocation(810,250);
System.out.println(p1p.length);
permissionsframe.pack();
permissionsframe.setSize(200, 200);
permissionsframe.setVisible(true);
JLabel playerperms = new JLabel("Player "+p1s+" has "+p1p.length+" permissions.");
playerperms.setBounds(1, 1, 150, 150);
permissionsframe.add(playerperms);
System.out.println("You chose "+player1.getText()+".");
}
else{
System.out.println("You did not click any label.");
}
}
});
This selection area is adapted to the name I have now - NonameSL. But if the name will be longer or shorter, the selection area will obviuosly be different...
Is there a way to get the excact label? I tried if(source.equals(player1))(Player 1 is the label) but I placed the label in 1, 1 and I have to click the excact point that I defined the label in, X=1, Y=1. How can I make a mouse listener listen to a label?
There is no need to check if the mouse coordinates are inside your JLabel.
You can bind a Listener to every JLabel an process the click/press event in your MyMouseListener.class
To do so:
You have to add the MouseListener to every JLabel:
MyMouseListener myMouseListener = new MyMouseListener();
label01.setName("name01");
label01.addMouseListener(myMouseListener);
label02.setName("name02");
label02.addMouseListener(myMouseListener);
To identify the JLabel you could do something like this:
class MyMouseListener extends MouseAdapter {
#Override
public void mouseClicked(MouseEvent e) {
JLabel l = (JLabel) e.getSource();
if(l.getName().equals("name01"))
doSomething01();
else if(l.getName().equals("name02"))
doSomething02();
}
}
The correct way to do this is to use the .getComponent() method instead of the .getSource() since this is MouseEvent which is something different than ActionEvent.
Implement the Listener to your class:
public class Main implements MouseListener {
public static void main(String[] args) {
//setup JFrame and some label and then
label.addMouseListener(this);
}
#Override
public void mousePressed(MouseEvent e) {
if (e.getComponent().equals(label)) {
System.out.println("clicked");
}
}
//the other orderride methods..
JAVA USING NETBEANS
Hello stackoverflow, i have a problem i would like help with. In a nutshell, I have a mouselistener and a keylistener on a jpanel, everything works fine except when i press one of my jbuttons, then the keylistener goes AWOL. Can any1 explain the problem, is the panels focus now on the buttons instead of the keyboard, im at a lost.
Here is the code, if somethings are not reference, assume its are there the entire panel code was 500+ long so i cut quite a bit.
Thanks in advance for any help.
package tankgame;
public class TankPanel extends JPanel implements KeyListener,
MouseListener,MouseMotionListener
{
JButton back,shop, menu, health, speed, rapidfire, shootradius;
TankPanel()
{
setLayout( null );
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
setFocusable(true);
shop= new JButton("SHOP");
shop.addMouseListener(this);
shop.setBounds(400,0, 80,15);
add(shop);
}
public void keyPressed(KeyEvent k)
{
char c = k.getKeyChar();
if(c=='u')
{
u++;
System.out.println(u+" = u");
}
if(c=='i')
{
i++;
System.out.println(i+" = i");
}
if( c == 'd' )
{
if(Ptank.pic==PlayerTankE)
{
if(Ptank.move==true)
{
Pbarrel.x+=Ptank.speed;
Ptank.x+=Ptank.speed;
}
}
else
{
if(Ptank.pic==PlayerTankN || Ptank.pic==PlayerTankS)
{
Ptank.x = Ptank.x - 5;
Ptank.y=Ptank.y+5;
}
Ptank.setPic(PlayerTankE);
Ptank.width=35;
Ptank.height = 23;
}
}
setFocusable(true);
repaint();
}
public void keyReleased(KeyEvent k)
{
}
public void keyTyped(KeyEvent k)
{
}
public void mouseClicked(MouseEvent e)
{
//Invoked when the mouse button has been clicked (pressed and released)
}
public void mouseEntered(MouseEvent e)
{//Invoked when the mouse enters a component.
}
public void mouseExited(MouseEvent e)
{ //Invoked when the mouse exits a component.
}
public void mousePressed(MouseEvent e)
{//Invoked when a mouse button has been pressed on a component.
if(e.getSource()==back)
{
System.out.println(456);
System.out.println(back.getLocation().x + " "+back.getLocation().y);
}
else if(e.getSource() == menu)
{
changebuttons("menu");
System.out.println(456);
System.out.println(menu.getLocation().x + " "+menu.getLocation().y);
}
else if(e.getSource() == shop)
{
changebuttons("shop");
System.out.println(456);
System.out.println(shop.getLocation().x + " "+shop.getLocation().y);
}
else if(e.getButton() == MouseEvent.BUTTON1)
{
destpoint= new Point();
destpoint.setLocation(mousex, mousey);
origin = new Point();
}
for(int i = 0; i< Ptank.rapidfire; i++)
{
if (origin.distance(destpoint) <= 100 && origin.distance(destpoint) >= 50)
{
Bullet add = new Bullet(this,destpoint);
add.getOrigin(origin);
add.setPic(PlayerBullet);
add.width=4;
add.height=4;
bulletList.add(add);
}
}
}
}
public void mouseReleased(MouseEvent e)
{//Invoked when a mouse button has been released on a component.
}
public void mouseDragged(MouseEvent e)
{//Invoked when a mouse button is pressed on a component and then dragged.
}
public void mouseMoved(MouseEvent e)
{
//Invoked when the mouse cursor has been moved onto a component but no buttons
Cursor cursor = Cursor.getDefaultCursor();
//you have a List<Polygon>, so you can use this enhanced for loop
cursor = Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR);
setCursor(cursor);
mousex=e.getX();
mousey=e.getY();
}
public void changebuttons(String x)
{
if(x.equals("shop"))
{
menu.setBounds(720, 0, 80, 15);
health.setBounds(0, 0, 125, 15);
speed.setBounds(150, 0, 125, 15);
shootradius.setBounds(300, 0, 200, 15);
rapidfire.setBounds(500, 0, 150, 15);
shop.setBounds(1000, 0, 150, 15);
}
}
KeyEvents are only generated on a component that has focus. When you click on the button is now has focus to key events won't be generated on the panel. You need to add:
panel.requestFocusInWindow()
in your ActionListener to give focus back to the panel.
However the better solution is to use Key Bindings as you can add bindings to a KeyStroke even when the component doesn't have focus.
Don't use a KeyListener which requires the component be focused to work. Instead consider using Key Bindings. You can find out how to use these guys at the Swing tutorial: How To Use Key Bindings. If you need more specific help, you will want to post a much smaller bit of code than you show above, code that is self-contained and will actually compile and run for us, an SSCCE.