java - Questions on object declaring and a few image import statements for making a basic game -


so friend , making very basic game fun, going top down (about 45 degree angle) there lumberjack chopping down trees. basic , kinda lame i'm aware getting semi-advanced java.

to start decided needed sprites trees realized no matter rectangle white pixels around tree, cut out part of background image. wanted take every pixel white (whitespace/negative space), make pixels transparent. looked @ ton of codes , 1 saw code below, worked don't quite understand it.

import java.awt.*; import java.awt.image.*; import javax.swing.*;  public class simpleframe extends jframe {    jpanel mainpanel = new jpanel() {       imageicon originalicon = new imageicon("image.png");       imagefilter filter = new rgbimagefilter() {          int transparentcolor = color.white.getrgb() | 0xff000000;          public final int filterrgb(int x, int y, int rgb) {             if ((rgb | 0xff000000) == transparentcolor) {                return 0x00ffffff & rgb;             } else {                return rgb;             }          }       };       imageproducer filteredimgprod = new filteredimagesource(originalicon.getimage().getsource(), filter);       image transparentimg = toolkit.getdefaulttoolkit().createimage(filteredimgprod);       public void paintcomponent(graphics g) {          g.setcolor(getbackground());          g.fillrect(0, 0, getsize().width, getsize().height);          g.drawimage(transparentimg, 140, 10, this);       }    };    public simpleframe() {       super("transparency example");        jpanel content = (jpanel)getcontentpane();       mainpanel.setbackground(color.black);       content.add("center", mainpanel);    }    public static void main(string[] argv) {       swingutilities.invokelater(new runnable() {          public void run() {             simpleframe c = new simpleframe();             c.setdefaultcloseoperation(jframe.exit_on_close);             c.setsize(700,700);             c.setvisible(true);          }       });    } } 

so don't understand why there brackets after declaration of jpanel in 6th line have no idea how lines 6-16. in between brackets, close bracket has semi colon after it. can't figure out how works, every command line set aside structure of declaring jpanel, jpanel mainpanel = new jpanel() { };

im trying implement can import image jpanel class, make negative space transparent, paint it. cant seem understand structure of program , how implement class.

my code following:

public class frame extends jpanel   {     /*      * jpanel in want add transparent image jpanel      * add jpanel object,"frame", above in jframe declared in      * class above      */     public frame()  {         jpanel jp = new jpanel();         jp.setsize(300,300);         jp.setvisible(true);         circle = new bufferedimage();                    try {circle = imageio.read(new file("circle.png"));}         catch (ioexception ex) {}     }     public void paintcomponents(graphics g) {         super.paintcomponents(g);         g.drawimage(circle,0,0,300,300, null);         g.drawrect(50, 50, 50, 50);     }  } 

it test code import image, make transparent, , paint on jpanel. trying understand best way , code found (the first code block) seems job can't figure out best way implement our code.

thanks in advance,

robbie , nick

what code in example block jpanel doing declaring jpanel's class right there--immediately--when instantiated. it's sort of coding shorthand. doing that, don't have write formal new, named class jpanel in new .java file: entire definition of class right there. if @ java class definition, i'm sure make more sense.

if take stuff out , put in formal java class, inherited jpanel, should work. formal name they're doing called implementing anonymous class (http://docs.oracle.com/javase/tutorial/java/javaoo/anonymousclasses.html). don't it, because makes code harder read, it's permitted.

hth


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -