views:

177

answers:

2

I am adding images to a JPanel but the images are getting cut off. I was originally trying BorderLayout but that only worked for one image and adding others added image cut-off. So I switched to other layouts and the best and closest I could get was BoxLayout however that adds a very large cut-off which is not acceptable either.

So basically; How can I add images (from a custom JComponent) to a custom JPanel without bad effects such as the one present in the code.

Custom JPanel:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.Timer;

public class GraphicsPanel extends JPanel implements MouseListener {
    private Entity test;
    private Timer timer;
    private long startTime = 0;
    private int numFrames = 0;
    private float fps = 0.0f;

    GraphicsPanel() {      
        test = new Entity("test.png");
        Thread t1 =  new Thread(test);
        t1.start();

        Entity ent2 = new Entity("images.jpg");
        ent2.setX(150);
        ent2.setY(150);
        Thread t2 = new Thread(ent2);
        t2.start();

        Entity ent3 = new Entity("test.png");
        ent3.setX(0);
        ent3.setY(150);
        Thread t3 = new Thread(ent3);
        t3.start();

        //ESSENTIAL COMMENT ANY OF THESE and you will see the problem immediately
        //You can use ANY image to reproduce the problem
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        add(test);
        add(ent2);
        add(ent3);

        //GAMELOOP
        timer = new Timer(30, new Gameloop(this));
        timer.start();
        addMouseListener(this);
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setClip(0, 0, getWidth(), getHeight());

        g2.setColor(Color.BLACK);
        g2.drawString("FPS: " + fps, 1, 15);
    }

    public void getFPS()
    {
        ++numFrames;
        if (startTime == 0) {
            startTime = System.currentTimeMillis();
        } else {
            long currentTime = System.currentTimeMillis();
            long delta = (currentTime - startTime);
            if (delta > 1000) {
                fps = (numFrames * 1000) / delta;
                numFrames = 0;
                startTime = currentTime;              
            }
        }
    }

    public void mouseClicked(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) { }
    public void mouseExited(MouseEvent e) { }

    class Gameloop implements ActionListener
    {
        private GraphicsPanel gp;

        Gameloop(GraphicsPanel gp) {
            this.gp = gp;
        }

        public void actionPerformed(ActionEvent e) {
            try {
                gp.getFPS();
                gp.repaint();
            } catch (Exception ez) { }
        }
    }
}

Main class:

import java.awt.EventQueue;
import javax.swing.JFrame;

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

    private JFrame frame;
    private GraphicsPanel gp = new GraphicsPanel();

    MainWindow()
    {
        EventQueue.invokeLater(new Runnable() {
            public void run() {

                frame = new JFrame("Graphics Practice");
                frame.setSize(680, 420);
                frame.setVisible(true);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                frame.add(gp);
            }
        });
    }
}

Custom JComponent

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JComponent;

public class Entity extends JComponent implements Runnable {
    private BufferedImage bImg;
    private int x = 0;
    private int y = 0;
    private int entityWidth, entityHeight;
    private String filename;

    Entity(String filename) {
        this.filename = filename;       
    }

    public void run() {
        bImg = loadBImage(filename);
        entityWidth = bImg.getWidth();
        entityHeight = bImg.getHeight();
        setPreferredSize(new Dimension(entityWidth, entityHeight));
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();

        g2d.drawImage(bImg, x, y, null);

        g2d.dispose();
    }

    public BufferedImage loadBImage(String filename) {
        try {
            bImg = ImageIO.read(getClass().getResource(filename));
        } catch (Exception e) { }
        return bImg;
    }

    public int getEntityWidth() { return entityWidth; }
    public int getEntityHeight() { return entityHeight; }

    public int getX() { return x; }
    public int getY() { return y; }
    public void setX(int x) { this.x = x; }
    public void setY(int y) { this.y = y; }
}
+2  A: 

One thing I notice is that your preferred size is calculated incorrectly. You set the preferred size to be the size of the image. The problem is you paint the image at (x, y). So the preferred size needs to take that into account.

Otherwise I don't understand the question and running the code doesn't help since I don't know the size of your images whether they should be large, small, same size etc..

camickr
What is happening is when I add an object to GraphicsPanel the layout does not work correctly. For example the images compete for the custom JPanel space. Basically adding components is not as intuitive and easy as it should be with a regular jpanel. Also how do I take into count the x, y position when setting a preferred size?
All components are the same. You use the same code to a regular component or a custom component to a panel. This assumes you created the component correctly and set its properties. correctly. Normally when you add an icon to a label the image is painted at location (0, 0). If in your custom component you are drawing the image at (x, y), then the preferred size of the component increases by the x, y amounts.
camickr
+1  A: 

@camickr is likely right about why your existing approach isn't getting the results you want.

As an alternative, you might consider using JInternalFrame within a JDesktopPane. In this way, your images would be documents that could be individually moved, resized and scrolled. The article How to Use Internal Frames gives an idea of how such an implementation might look. This example shows a simple approach to staggering the frames and selecting them from a menu.

trashgod