1 Replies - 559 Views - Last Post: 05 June 2015 - 03:42 AM Rate Topic: -----

#1 Soaps   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-June 15

Java Final Project

Posted 04 June 2015 - 07:09 PM

I have to make a final project for my Computer Science class. I have already made the graphics for the backgrounds and buttons. I wanted to create a menu system where I can have a background with buttons on top. I was able to get the buttons to be on top by creating a new class that overrides the JPanel class and making that hold my backgrounds.

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

class ImagePanel extends JPanel
{
    private static Image img;
    private static int x;
    private static int y;
    
    public ImagePanel(String img)
    {
        this(new ImageIcon(img).getImage());
    }

    public ImagePanel(Image img)
    {
        this.img = img;
        Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        setSize(size);
        setLayout(null);
    }

    public void paintComponent(Graphics g)
    {
        g.drawImage(getImage(), getImageX(), getImageY(), null);
    }
    
    public static void setX(int n)
    {
        x = n;
    }
    
    public static void setY(int n)
    {
        y = n;
    }
    
    public static void setImage(ImageIcon icon)
    {
        img = icon.getImage();
    }
    
    public static int getImageX()
    {
        return x;
    }
    
    public static int getImageY()
    {
        return y;
    }
    
    public static Image getImage()
    {
        return img;
    }
}



I also have a class called main viewer that displays everything but It isn't working correctly. I want to create a button that, when clicked, changes the background, removes all buttons, and places new buttons so that it feels like you are navigating through a menu system. I'm not sure what the best way to do this is. I have been using a GridBagLayout and GridBagConstraints for the buttons. I don't think that I am doing this right because when I change the image using setImage, the frame stops displaying everything. I would like some help going in the right direction.

What is the best way for me to make a system like this?

P.S. I didn't include the code for the other class because there is more than just this inside and I think explaining it is less complicated than having to explain my cryptic variable names.

Is This A Good Question/Topic? 0
  • +

Replies To: Java Final Project

#2 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3550
  • View blog
  • Posts: 16,190
  • Joined: 20-September 08

Re: Java Final Project

Posted 05 June 2015 - 03:42 AM

You should probably look at CardLayout

http://technojeeves....with-cardlayout
https://docs.oracle....ayout/card.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1