I am doing a project like a car gallery system and need to ask for help...
Suppose I want to create a class call "Brand", it is basically just to store the brand name, a brief description, and also need to give it a picture.

Says: Toyota, it is a Japanese car blah blah blah, then plus an image.

So inside the class, how do I store the attribute of Image?
I've wrote 2 separate class which is "Brand" + "LoadImage", how do I link them together? or possibly to combine them into a single class...

Here my code...

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class LoadImage extends Component {

    private static final long serialVersionUID = 1L;
    BufferedImage img;

    public void paint(Graphics g) {
        g.drawImage(img, 0, 0, null);
    }

    public LoadImage() {
        try {
            img = ImageIO.read(new File("logo.png"));
        } catch (IOException e) {
        }

    }

    public Dimension getPreferredSize() {
        if (img == null) {
            return new Dimension(100, 100);
        } else {
            return new Dimension(img.getWidth(null), img.getHeight(null));
        }
    }
}
//    public static void main(String[] args) {
//
//        JFrame f = new JFrame("Load Image Sample");
//
//        f.addWindowListener(new WindowAdapter() {
//
//            @Override
//            public void windowClosing(WindowEvent e) {
//                System.exit(0);
//            }
//        });
//
//        f.add(new LoadImage());
//        f.pack();
//        f.setVisible(true);
//    }
//}
public class Brand {

    private String brandName;
    private String description;
    // possible just do -> "LoadImage loadImg = new LoadImage()" ?

    public Brand(String b, String d) {
        setBrand(b);
        setDescription(d);
        // how to pass Image param to here also ?
    }

    public void setBrand(String b) {
        brandName = b;
    }

    public String getBrand() {
        return brandName;
    }

    public void setDescription(String d) {
        description = d;
    }

    public String getDescription() {
        return description;
    }
}

Appreciate for any helps, thanks... =)

After a very quick look I can see no reason why you can't just add a private LoadImage loadImg = new LoadImage(); to your class like you said - the constructor for LoadImage will do the necessary initialisation, so there's no need to add it to Brand's constructor. You will want a get method for it.
ps "LoadImage" sounds like a verb; it's not a good class name because it sounds like a method. How about changing it to BrandImage?

After a very quick look I can see no reason why you can't just add a private LoadImage loadImg = new LoadImage(); to your class like you said - the constructor for LoadImage will do the necessary initialisation, so there's no need to add it to Brand's constructor. You will want a get method for it.
ps "LoadImage" sounds like a verb; it's not a good class name because it sounds like a method. How about changing it to BrandImage?

pps: This is a really stupid piece of code:

catch (IOException e) {
}

... if you have any kind of problem finding and reading the file you just told Java to throw away the error message it carefully constructed, and not tell you there was a problem at all.
Please put an e.printStackTrace() into the catch so you know what's happening.

Owh it is a class name MUST be a noun?
I just started to learn JAVA since the beginning of last month, still not really know the naming convention... =)

PS JamesCherrill, the implementation of catch (IOException e), or I can say the whole class of LoadImage is basically copy from the Java text book, so I have no idea why need this haha... =)

anyway, thanks for help, I will go try it out... =D

It's an Object-Oriented way of thinking, that classes represent things and methods represent actions, so class names will usually be nouns and method names usually verbs. This is NOT mandatory. You can call you classes (almost) anything you want. It's just that when I see a class name that is a verb then I suspect a problem with the thinking behind the design. That's all.
If you haven't covered try/catch yet in your course don't worry. Just put the line I told you inside the currently empty { } after the catch. It could save you hours of debugging time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.