tags:

views:

232

answers:

2

Hello all, I'm working with java images for the first time and having a problem viewing them when the applet loads. The code I've posted below is a dramatically pared-down version of the code I'm actually working with, hopefully figuring out why I can't see an image with this code will show me while I have to resize the window to see images with this code. All help is greatly appreciated and thanks are extended in advance :)

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.*;
import java.awt.Graphics;


public class example extends JApplet implements Runnable
{


boolean updating;
Thread thread;
private int width, height;

TestImageDraw aTable;     //used to create and store values


private AudioClip[] sounds = new AudioClip[4];    //array to hold audio clips
private int counter = 0;      //counter for audio clip array

private Image GameImage;
private Graphics GameGraphics;


public example() //set up applet gui
{

 this.resize(new Dimension(600, 500));


    //setup table
  //aTable = new Table(50, 50, 50, 50, 16, 16, getImage("images/FLY.gif", Color.white),
    //getImage("images/FlySwatter.gif", Color.white)); //Table must be square or flyswatter wont move straight

  aTable = new TestImageDraw(getImage("images/FLY.gif", Color.white));

  //this.add(aTable);
  super.resize(800, 600);

  repaint();

}

public void init()
{
    width = getSize().width;
    height = getSize().height;
    GameImage = createImage(width, height);
    GameGraphics = GameImage.getGraphics();
    // Automatic in some systems, not in others
    GameGraphics.setColor(Color.black);

  repaint();
   validate();

}



public void start()
{
 thread = new Thread(this);
 thread.start();

    }

public void stop()
{
 updating = false;
}

public void run()
{
 while(updating)
 {
  //aTable.update();
}
aTable.revalidate();
}

//returns a transparent image.
//color is made transparent
private Image getImage(String imgPath, final Color color)
{
 Image img = Toolkit.getDefaultToolkit().getImage(imgPath);

    ImageFilter filter = new RGBImageFilter() {
        // the color we are looking for... Alpha bits are set to opaque
        public int markerRGB = color.getRGB() | 0xFFFFFF;

        public final int filterRGB(int x, int y, int rgb) {
          if ( ( rgb | 0xFF000000 ) == markerRGB ) {
            // Mark the alpha bits as zero - transparent
            return 0x00FFFFFF & rgb;
            }
          else {
            // nothing to do
            return rgb;
            }
          }
        };
        ImageProducer ip = new FilteredImageSource(img.getSource(), filter);
        img = Toolkit.getDefaultToolkit().createImage(ip);

        return img;
}


}

TestImageDraw.java

import java.awt.*;  
  import java.util.Random;  
  import javax.swing.*;  

public class TestImageDraw extends JPanel
{

Image itemImg; // stores the item image

public TestImageDraw(Image itemImg)
{

 this.itemImg = itemImg;

}


/** Description of draw(Graphics g)
*
* Function draws the lines used in the table
* @param g object used to draw the table
* @return none
*/
public void draw(Graphics g)
{
  Graphics2D g2=(Graphics2D)g;
  //draw flyswatter
  drawValues(g2); //draw values

    }

    private void drawValues(Graphics g)
{

    g.drawImage(itemImg,20,140,30,40, null);

   g.setColor(Color.black);  // set color of table to black

}


}
A: 

This is NOT a simple example there is still way to much junk in the code. For example, what does all the image filtering have to do with display an image? What does all the Thread code have to do with displaying an image?

After I spent an hour with you yesterday teaching you the basics of painting you haven't listened to a thing I said.

I taught you all about overriding paintComponent(). I pointed you to the Swing tuturial which has a working example of using an image. The structure of your applet looks nothing like the example in the tutorial. Your example will be much simple since you don't have to worry about the animation.

Not only did you waste my time yesterday, but know you are attempting to waste other peoples time.

Learn from the tutorial and post a proper SSCCE.

camickr
Obviously you didn't teach me all about overriding paintComponent() as I'm still asking questions. I don't understand what can be removed from the code so I leave functions in which I think may be important. After I took out all the things you recommended in our last conversation, the program doesn't draw anything, so I have no idea what's going on. I've read the swing tutorial and when I tried compiling the sample code, I got "uses or overrides a deprecated API" errors, so not much help there. I appreciate you trying to help me, but no one is forcing you to "waste" your time with me.
danwoods
I was under the impression that code like this was exactly what you recommended when you said "Create a simple custom JPanel that has a simple drawSTring(...) and add it to you JApplet and get that working first." (except that I need to draw an image, not a string, and I can't get it working, which is why I'm asking questions)...
danwoods
You learned about paintComponent you your posting: http://stackoverflow.com/questions/1830603/drawing-text-within-a-jpanel. I also referred you a second time to the Swing tutorial on Custom Painting. Again, the code here looks nothing like the example code from the tutorial on "How to Make Applets". You claim the problem with this code is that it won't display images, well it won't even work with a drawString(), so you ignored that suggestion as well.
camickr
I know I don't have to help. My comment was aimed as a warning to others that you have NOT listened to my suggestions, nor have you read the tutorials that you where pointed to. Therefore the implication is that you are expecting us to spoonfeed the answer to you, even though the tutorial contains a WORKING example (that you will need to understand and modify for your purposes).
camickr
Yes I did recommend creating a simple example. As I stated in my first posting, this is NOT a simple example. I maintain you first need to get an applet working that simply does a drawString(). That should be about 10-20 lines of code. YOu have not yet done that so how can you possible move on the next, more complicated step of showing an image?
camickr
Just because I'm having trouble understanding examples and tutorials does not mean I'm asking anyone to "spoonfeed" me anything. I came here for some extra help when reading the tutorials wasn't enough to make it "click". Neither you nor Sun is the world's greatest teacher, so please don't be offended if I keep asking for help
danwoods
And my panel class is 21 lines with the excess whitespace removed, I thought that would be close enough. I'll keep working on it.
danwoods
Where have you asked a specific question about a line of code or a staement from a tutorial? If there is something you don't understand then ask a direct question, don't ask us to fix your code. All you are doing is making up code and not even following code from the tutorial or old code from the paintComponent() method that you accepted two postings ago. That is why I am frustrated because you can't even follow working code from a previous posting.
camickr
Excuse me my teaching skills and the tutorial examples are fine thank you, its your reading skill that need to be improved. In the "Custom Painting" tutorial, the examples implements "getPreferredSize" and "paintComponent". In your code you make up two methods "draw" and "drawValues". How is that a problem with the tutorial? In the "Make an Applet" tutorial the images are read by using "getResourceAsStream". Where is that method in your code? You can lead a horse to water but you can't make a horse drink. Don't blame me or the tutorial.
camickr
I'm not blaming anyone. I wasn't being derogatory when I said you weren't the world's greatest teacher (I see how that could be inferred though), I just meant that the way you explain things may not work for everyone
danwoods
So I guess the "draw" and "drawValues" methods are unnecessary? Then where would I put the call to drawImage()?
danwoods
Again, sorry if you thought I was being insulting or derogatory, not my intent at all
danwoods
got it figured out. Thanks
danwoods
A: 

The answer is changing the draw() method in the class that extends JPanel to paintComponent() and switching the last parameter in the call to drawImage() to 'this' instead of 'null'. Worked instantly and perfectly!

danwoods
So why wan't my answer accepted. How many times did I suggest you needed a paintComponent() method or to use "this" instead of "null".
camickr