Member Avatar for san_fran_crisko

Hi,

I am designing a puzzle game and I am trying to use the data that comes back from "getLocation()". It tells me the x, y co-ordinates of a button on the screen but so far I cannot export the x, y data to a String or int.

There is a toString() function but it produces reams of info about the button and truncating/editing it isn't feasible as the co-ordinates could change from 2 or 3 characters. E.g. x= 01, y = 56 // x=101, y = 320

Any ideas or help would be greatly appreciated. I've exhausted the Java API, textbooks and Internet for any information on how to do this.

Thanks,

Crisko.

Here is the code should you need to view it:

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

class MainWindow extends JFrame implements ActionListener {
    
    
    JPanel slider = new JPanel(new GridLayout(3, 3, 3, 3));
    
    JButton one = new JButton("1");
    JButton two = new JButton("2");
    JButton three = new JButton("3");    
    JButton four = new JButton("4");    
    JButton five = new JButton("5");    
    JButton six = new JButton("6");    
    JButton seven = new JButton("7");    
    JButton eight = new JButton("8");
    JButton blank = new JButton();
    

    public MainWindow() {
        
        super("Ciaran's Slider Game");
        setSize(300,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        Container window = getContentPane();
    
        one.addActionListener(this);
        two.addActionListener(this);
        three.addActionListener(this);
        four.addActionListener(this);
        five.addActionListener(this);
        six.addActionListener(this);
        seven.addActionListener(this);
        eight.addActionListener(this);
        
        slider.add(one);
        slider.add(two);
        slider.add(three);
        slider.add(four);
        slider.add(five);
        slider.add(six);
        slider.add(seven);
        slider.add(eight);
        slider.add(blank);
        
        window.add(slider);
        
        setContentPane(window);
        
        setVisible(true);
        
        
    } 
    public void actionPerformed(ActionEvent event){
        
        if (event.getSource() == eight) {
             
             System.out.println(eight.getLocation());
         }
    }
        
        if (event.getSource() == two) {
                
        System.out.println(two.getLocation());
        }    
    } 
    
}

public class SlideGame {

    public static void main(String[] args) {
            
    MainWindow start = new MainWindow();
    }
    
    
}
Member Avatar for san_fran_crisko

Isn't it always the way. Just found a solution to it.

Sorry!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.