News:

Unnecessarily argumentative

Main Menu

The Programming Thread

Started by Renegnicat, October 27, 2009, 05:10:25 PM

Previous topic - Next topic

LoneMateria

Alright new problem with the game.  So I've finished some of my background functions and some of the core components.  Now I need to display my grid of objects on the screen.   First thing is first I want to put my background in my JPanel.  Well apparently JPanels aren't setup to use JPanel.drawImage(img, x, y, observer).  I've looked for way around this by google searches.  The only one that looks somewhat promising isn't practical for a game because every time it draws an image it grabs the image from its file instead of loading it and storing it in memory to be referenced.  I'm frankly at a loss and was wondering if anyone had ideas on what to do.

***EDIT*** I should probably be a bit more specific here.  I tried to extend JPanel with a different class to take care of this problem  however this created the problem with the images always reloaded from the file.  But i just can't think of a way to do this >.<.  I basically tried to modify this code I found online.


import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class ImagePanel extends JPanel{

    private BufferedImage image;

    public ImagePanel() {
       try {                
          image = ImageIO.read(new File("image name and path"));
       } catch (IOException ex) {
            // handle exception...
       }
    }

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

    }

But honestly I don't want to go and remake my current JPanels into ImagePanels if I don't have to.  So i'm looking for some way to do this ... >.<
Quote from: "Richard Lederer"There once was a time when all people believed in God and the church ruled. This time was called the Dark Ages
Quote from: "Demosthenes"A man is his own easiest dupe, for what he wishes to be true he generally believes to be true.
Quote from: "Oscar Wilde"Truth, in matters of religion, is simpl

Tom62

What you could do, is to preload the images that you'd need at the start of your program and then pass the required image as a parameter in the ImagePanel constructor (or to a setter method to change images) . Something like this:

public class myMainClass {

private final BufferedImage image1;
private final BufferedImage image2;
private ImagePanel panel;

public void myMainMethod() {
      // load all images
      image1 = createImage("filename1");
      image2 = createImage("filename2");
     
      // create ImagePanel with first image
      panel =  new ImagePanel(image1);
      .......

      // change image in ImagePanel to second image
      panel.setImage(image2);
      .......
   }

private BufferedImage createImage(String filename) {
try {
return ImageIO.read(new File(filename));
} catch (IOException ex) {
// handle exception...
}
}
}

public class ImagePanel extends JPanel {
private BufferedImage image;

public ImagePanel(BufferedImage p_image) {
image = p_image;
}

public void setImage(BufferedImage p_image) {
image = p_image;
repaint(); // might not be required
}

@Override
public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters
}
}
The universe never did make sense; I suspect it was built on government contract.
Robert A. Heinlein

LoneMateria

Thank you again Tom ... i'm going to try and implement it tonight.  I was getting so frustrated.  I originally wanted to load all the pictures to memory before my program started and create a loading screen ... this will definitely help me achieve that end.  Thanks again ... I feel like such a noob asking questions >.<

***EDIT*** alright, with a little tinkering the function is up and working ... thanks again tom.  One last question (shouldn't need code this time... hopefully).  I set my JFrame to 1280 X 720 and then I based my JPanels off of those sizes.  However when it first loads it cuts off a small portion of the canvas and I have to manually resize it to display the full image ... I noticed this earlier before I put the images in.  Does Java naturally cut off say ... 20 pixels or so of the JFrame for border (proally more) or did I screw up somewhere?
Quote from: "Richard Lederer"There once was a time when all people believed in God and the church ruled. This time was called the Dark Ages
Quote from: "Demosthenes"A man is his own easiest dupe, for what he wishes to be true he generally believes to be true.
Quote from: "Oscar Wilde"Truth, in matters of religion, is simpl

Whitney

I'm about to finish coding a website tomorrow...be on the lookout for stupid questions (if I run into snags).

Tom62

Hi LoneMateria, the "loss" of 20 pixels is indeed caused by the component's margins (insets) and border.
The universe never did make sense; I suspect it was built on government contract.
Robert A. Heinlein

LoneMateria

Quote from: "Tom62"Hi LoneMateria, the "loss" of 20 pixels is indeed caused by the component's margins (insets) and border.

OK cool thanks again Tom ... that should be the last of my noob questions for a while ... GUI programming in an unfamiliar language is fun... and the experience is invaluable.
Quote from: "Richard Lederer"There once was a time when all people believed in God and the church ruled. This time was called the Dark Ages
Quote from: "Demosthenes"A man is his own easiest dupe, for what he wishes to be true he generally believes to be true.
Quote from: "Oscar Wilde"Truth, in matters of religion, is simpl

LoneMateria

A while wasn't as long as I expected.  So i'm using MouseListener to detect my click events.  2 strange things are happening.  First I declared an array of those ImagePanels from earlier.  Just to make sure my click event is working i'm testing them by  setting this.setVisibile(false);  Basically just turning them invisible.  Now when I click them it will turn many invisible on my first click.  Sometimes it takes more.  However after counting like 80 clicks or so I get an error message I know nothing about:


Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at java.awt.AWTEventMulticaster.mouseExited(Unknown Source)

It also spams the second line at me.  It might have to do with the number of Places I initiated MouseLIstener.  If this is a stack problem is there a way I can clear the stack?

btw this is my ImagePanel Class now and I invoke everything withing my game.java section
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JComponent;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ImagePanel extends JPanel implements MouseListener
{
   private BufferedImage image;
   public static int xPos,
              yPos,
              sWidth,
              sHeight,
              boardX,
              boardY;
   public boolean isTrue = false,
                  isClicked = false,
                  draggedLeft = false,
                  draggedRight = false,
                  draggedUp = false,
                  draggedDown = false;
 
   public ImagePanel(BufferedImage p_image, int x, int y, int width, int height)
   {
      image = p_image;
      xPos = x;
      yPos = y;
      sWidth = width;
      sHeight = height;
      this.addMouseListener(this);
   }
   
   public ImagePanel(BufferedImage p_image)
   {
  image = p_image;
  isTrue = true;
   }

   public void setImage(BufferedImage p_image)
   {
      image = p_image;
      this.addMouseListener(this);
      repaint(); // might not be required
   }
   public void setImage(BufferedImage p_image, int x, int y, int width, int height)
   {
  image = p_image;
  xPos = x;
  yPos = y;
  sWidth = width;
  sHeight = height;
  this.addMouseListener(this);
  repaint();
   }
   public void setBoardPos(int x, int y)
   {
  boardX = x;
  boardY = y;
  this.addMouseListener(this);
  repaint();
   }


   @Override
   public void paintComponent(Graphics g)
   {  
  if(isTrue == true)
  g.drawImage(image, 0, 0, null);
  else
  g.drawImage(image, xPos, yPos, sWidth, sHeight, null);
  this.addMouseListener(this);
  repaint();
   }

   @Override
public void mouseClicked(MouseEvent e)
{
// TODO Auto-generated method stub
  if (isClicked == false)
      isClicked = true;
  else
  isClicked = false;
  this.setVisible(false);
}

@Override
public void mouseEntered(MouseEvent e)
{
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e)
{
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e)
{
// TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e)
{
// TODO Auto-generated method stub
}
}

Any suggestion would be greatly appreciated.
Quote from: "Richard Lederer"There once was a time when all people believed in God and the church ruled. This time was called the Dark Ages
Quote from: "Demosthenes"A man is his own easiest dupe, for what he wishes to be true he generally believes to be true.
Quote from: "Oscar Wilde"Truth, in matters of religion, is simpl

LoneMateria

I think I may have fixed it.  The way I had it set up was I was adding a MouseListener to every instance of ImagePanel I had.  Well the JPanel that the game is in is an ImagePanel.  Once I took the mouse click feature away from it it seems like the problem went away.  Hopefully that was it.
Quote from: "Richard Lederer"There once was a time when all people believed in God and the church ruled. This time was called the Dark Ages
Quote from: "Demosthenes"A man is his own easiest dupe, for what he wishes to be true he generally believes to be true.
Quote from: "Oscar Wilde"Truth, in matters of religion, is simpl

AlP

The exception is a stack overflow so you've probably got some kind of unintended recursion. I'm not familiar with Java GUI events so my spidey-sense isn't working. Have you tried debugging it in the Eclipse debugger or similar? You can set that to freeze the execution of the program when the exception is thrown. If you look at the call stack you should get a better idea of what's going on.
"I rebel -- therefore we exist." - Camus

LoneMateria

Quote from: "AlP"The exception is a stack overflow so you've probably got some kind of unintended recursion. I'm not familiar with Java GUI events so my spidey-sense isn't working. Have you tried debugging it in the Eclipse debugger or similar? You can set that to freeze the execution of the program when the exception is thrown. If you look at the call stack you should get a better idea of what's going on.

I have no recursion in my program (I was going to use it but found an easier way).  Anyway It is no longer giving me the error message.  It looks like the problem was that I kept adding a new MouseListener to it every time I accessed a function.  I was trying to figure out where to put them and got frustrated and threw them in everything ... and it worked ... until it gave me that error message.  Anyway I changed how I implemented them to make damn sure I only implemented them once.  I haven't had a problem since.

I also figured out the clicking problem.  I still got to write the drag function but when I click I have to keep my mouse still and click on the object, even the slightest movement will activate a different handler.  These objects have a few handlers for Mouse features and I need to figure out which ones I will use and delete the rest.  But one step at a time.  I tell you what this is a very good learning experience for me.  I still don't like java but i'm learning a ton of different things.

You should try doing some GUI work in java.  Its ... different lol.
Quote from: "Richard Lederer"There once was a time when all people believed in God and the church ruled. This time was called the Dark Ages
Quote from: "Demosthenes"A man is his own easiest dupe, for what he wishes to be true he generally believes to be true.
Quote from: "Oscar Wilde"Truth, in matters of religion, is simpl

Renegnicat

#40
*deleted to protect Idea*
[size=135]The best thing to do is reflect, understand, apreciate, and consider.[/size]

AlP

It's a brilliant idea. A few thoughts...

Google Toolbar without the need to get information from all the DNS servers.

Web browsers such as Chrome "warm up" the DNS server by resolving the URLs on the pages a user visits to IP addresses, which might create noise.

Companies such as OpenDNS are probably already doing this if they have any sense.
"I rebel -- therefore we exist." - Camus

Renegnicat

Well, the main problems I can see is that I am uncertain whether I will even be able to access DNS server logs, as I have no idea how to go about doing that, and since I'm not a DNS server administrator... I don't think I want to hack into anything.

You said that google get's this information through google toolbar, though. How does that work? I don't think I could do the same unless I already built up a good size of links.  ;)
[size=135]The best thing to do is reflect, understand, apreciate, and consider.[/size]

AlP

Quote from: "Renegnicat"Well, the main problems I can see is that I am uncertain whether I will even be able to access DNS server logs, as I have no idea how to go about doing that, and since I'm not a DNS server administrator... I don't think I want to hack into anything.

You said that google get's this information through google toolbar, though. How does that work? I don't think I could do the same unless I already built up a good size of links.  ;)

Scroll to the end of this page.
http://www.google.com/privacy_faq.html
"I rebel -- therefore we exist." - Camus

AlP

This is awesome! NPTEL (joint venture between the Indian Institutes of Technology and the Indian Institute of Science) seems to have made a lot of their college Computer Science lectures available as video online. I'm talking like entire courses. Look for the ones that have "video" in the type column. I have a bachelors degree in Computer Science and having reviewed some of them they appear to be good. Let me know what you think. I'm still working my way through. It could take months =). Don't worry about hurting their bandwidth; the videos are served on youtube. Now CS education is as easy to find as porn! :yay:

http://nptel.iitm.ac.in/courses.php?branch=Comp

Sample:

[youtube:r6pv7chj]http://www.youtube.com/watch?v=xlUFkMKSB3Y[/youtube:r6pv7chj]
"I rebel -- therefore we exist." - Camus