0

I've made a few of simple Java applications last year [I am an engineering student and I have fallen in love with Java through a subject last term] - I learned a lot about classes, objects, listeners and various other stuff. The problem is that I had to use unusual libraries (I think they were optimalized to shorten some long parts of code and just to let us focus on the object-oriented fundamentals) - here is link: http://useobjects.net/ (never seen these anywhere else). Now I am trying to create a simple game in Java, (card game, say something like Solitaire) WITHOUT using these libraries - only with swing and another generally-known "core" Java libraries - and I am experiencing various (usually little) issues with mostly "everyday" simple tasks.

First problem - how to add an image from file into Java? Say, I have class Card, that can be shown or reverted, and I need to use 2 images (one for back and one for front); and I couldnt find any tutorial explaining the whole process with BufferedImage, View, Image,... and so on. (for comparison, in the libraries mentioned above, it was just something like this:

ImageView cardBack = new ImageView(positionX, positionY, height, width, filepath, angle etc.);

and the image was added and the task was completed.

Second problem is about main app window. In our old library, it was just something like this in main class:

MainWindow mainWindow = new MainWindow(title,height,width);
GroupView mainGroup = mainWindow.getRootGroup();

But when I tried to find out anything about creating a window, I found only JFrame-solution and I would like to ask you for a simple implementation example, and maybe how to solve classes at all (if should I create a "Main" class where will be JFrame just an element, or to make something like MainWindow class and create JFrame within this class, and create an instance of this class in Main, or ... simply, I am afraid, that my Java coding knowledge and XP is not fully compatible with conventions, so I ask for some recommendation in this way).

Thanks you very much.

Beau Grantham
  • 3,435
  • 5
  • 33
  • 43
  • So what's your question? – NESPowerGlove Mar 04 '14 at 17:49
  • @NESPowerGlove How to add an image from file into java, and a simple implementation example of a JFrame. Not the most well formatted question ever, but the questions are certainly there. – quazzieclodo Mar 04 '14 at 17:56
  • Search for loading BufferedImage with ImageIO on Google, and look through the official Oracle tutorials on how to use Swing as well as constructing a JFrame. – NESPowerGlove Mar 04 '14 at 17:57
  • @NESPowerGlove I know that there is google and many tutorials, but I am asking for these particular solutions not because I am lazy to find them out, but I would like to find here solvings for exactly these issues, considering all specific details mentioned above. Btw, I am not from that sort of people, which ask every simple crap at forums and which expect immediate help from others. I am just sad because I am either dumb or lazy, but I cannot solve these simplicities. – user3380075 Mar 04 '14 at 18:08

2 Answers2

0

you could start with the following code:

public static void show() throws Exception
{
    final BufferedImage image = ImageIO.read( new File( "<path>" ) );

    JFrame window = new JFrame();
    window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    window.setSize( 300, 300 );

    window.setLayout( new BorderLayout() );
    window.add( new JLabel( "My Image name" ), BorderLayout.NORTH );
    window.add( new JImage( image ), BorderLayout.CENTER );

    window.setVisible( true );
}

static class JImage extends JPanel implements MouseListener
{

    private BufferedImage image;

    public JImage( BufferedImage image )
    {
        this.image = image;
        addMouseListener( this );

    }

    @Override
    protected void paintComponent( Graphics g )
    {
        super.paintComponent( g );
        g.drawImage( image, 0, 0, null ); // see javadoc for more info on the parameters
    }

    @Override
    public void mouseClicked( MouseEvent ev )
    {
        System.out.println( String.format( "mouse clicked (x:%d, y:%s)", ev.getX(), ev.getY() ) );
    }

    @Override
    public void mouseEntered( MouseEvent arg0 )
    {}

    @Override
    public void mouseExited( MouseEvent arg0 )
    {}

    @Override
    public void mousePressed( MouseEvent ev )
    {
        System.out.println( String.format( "mouse pressed (x:%d, y:%s)", ev.getX(), ev.getY() ) );
    }

    @Override
    public void mouseReleased( MouseEvent ev )
    {
        System.out.println( String.format( "mouse released (x:%d, y:%s)", ev.getX(), ev.getY() ) );
    }
}

this creates a simple Window containing an image an a label on top. you should probably consider moving to JavaFX but I honestly have little knowledge about client side java ui programming.

Max Fichtelmann
  • 3,366
  • 1
  • 22
  • 27
  • thanks you very much for your time, right now I'm going to try getting familiar with this code and using it well. Just a question - as I'm planning to make something like solitaire, where cards (with images) should be mouse-catchable and movable - will it work when that image will be put into such a layout? I am not sure, if i am not absolutely wrong, but I guess that parts of (f.e.) BorderLayout are not likely to move with just a mouse click. Or its just the magic of MouseListener ? (never applied that on something like this, only at single class) – user3380075 Mar 04 '14 at 18:20
  • as you see, the component with the image is a subclass of JPanel - it should be easy to attach a MouseListener to it - but you should try it out for sure :-) – Max Fichtelmann Mar 04 '14 at 18:33
  • I have edited my original answer to include mouse event capturing – Max Fichtelmann Mar 04 '14 at 18:47
0

"Now I am trying to create a simple game in Java, (card game, say something like Solitaire) WITHOUT using these libraries - only with swing and another generally-known "core" Java libraries"

First of all the code you've provided is not core Java. It's JavaFX. If you want to learn Swing, the Oracle tutorials are a good way to start. You'll find options in that link for either Swing or JavaFX trails

As far as images, you have options. You could load an ImageIcon on to a JLabel or you can paint it onto a JPanel or JComponent. Both options can be seen in this thread

"if should I create a "Main" class where will be JFrame just an element, or to make something like MainWindow class and create JFrame within this class, and create an instance of this class in Main"

See Extends JFrame vs. creating it inside the the program. If you go through a few of the tutorials in the above link, you'll pick up on good practices pretty quick just by looking at the code examples.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720