Java 在非jframe上下文中绘制BuffereImage

Java 在非jframe上下文中绘制BuffereImage,java,bufferedimage,getpixel,Java,Bufferedimage,Getpixel,好的,我的朋友和我正在制作一个需要对矩形进行像素分析的游戏,我编写了分析矩形的代码,它工作得很顺利,但是,当我尝试绘制BuffereImage时,它没有显示出来。主要问题是,我的java类从未教过我们如何使用Jframes,我不想更改代码以适应: // Threaded Applet Template that works with Macs // Import section // Use this section to add additional libaries fo

好的,我的朋友和我正在制作一个需要对矩形进行像素分析的游戏,我编写了分析矩形的代码,它工作得很顺利,但是,当我尝试绘制BuffereImage时,它没有显示出来。主要问题是,我的java类从未教过我们如何使用Jframes,我不想更改代码以适应:

        // 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()





}
//适用于Mac的线程化小程序模板
//进口部
//使用本节可添加额外的库,以便在程序中使用。
导入java.applet.*;
导入java.awt.*;
导入java.awt.event.*;
导入java.util.*;
导入java.awt.image.buffereImage;
导入java.awt.Toolkit;
导入javax.imageio.*;
导入javax.swing.*;
导入java.awt.image.*;
导入java.io.*;
//这将开始类定义。
//请注意,这是一个“世界”。您可以知道,因为它扩展了Applet。
//它还实现了Runnable,这允许它使用线程。
公共类PixelAnalyzetest扩展小程序实现可运行
{
//变量声明部分
//公共数据类型variablename
公共int xTitle;
公共交通工具;
公共分析仪;
公共缓冲区图像测试;
公众形象测试;
公共布尔值analyzeonce=true;
//在此声明您的英雄对象
//设置一个名为Thread的线程
螺纹;
//方法定义部分
//init()是小程序启动时运行的第一个方法
公共void init()
{
//初始化变量
xTitle=10;
yTitle=10;
BuffereImage测试=新的BuffereImage(12001200,BuffereImage.TYPE_INT_ARGB);
//test=getImage(getDocumentBase(),“test.png”);
System.out.println(“图像的宽度为:”+test.getWidth(this));
试一试{
test=ImageIO.read(新文件(“test.png”);
}捕获(IOE){};
//itest=test.getAsBufferedImage();
分析仪=新分析仪(测试,5);
//构造对象
//在这里建造你的英雄!
//设置线程
//这些应该是init()方法中的最后几行。
thread=newthread(this);//构造一个新线程
thread.start();//启动线程
}//init()
//paint()用于在屏幕上显示内容
公共空间涂料(图g)
{
设置大小(12001200);
//把标题放在屏幕上。
/*对于(int x=0;x=(totalbox))
{
donean=真;
}
}
//System.out.println(“黑色线条宽度为:”+(线条[linecounter].endx线条[linecounter].startx));
}
}
公共布尔相等(矩形c1)
{
返回的布尔值=false;

对于(int i=0;首先,你不是在使用
JFrame
,而是在使用
Applet
,这是一个巨大的差异。为了让生活更简单,从使用
JPanel
开始,那么你以后想把它输出到哪里就无关紧要了。请查看并获取更多详细信息,从
paint(g)中缩放
setSize(x,y)
从来都不是一个好主意,因为它会导致属性更改事件,发出
repaint()
并再次发出新的
paint(g)
调用(重复,可能会导致
StackOverflowException
,尽管可能不像您总是设置相同的值那样).关于StackOverflow的一些更一般的建议:不要用大量的行来发布整个项目代码。大多数人不会费心阅读那么多代码来帮助你。相反,要具体!创建一个最小的程序或代码片段来说明你要解决的问题(这可能很困难,但在许多情况下,您也可以通过这样做找到初始问题的解决方案)。发布代码时:正确缩进代码并遵循正常的代码样式准则。祝您好运!:-)
        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.








}