Displaying images in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tommyny04
    New Member
    • Sep 2007
    • 6

    #1

    Displaying images in Java

    I'm kinda new to programming but I usually figure things out by myself but I need help with this. I'm trying to display an image in the Center region of a JFrame and other icons/button in the other regions. So far, I have this:

    [code=java]
    public class RiskFrame extends JFrame implements ActionListener, MouseListener{

    private BufferedImage bImage;
    private Image image;

    private JPanel topPanel = new JPanel();
    private JPanel bottomPanel = new JPanel();
    private JPanel centerPanel = new JPanel();
    private JPanel rightPanel = new JPanel();

    private JButton saveButton = new JButton("Save", new ImageIcon("save .jpg"));
    private JButton openButton = new JButton("Open", new ImageIcon("Open Icon.gif"));
    private static JButton newButton = new JButton("New");
    private JButton addTerritory = new JButton("Add New");
    private JButton removeTerritory = new JButton(" Remove");

    private JLabel goodStatus = new JLabel();
    private JLabel badStatus = new JLabel();

    private JPanel territoryPanel = new JPanel();
    private JLabel territoryInfo = new JLabel("Territo ry Info");
    private JLabel country = new JLabel("Country : ");
    private JTextField countryField = new JTextField(20);
    private JPanel countryComp = new JPanel();
    private JLabel continent = new JLabel("Contine nt: ");
    private JTextField continentField = new JTextField(20);
    private JPanel continentComp = new JPanel();
    private JLabel cardType = new JLabel("Card Type: ");
    private JTextField cardField = new JTextField(20);
    private JPanel cardComp = new JPanel();
    private JLabel redColor = new JLabel("R Color #: ");
    private JTextField redColorField = new JTextField(20);
    private JPanel redComp = new JPanel();
    private JLabel greenColor = new JLabel("G Color #: ");
    private JTextField greenColorField = new JTextField(20);
    private JPanel greenComp = new JPanel();
    private JLabel blueColor = new JLabel("B Color #: ");
    private JTextField blueColorField = new JTextField(20);
    private JPanel blueComp = new JPanel();

    public RiskFrame(){
    layoutGUI();
    centerPanel.add MouseListener(t his);
    }



    public void layoutGUI(){
    topPanel.add(ne wButton);
    topPanel.add(op enButton);
    topPanel.add(sa veButton);
    topPanel.add(ad dTerritory);
    topPanel.add(re moveTerritory);
    bottomPanel.add (goodStatus);

    rightPanel.setL ayout(new BorderLayout()) ;
    rightPanel.add( territoryInfo, BorderLayout.NO RTH);
    territoryPanel. setLayout(new BoxLayout(terri toryPanel, BoxLayout.Y_AXI S));
    territoryPanel. setAlignmentX(L EFT_ALIGNMENT);
    territoryPanel. add(Box.createR igidArea(new Dimension(5,5)) );

    countryComp.add (country);
    countryComp.add (countryField);
    continentComp.a dd(continent);
    continentComp.a dd(continentFie ld);
    cardComp.add(ca rdType);
    cardComp.add(ca rdField);
    redComp.add(red Color);
    redComp.add(red ColorField);
    blueComp.add(bl ueColor);
    blueComp.add(bl ueColorField);
    greenComp.add(g reenColor);
    greenComp.add(g reenColorField) ;

    territoryPanel. add(countryComp );
    territoryPanel. add(continentCo mp);
    territoryPanel. add(cardComp);
    territoryPanel. add(redComp);
    territoryPanel. add(greenComp);
    territoryPanel. add(blueComp);

    rightPanel.add( territoryPanel, BorderLayout.CE NTER);
    this.add(topPan el, BorderLayout.NO RTH);
    this.add(rightP anel, BorderLayout.EA ST);
    this.add(center Panel, BorderLayout.CE NTER);
    this.add(bottom Panel, BorderLayout.SO UTH);

    newButton.addAc tionListener(th is);
    openButton.addA ctionListener(t his);
    saveButton.addA ctionListener(t his);
    addTerritory.ad dActionListener (this);
    removeTerritory .addActionListe ner(this);
    }



    public void actionPerformed (ActionEvent e){
    String command = e.getActionComm and();
    if (command.equals ("New")){
    newGame();
    }
    else if (command.equals ("Save")){
    save();
    }
    else if (command.equals ("Open")){
    open();
    }
    }

    public void newGame(){

    String inputValue = JOptionPane.sho wInputDialog("P lease enter the name of the map you would like to use. (Map must already be saved in directory)");

    Icon icon = new ImageIcon(input Value + ".png");
    final JLabel imageLabel = new JLabel(icon);
    centerPanel.add (imageLabel);
    repaint();

    try{
    File file = new File(inputValue + ".board");
    boolean success = file.createNewF ile();
    if (success){}
    else {
    JOptionPane.sho wMessageDialog( null, "File already exists. Please try again");
    }
    }
    catch (IOException e){}
    }

    public void open(){

    String inputValue = JOptionPane.sho wInputDialog("P lease enter the name of the board file you would like to use. (File must already be saved in directory)");

    try {
    FileReader fr = new FileReader(inpu tValue + ".board");
    BufferedReader br = new BufferedReader( fr);
    }
    catch(IOExcepti on e){}

    JLabel imageLabel = new JLabel();
    ImageIcon icon = new ImageIcon(input Value + ".png");
    imageLabel.setI con(icon);
    centerPanel.add (imageLabel);
    repaint();
    }

    public void getColor(int x, int y){
    Raster raster = bImage.getRaste r();
    Object data = raster.getDataE lements(x, y, null);
    ColorModel model = bImage.getColor Model();
    int rgb = model.getRGB(da ta);
    Color color = new Color(rgb);
    String pixel = color.toString( );
    System.out.prin tln(pixel);

    }

    public void mouseClicked(Mo useEvent e) {
    getColor(e.getX (), e.getY());
    }

    public void save(){}

    public static void main(String[] args){

    RiskFrame frame = new RiskFrame();
    frame.setSize(8 00,800);
    frame.setVisibl e(true);
    frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);
    }
    [/code]

    It's not anywhere near done but everytime I run my program, instead of the image appearing in the center region, I will have to resize the window to get the image to appear even though I call the repaint method. Also, my getColor method doesn't work. I always get a nullPointerExce ption when I try to click on the map.

    Any help would be greatly appreciated. THANK YOU!!
    Last edited by JosAH; Sep 30 '07, 11:46 AM. Reason: added [code] ... [/code] tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You shouldn't add these components directly to the JFrame; first grab the frame's
    'contentpane' and add all the components to that container. For an explanation
    read the API documentation for the JFrame class.

    kind regards,

    Jos

    Comment

    Working...