0

Okay, so my friend and I are making are a game that requires some pixel analysis for the rectangles, and I wrote the code to analyze the rectangles, and It works swimmingly, However, when I try to draw the bufferedImage it doesn't show up. The main issue is that my java class has never taught us how to use Jframes, and I don't want to change my code to accommodate:

        // Threaded Applet Template that works with Macs


// Import section
// Use this section to add additional libaries for use in your program.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.image.BufferedImage;
import java.awt.Toolkit;
import javax.imageio.*;
import javax.swing.*;
import java.awt.image.*;
import java.io.*;


// This begins the class definition.
// Notice that this is a "world".  You can tell since it extends Applet.
// It also implement Runnable which allows it to be and use threads.

public class PixelAnalyzetest extends Applet implements Runnable
{
    //variable declaration section
    // public  datatype  variablename

    public int xTitle;
    public int yTitle;
    public Analyzer analyzer;
    public BufferedImage test;
    public Image itest;
    public boolean analyzeonce=true;
    //declare your Hero object here


    //Sets up a Thread called thread
    Thread thread;





    // Method definition section
    // init() is the first method an Applet runs when started
    public void init()
    {
        //Initialize variables
        xTitle=10;
        yTitle=10;


        BufferedImage test = new BufferedImage(1200,1200, BufferedImage.TYPE_INT_ARGB);
        //test = getImage(getDocumentBase(),"test.png");
        System.out.println("the width of the image is: "+test.getWidth(this));
        try{
            test = ImageIO.read(new File("test.png"));
        } catch(IOException e) {};

        //itest=test.getAsBufferedImage();
        analyzer = new Analyzer(test, 5);

        //construct objects
        //construct your hero here!





        //Set up the thread
        //These should be the LAST lines in your init( ) method.
        thread = new Thread(this);  //constructs a new thread
        thread.start();             //starts the thread

    }//init()




    // paint() is used to display things on the screen
    public void paint(Graphics g)
    {
        setSize(1200,1200);
        //Put the title on the screen.
        /*for(int x=0;x<test.getWidth();x++)
        {
            for(int y=0;y<test.getHeight();y++)
            {
                BufferedImage.setRGB(x,y,analyzer.pictureRGB[x][y]);
            }
        }*/

        g.drawImage(test,0,0,1200,1200,this);


        //draw your hero's name here using g.drawString( )




    }// paint()




    // every thread needs a run method
    // this is what the thread will do
    public void run() {

        // this thread loop forever and runs the paint method and then sleeps.
        while(true)
        {

            //put what you want your program to do here.

            //move your hero here by calling its move() method.

            if(analyzeonce==true)
            {
                analyzer.analyzelines();
                analyzeonce=false;
            }




            repaint();      // run the paint method.

            //sleep
            try {
            thread.sleep(100);
            }
            catch (Exception e){ }
        }//while

    }// run()





}

Now my analyzer class:

        import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.image.BufferedImage;
import java.awt.Toolkit;

public class Analyzer
{
    public int[][] pictureRGB;
    //VARIABLE DECLARATION SECTION
    //Here's where you state which variables you are going to use.
    public String name;     //use this format    public datatype variablename;

    public boolean isAlive;
    public int health;
    public int strength;
    public String theGlove;
    public int dx, dy;
    public BufferedImage analyzing, bgrab;
    public int imagewidth, imageheight;
    public int linecounter = 0;
    public int boxcounter = 0;
    public boolean endline = false;
    public Line lines[];
    public Box boxes[];
    public int totalboxes;
    public boolean donean = false;

    // METHOD DEFINITION SECTION

    // Constructor Definition
    // A constructor "builds" the object when called and give the variable initial values.
    // This constructor build Heroes with all the same initial values.

    public Analyzer(BufferedImage grab, int xtotalboxes)
        {
            //BufferedImage bgrab = (BufferedImage) grab;
            //bgrab = new BufferedImage(1200,1200, BufferedImage.TYPE_INT_ARGB);
            //bgrab.getGraphics().drawImage(grab,0,0,bgrab.getWidth(),bgrab.getHeight(),null);
            analyzing = grab;
            totalboxes = xtotalboxes;
            imagewidth=analyzing.getWidth();
            System.out.println(imagewidth+" Is the image's Width");
            imageheight=analyzing.getHeight();
            System.out.println(imagewidth+" Is the image's Height");
            pictureRGB= new int[imagewidth] [imageheight];
            lines = new Line[10];
            boxes = new Box[20];
            isAlive = true;
            health = 200;
            strength = 20;
            for(int xpos = 1; xpos<imagewidth-1;xpos++)
            {
                for(int ypos = 1; ypos<imageheight-1; ypos++)
                    {
                        pictureRGB[xpos][ypos]=analyzing.getRGB(xpos,ypos);
                        //System.out.println(pictureRGB[xpos][ypos]);
                    }
            }
        }
    public void analyzelines()
    {

        for(boxcounter=0; boxcounter<boxes.length; boxcounter++)
        {
            int tempx = 0;
            int tempy = 0;
            int tempw = 0;
            int temph = 0;
            //int pxpos = 0;
            //int pypos = 0;
            boolean startline=false;
            boolean foundw = false;
            boolean foundh = false;
            if(donean==false)
            {
            for(int ypos = 1; ypos<imageheight-1; ypos++)
            {
                for(int xpos = 1; xpos<imagewidth-1;xpos++)
                {

                    if(boxcounter>0)
                    {
                        if(pictureRGB[xpos][ypos]==-3584 && startline==false && boxes[boxcounter-1].rect.contains(new Point(xpos,ypos))==false && boxes[boxcounter-1].rect.intersects(new Rectangle(xpos-2,ypos-2,4,4))==false && isEqual(new Rectangle(xpos-2,ypos-2,4,4))==false)
                            {
                                System.out.println("--------------------------------START BOX --------------");
                                System.out.println("The top left corner of the box is: ("+xpos+","+ypos+")");
                                startline=true;
                                tempx=xpos;
                                tempy=ypos;
                                //System.out.println(tempx+"<<TEMPX TEMPY>>"+tempy);
                            }
                        if(startline==true && pictureRGB[xpos+1][ypos]!=pictureRGB[xpos][ypos] && foundw==false && foundh==false && boxes[boxcounter-1].rect.contains(new Point(xpos,ypos))==false  && isEqual(new Rectangle(xpos-2,ypos-2,4,4))==false)
                            {
                                tempw=xpos-tempx;
                                System.out.println("XPOS EQ = "+xpos+" - "+tempx+" = "+ tempw);
                                foundw = true;
                                //System.out.println(tempw+"<<TEMPw TEMPx>>"+tempx+"<<<<XPOS>>>>>>>>>>>>>"+xpos);
                            }
                        if(startline==true && pictureRGB[xpos][ypos+1]!=pictureRGB[xpos][ypos] && foundh==false && foundw==true && boxes[boxcounter-1].rect.contains(new Point(xpos,ypos))==false && pictureRGB[xpos-1][ypos]==pictureRGB[xpos][ypos] && pictureRGB[xpos+1][ypos]!=pictureRGB[xpos][ypos] && isEqual(new Rectangle(xpos-2,ypos-2,4,4))==false)
                            {
                                temph=ypos-tempy;
                                System.out.println("YPOS EQ = "+ypos+" - "+tempy+" = "+ temph);

                                foundh=true;
                                System.out.println("The Width is: "+tempw+" The height is: "+temph+" boxcounter="+boxcounter);
                                boxes[boxcounter]= new Box(tempx, tempy, tempw, temph);
                                System.out.println("--------------------------------NEXT BOX ---------------");
                                //System.out.println("BOX WIDTH"+boxes[boxcounter].width + "BOX COUNTER=="+boxcounter);

                            }

                    }
                    if(boxcounter==0)
                    {
                        if(pictureRGB[xpos][ypos]==-3584 && startline==false)
                            {
                                System.out.println("The top left corner of the box is: ("+xpos+","+ypos+")");
                                startline=true;
                                tempx=xpos;
                                tempy=ypos;
                            }
                        if(startline==true && pictureRGB[xpos+1][ypos]!=pictureRGB[xpos][ypos] && foundw==false && foundh==false)
                            {
                                tempw=xpos-tempx;
                                foundw = true;
                            }
                        if(startline==true && pictureRGB[xpos][ypos+1]!=pictureRGB[xpos][ypos] && foundh==false && foundw==true)
                            {
                                temph=ypos-tempy;
                                foundh=true;
                                System.out.println("The Width is: "+tempw+" The height is: "+temph+" boxcounter="+boxcounter);
                                boxes[boxcounter]= new Box(tempx, tempy, tempw, temph);
                                //System.out.println("BOX WIDTH"+boxes[boxcounter].width);

                            }
                    }



                }
            }
            if(boxcounter>=(totalboxes))
                    {
                        donean=true;
                    }
        }


        //System.out.println("The black lines width is: "+(lines[linecounter].endx-lines[linecounter].startx));


    }
    }

    public boolean isEqual(Rectangle c1)
    {
        boolean returned = false;
        for(int i = 0; i<boxcounter; i++)
        {
            if(c1.intersects(boxes[i].rect) || boxes[i].rect.contains(new Point(c1.x, c1.y)))
            {
                returned=true;
            }

        }
        return(returned);

    }
/*
    public static BufferedImage toBufferedImage(Image img)
    {
        if (img instanceof BufferedImage)
        {
            return (BufferedImage) img;
        }

        // Create a buffered image with transparency
        BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);

        // Draw the image on to the buffered image
        Graphics2D bGr = bimage.createGraphics();
        bGr.drawImage(img, 0, 0, null);
        bGr.dispose();

        // Return the buffered image
        return bimage;
    }
*/
    public void move()
        {

        }

    //Other methods
    //You can define what this type of object can do here.








}

my line and box classes are very straight forward just some classes i made with xposes and yposes, width's and heights

jaronoff97
  • 366
  • 3
  • 15
  • 1
    To start with, you're not using a `JFrame`, you're using an `Applet`, massive difference. To make life easier, start by using a `JPanel`, then it won't matter where you want to output it to later. Take a look at [Creating a GUI With JFC/Swing](http://docs.oracle.com/javase/tutorial/uiswing/) and [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) for more details – MadProgrammer May 15 '14 at 13:11
  • Calling `setSize(x, y)` from within `paint(g)` is *never* a good idea, because it will lead to a property change event, issuing a `repaint()` and again a new `paint(g)` invocation (and repeat, could lead to `StackOverflowException`, although probably not as you always set the same values). – Harald K May 16 '14 at 14:28
  • Some more general advice for StackOverflow: Don't post your entire project code, with loads of lines. Most people won't bother to read that much code to help you. Instead, be specific! Create a minimal program or code snippet that illustrates the problem you are tying to solve (this might be difficult, but in many cases, you also find the solution to your initial problem by doing so). When posting code: Indent it properly and follow normal code style guidelines. Good luck! :-) – Harald K May 16 '14 at 14:33

0 Answers0