-1

This is an example i am working with to LEARN java, i am trying to make one of the boxes a .png file, I have on my computer, but as you can probably already guess it doesn't work. I was thinking that it had to do with my class being string text. My questions are:

1) Would i have to make a new class for an image?, but i think all arguments were still passed along as string anyway? 2) Should i try to use another method such as "repainting the canvas"? 3) Should i use ImageIcon(s) instead? on ImangePanel?

Here is my source code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Toolkit;
import javax.swing.ImageIcon;  
import javax.swing.JFrame;  
import javax.swing.JPanel;  
import java.awt.Dimension;  
import java.awt.Graphics;  
import java.awt.Image; 


public class DragButton extends JButton{

    private volatile int draggedAtX, draggedAtY;

    public DragButton(String text){
        super(text);
        setDoubleBuffered(false);
        setMargin(new Insets(0, 0, 0, 0));  
        setSize(100, 100);
        setPreferredSize(new Dimension(25, 25));

        addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent e){
                draggedAtX = e.getX();
                draggedAtY = e.getY();
            }
        });

        addMouseMotionListener(new MouseMotionAdapter(){
            public void mouseDragged(MouseEvent e){
                setLocation(e.getX() - draggedAtX + getLocation().x,
                        e.getY() - draggedAtY + getLocation().y);
            }
        });
    }

    public static void main(String[] args){

        JFrame frame = new JFrame("DragButton");
        frame.setLayout(null);
        frame.getContentPane().add(new DragButton("1")).setLocation(300, 500);
        frame.getContentPane().add(new DragButton("2")).setLocation(100, 100);
        frame.getContentPane().add(new DragButton("3")).setLocation(75, 75);
        frame.getContentPane().add(new DragButton("yo")).setLocation(310, 310);
        ImagePanel panel = new ImagePanel(
           new ImageIcon("C:\\Users\\Donnie\\Desktop\\gtav.jpg").getImage());  
        frame.getContentPane().add(panel);  


        Toolkit tk = Toolkit.getDefaultToolkit();  
        int xSize = ((int) tk.getScreenSize().getWidth());  
        int ySize = ((int) tk.getScreenSize().getHeight());  
        frame.setSize(xSize, ySize);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Shawn
  • 933
  • 4
  • 18
  • 29
  • 1
    How can you tag this question for both android and Swing? That makes no sense in any fashion. It's either one or the other (and looks to be Swing per my view). Please fix this to avoid confusion. – Hovercraft Full Of Eels Feb 09 '14 at 03:18
  • OK, if you won't fix it, I will -- [tag:android] tag removed. Please take care to use appropriate tags in the future as they are key to attracting the right expert to your question. – Hovercraft Full Of Eels Feb 09 '14 at 03:24
  • Also, this is unclear to me: `"i am trying to make one of the boxes a .png file"`. What is "one of the boxes"? What box? What are you trying to do with the image? Move it with a MouseListener? Or are you trying to move your buttons? – Hovercraft Full Of Eels Feb 09 '14 at 03:30
  • sorry , but if you ran the program there are box's that appear on the screen and i am trying to make one repainted as a picture – Shawn Feb 09 '14 at 03:34
  • Please have a look at this [answer](http://stackoverflow.com/a/9866659/1057230), hopefully the last link might be able to give you a good understanding, how to go about doing this :-) What if you use a simple [JLabel](http://docs.oracle.com/javase/tutorial/uiswing/components/label.html) to show images? – nIcE cOw Feb 09 '14 at 04:33

1 Answers1

1

One problem is that you're using a null layout for your JFrame, and when doing this, you are responsible for setting the size and position of every component added, including your ImagePanel instance. So if you absolutely have to use a null layout (and I generally try to avoid doing this), in the very least, set the bounds of your ImagePanel instance.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373