Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Puzzle Programming - HEEELP! DX

  1. #1
    Junior Member JavaMistress's Avatar
    Join Date
    Apr 2014
    Location
    The Milky Way :3
    Posts
    9
    My Mood
    Stressed
    Thanks
    0
    Thanked 1 Time in 1 Post

    Unhappy Puzzle Programming - HEEELP! DX

    Hello everyone, I will have to finish a school project until the .. 10th of July or, if I am lucky, next week.

    I canīt seem to solve a certain problem in one of my classes that I havenīt even been able to detect yet.

    The code I have for the puzzle so far is this:

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.CropImageFilter;
    import java.awt.image.FilteredImageSource;
     
    import javax.swing.Box;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
     
    public class Puzzle extends JFrame implements ActionListener {
        private CountDown cd;
        private JPanel centerPanel;
        private JButton button;
        private JLabel label;
        private Image source;
        private Image image;
        int[][] pos;
        int width, height;
     
        public Puzzle() {
            CountDown cd = new CountDown();
            add(cd);
            initUI();
        }
     
        public final void initUI() {
     
            pos = new int[][]{
                        {0, 1, 2},
                        {3, 4, 5},
                        {6, 7, 8},
                        {9, 10, 11}
                    };
     
     
            centerPanel = new JPanel();
            centerPanel.setLayout(new GridLayout(4, 4, 0, 0));
     
            ImageIcon lama = new ImageIcon(Puzzle.class.getResource("Lama.png"));
            source = lama.getImage();
     
            width = lama.getIconWidth();
            height = lama.getIconHeight();
     
     
            add(Box.createRigidArea(new Dimension(0, 5)), BorderLayout.NORTH);
            add(centerPanel, BorderLayout.CENTER);
     
     
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 3; j++) {
                    if (j == 2 && i == 3) {
                        label = new JLabel("");
                        centerPanel.add(label);
                    } else {
                        button = new JButton();
                        button.addActionListener(this);
                        centerPanel.add(button);
                        image = createImage(new FilteredImageSource(source.getSource(),
                                new CropImageFilter(j * width / 3, i * height / 4,
                                (width / 3) + 1, height / 4)));
                        button.setIcon(new ImageIcon(image));
                    }
                }
            }
     
            setSize(325, 275);
            setTitle("Puzzle");
            setResizable(false);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        }
     
        public void actionPerformed(ActionEvent e) {
     
            JButton button = (JButton) e.getSource();
            Dimension size = button.getSize();
     
            int labelX = label.getX();
            int labelY = label.getY();
            int buttonX = button.getX();
            int buttonY = button.getY();
            int buttonPosX = buttonX / size.width;
            int buttonPosY = buttonY / size.height;
            int buttonIndex = pos[buttonPosY][buttonPosX];
     
     
     
            if (labelX == buttonX && (labelY - buttonY) == size.height) {
     
                int labelIndex = buttonIndex + 3;
     
                centerPanel.remove(buttonIndex);
                centerPanel.add(label, buttonIndex);
                centerPanel.add(button, labelIndex);
                centerPanel.validate();
            }
     
            if (labelX == buttonX && (labelY - buttonY) == -size.height) {
     
                int labelIndex = buttonIndex - 3;
                centerPanel.remove(labelIndex);
                centerPanel.add(button, labelIndex);
                centerPanel.add(label, buttonIndex);
                centerPanel.validate();
            }
     
            if (labelY == buttonY && (labelX - buttonX) == size.width) {
     
                int labelIndex = buttonIndex + 1;
     
                centerPanel.remove(buttonIndex);
                centerPanel.add(label, buttonIndex);
                centerPanel.add(button, labelIndex);
                centerPanel.validate();
            }
     
            if (labelY == buttonY && (labelX - buttonX) == -size.width) {
     
                int labelIndex = buttonIndex - 1;
     
                centerPanel.remove(buttonIndex);
                centerPanel.add(label, labelIndex);
                centerPanel.add(button, labelIndex);
                centerPanel.validate();
            }
        }
     
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    Puzzle puzzle = new Puzzle();
                    puzzle.setVisible(true);
                }
            });
        }
    }


    I donīt even know where to start searching.

    The CountDown works fine by the way. Itīs the puzzle that sorta fails.
    And BlueJ (I have to use it for school, the shittiest IDE ever..) couldnīt find any syntax errors or Exceptions, etc..
    My IDE thinks that it works, but only shows the CountDown as if everything was alright. :/
    But where did my picture go? :X

    I have the same problem in the two other levels, too.
    One day I got the chance to speak to a hacker.. of course, I asked him to tell me where to start. i asked him what to do and even requested homework...

    He said: "Hehe, you have to work hard on your own little girl.. donīt change your dreams, change the world.."

    ... and thatīs what Iīm going to do. If he is somewhere out there and reads this someday, I thank him for encouraging me. His words have boosted my will to fight!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,034
    My Mood
    Mellow
    Thanks
    184
    Thanked 646 Times in 633 Posts

    Default Re: Puzzle Programming - HEEELP! DX

    Please don't post multiple threads on the same topic. The other post was deleted.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,322
    Thanks
    49
    Thanked 2,194 Times in 2,167 Posts

    Default Re: Puzzle Programming - HEEELP! DX

    Do you have a question about the code you've posted?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member JavaMistress's Avatar
    Join Date
    Apr 2014
    Location
    The Milky Way :3
    Posts
    9
    My Mood
    Stressed
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Puzzle Programming - HEEELP! DX

    Yeah... I canīt figure out the mistake in it.

    I know that there must be one.. a fatal one, actually, but I donīt know where. Whatever it is, the puzzle doesnīt work.
    One day I got the chance to speak to a hacker.. of course, I asked him to tell me where to start. i asked him what to do and even requested homework...

    He said: "Hehe, you have to work hard on your own little girl.. donīt change your dreams, change the world.."

    ... and thatīs what Iīm going to do. If he is somewhere out there and reads this someday, I thank him for encouraging me. His words have boosted my will to fight!

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,322
    Thanks
    49
    Thanked 2,194 Times in 2,167 Posts

    Default Re: Puzzle Programming - HEEELP! DX

    the puzzle doesnīt work.
    Please explain.
    What is the program supposed to do?
    What does it do now?
    Copy the program's output, post it here and explain what is wrong with it and show what it should be.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Word Search Puzzle
    By Arklondonbridge in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 31st, 2014, 12:47 PM
  2. Java puzzle Help needed
    By codoholic in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 25th, 2013, 02:51 PM
  3. 15 square puzzle
    By Yamaha360 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 10th, 2013, 02:10 PM
  4. Puzzle Game. Need some help
    By clydefrog in forum Java Theory & Questions
    Replies: 10
    Last Post: March 12th, 2012, 03:14 PM