user2278779
user2278779

Reputation: 1

Eclipse Indigo bug

I have been running Eclipse Indigo for a few months now, and I have run into a bug that I cannot seem to find an answer to. I am creating a small 2d side-scroller game similar to that of mario, the old zelda, etc.

I was going to show a my dad what new feature I had added into my program. Instead of coming upstairs to see my program on my computer, my dad decided that he could get into it using a sudo screen-viewing thing that I am not sure of. We have used this before, and basically all it does is let you see the screen of another computer in your home (on the same IP interface), and you can use the computer too.

I didn't want to show my program to my dad this way, so I told him to come upstairs. He did, and ever since then, eclipse will not show any graphics inside of your JFrame in your program. It will show things such as words (written on the screen), but will not show ANY graphics. Such as your background image, or your character, or anything else. I am POSITIVE that it isn't some problem with my coding, because I had tested and played the game quite a few times before my dad did the screen-viewing thing (we are both on linux mint 12, BTW).

I think that this bug is related to the screen-viewing thing.

I would love it if I could get some help. any would be great. Thanks.

-This has been solved*

Board

package External;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class Board extends JPanel implements ActionListener, Runnable { 
    Dude p; 
    Image img;    
    Timer time;
    int v = 172;
    Thread animator;

    boolean a = false;
    boolean done2 = false;

    public Board() {
        p = new Dude();
        addKeyListener(new AL());
        setFocusable(true);
        ImageIcon i = new ImageIcon ("/home/clark/Desktop/Swindle_test_background.png");
        img = i.getImage(); 
        time = new Timer (3, this);
        time.start();
    }

    public void actionPerformed(ActionEvent e) {
        p.move();
        repaint(); 
    }
    public void paint(Graphics g) {
        if (p.dy == 1 && done2 == false)
        {
            done2 = true;
            animator = new Thread(this);
            animator.start();
        }
        super.paint(g);
            Graphics2D g2d = (Graphics2D) g;
    if ((p.getX() - 590) % 2400 == 0)
        p.nx = 0;    
    if ((p.getX() - 1790) % 2400 == 0)
        p.nx2 = 0;
            g2d.drawImage(img, 985-p.nx2, 0, null);
            if (p.getX() >= 921)
            g2d.drawImage(img, 985-p.nx, 0, null);
            g2d.drawImage(p.getImage(), 75, v, null);


    }

    private class AL extends KeyAdapter {
        public void keyReleased(KeyEvent e) {
            p.keyReleased(e);
        }
        public void keyPressed(KeyEvent e) {
            p.keyPressed(e);
        }

    }
    boolean h = false;
    boolean done = false;

    public void cycle() {
        if (h == false)
                v--;
        if (v == 125)
                h = true;
        if (h == true && v <= 172 ) {
                v++;
        if (v == 172) {
                    done = true;

        }
    }
}




    public void run() {

        long beforeTime, timeDiff, sleep;

        beforeTime = System.currentTimeMillis();

        while (done == false) {

                cycle();

                timeDiff = System.currentTimeMillis() - beforeTime;
                sleep = 10 - timeDiff;

                if (sleep < 0)
                        sleep = 2;
                try {
                        Thread.sleep(sleep);
                } catch (InterruptedException e) {
                        System.out.println("interrupted");
                }

                beforeTime = System.currentTimeMillis();
        }
        done = false;
        h = false;
        done2 = false;
}

}

Dude

package External;

import java.awt.Image;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

public class Dude {
    int x, dx, y, nx2, nx, dy;
    Image Swindle_Man_Right;
    ImageIcon r = new ImageIcon("/home/clark/Desktop/Swindle_Man_Right.png");
    ImageIcon l = new ImageIcon("/home/clark/Desktop/Swindle_Man_Left.png");
    ImageIcon j = new ImageIcon("/home/clark/Desktop/Swindle_Man_Jump.png");

    public Dude() {
        Swindle_Man_Right = l.getImage();
        x = 75;
        nx2 = 685;
        nx = 0;
        y = 172;

}


public void move() {
    x = x + dx;
    nx2 = nx2 + dx;
    nx = nx + dx;
}
public int getX() {
    return x;
}
public int getY() {
    return y;
}

public Image getImage() {
    return Swindle_Man_Right;
}

public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_LEFT)
    {               dx = -1;
    Swindle_Man_Right = l.getImage();
            }
    if (key == KeyEvent.VK_RIGHT)
            {dx = 1;
    Swindle_Man_Right = r.getImage();  
            }

    if (key == KeyEvent.VK_UP)
            {dy = 1;
            Swindle_Man_Right= j.getImage();
            }                       }

public void keyReleased(KeyEvent e) {
    int key = e.getKeyCode();

    if (key == KeyEvent.VK_LEFT)
            dx = 0;

    if (key == KeyEvent.VK_RIGHT)
            dx = 0;

    if (key == KeyEvent.VK_UP)
            {dy = 0;
            Swindle_Man_Right= r.getImage();}
            }
}

Frame

package External;

import javax.swing.*;

public class Frame {

    public Frame() {
        JFrame frame = new JFrame("Swindle [version 0.1.9]");  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(700,390); 
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        new Frame();
}

}

Upvotes: 0

Views: 110

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347184

As near as I can tell, you've not added anything to the frame.

After I replaced the graphics with my own, I was able to get it running...

enter image description here

public class Frame {

    public static void main(String[] args) {
        new Frame();
    }

    public Frame() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Swindle [version 0.1.9]");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                // This is kind of important...
                frame.add(new Board());

                frame.setSize(700, 390);
                frame.setResizable(false);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

While I've only given the code quick glance, I would recommend that you don't use ImageIcon to load you images and instead use the ImageIO API. Apart from supporting more image formats, it will throw more errors when it can't load the images.

I would also avoid using KeyListener in favor of key bindings. They don't suffer from the same focus issues as KeyListener

Upvotes: 1

Related Questions