This site requires JavaScript, please enable it in your browser!
Greenfoot back
Andriyanto
Andriyanto wrote ...

2012/7/15

Inserting Images in Array??

Andriyanto Andriyanto

2012/7/15

#
this is my code : import greenfoot.*; import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics2D; import java.util.*; public class MateriRukunIman extends Materi { private static GreenfootImage title = new GreenfootImage("kelas1.png"); private Font font = new Font("Bradley Hand ITC TT", Font.BOLD, 20); private Font font2 = new Font("Courier New", Font.PLAIN, 17); private GreenfootImage back, back2; private int btnFadeA; private int btnFadeB; private int btnFadeC; private int state = 0; private static ArrayList<ArrayList<String>> info = new ArrayList<ArrayList<String>>() {{ add(new ArrayList<String>() {{ add("Bla bla bla"); add("dsfds"); add("fsdfds"); add("dfssd"); add("fdsfds"); add(""); add(""); }}); add(new ArrayList<String>() {{ add("Bla bla bla"); add("dsfds"); add("fsdfds"); add("dfssd"); add("fdsfds"); add(""); add(""); }}); }}; public MateriRukunIman() { title.setTransparency(200); back = new GreenfootImage(300, 200); back.fill(); back.setTransparency(0); setImage(back); } public void act() { if (getImage().getWidth() < 640) { double d = Math.sqrt(640 - getImage().getWidth()) / (640 - getImage().getWidth()); getImage().scale((int)(getImage().getWidth() + Math.sqrt(640 - getImage().getWidth())), (int)(getImage().getHeight() + d * (480 - getImage().getHeight()))); getImage().setTransparency((int)(getImage().getWidth() / 640d * 190)); if (getImage().getWidth() >= 640) { btnFadeA = getImage().getTransparency(); btnFadeB = btnFadeA; back = new GreenfootImage(640, 480); back.setColor(new Color(0, 0, 0, btnFadeA)); back.fillRect(0, 0, 640, 480); back.drawImage(title, (600 - title.getWidth()) / 2, 20); back2 = new GreenfootImage(640, 480); back2.setColor(new Color(0, 0, 0, btnFadeA)); back2.fillRect(0, 0, 640, 480); back2.drawImage(title, (600 - title.getWidth()) / 2, 20); } } else if (getImage().getTransparency() < 175) { getImage().setTransparency(getImage().getTransparency() - 20); if (getImage().getTransparency() < 20) { getWorld().removeObject(this); } } else { GreenfootImage pic; if (state == 0) { pic = new GreenfootImage(back); boolean a = false; boolean b = false; if (myWorld.lastMouse.getX() > 250 && myWorld.lastMouse.getX() < 350 && myWorld.lastMouse.getY() > 340 && myWorld.lastMouse.getY() < 380) a = true; else if (myWorld.lastMouse.getX() > 230 && myWorld.lastMouse.getX() < 350 && myWorld.lastMouse.getY() > 160 && myWorld.lastMouse.getY() < 240) b = true; if (a) { if (btnFadeA > 100) btnFadeA -= 5; } else if (btnFadeA < 190) btnFadeA += 5; if (b) { if (btnFadeB > 100) btnFadeB -= 5; } else if (btnFadeB < 190) btnFadeB += 5; pic.setColor(new Color(0, 0, 0, btnFadeA)); pic.fillRect(250, 340, 100, 40); pic.setColor(new Color(0, 0, 0, btnFadeB)); pic.fillRect(230, 200, 130, 40); pic.setColor(Color.WHITE); pic.setFont(font); Graphics2D graphics = pic.getAwtImage().createGraphics(); graphics.setFont(font); FontMetrics fm = graphics.getFontMetrics(); int width = fm.charsWidth(("Tutup").toCharArray(), 0, ("Tutup").length()); pic.drawString("Tutup", 300 - width / 2, 365); width = fm.charsWidth(("Rukun Iman").toCharArray(), 0, ("Rukun Iman").length()); pic.drawString("Rukun Iman", 295 - width / 2, 225); graphics.dispose(); if (Greenfoot.mousePressed(null)) { if (a) pic.setTransparency(170); else if (b) { state = 1; btnFadeC = 190; } } } else { pic = new GreenfootImage(back2); boolean a = myWorld.lastMouse.getX() > 250 && myWorld.lastMouse.getX() < 350 && myWorld.lastMouse.getY() > 340 && myWorld.lastMouse.getY() < 380; if (a) { if (btnFadeC > 100) btnFadeC -= 5; } else if (btnFadeC < 190) btnFadeC += 5; pic.setColor(new Color(0, 0, 0, btnFadeC)); pic.fillRect(250, 340, 100, 40); pic.setColor(Color.WHITE); pic.setFont(font); Graphics2D graphics = pic.getAwtImage().createGraphics(); graphics.setFont(font); FontMetrics fm = graphics.getFontMetrics(); int width = fm.charsWidth(("Next").toCharArray(), 0, ("Next").length()); pic.drawString("Next", 300 - width / 2, 365); pic.setFont(font2); graphics.setFont(font2); fm = graphics.getFontMetrics(); for (int i = 0; i < info.get(state - 1).size(); i++) { String s = info.get(state - 1).get(i); width = fm.charsWidth(s.toCharArray(), 0, s.length()); pic.drawString(s, 300 - width / 2, 200 + 19 * i); } graphics.dispose(); if (Greenfoot.mousePressed(null)) { if (a && ++state == info.size() + 1) { state = 0; btnFadeA = 190; btnFadeB = 190; } } } setImage(pic); } } } How change a text (in array) with image? or Inserting Images in Array??
SPower SPower

2012/7/15

#
Very clear which part we should look at in your code. And next time, learn some better English :) To change something in an array, do this:
1
arrayName[index] = some object;
Where index is an integer, and some object explains itself. To create an array which can hold images, use this:
1
GreenfootImage[] imageArray = new GreenfootImage[theMaxNumberOfObjects];
where theMaxNumberOfObject is an int.
Andriyanto Andriyanto

2012/7/15

#
Sorry my English not good.. :) private static GreenfootImage imageArray = new GreenfootImage where I put or call the image file?? {{ add(new ArrayList<String>() {{ add("how????"); add(" how???"); }}); }}; can you give me example?
SPower SPower

2012/7/15

#
doing it with a static variable, you should do this:
1
private static GreenfootImage[] imageArray = {all the images, sorted by commas};
And next time, click on the code button when you want to insert code into a comment; it's right underneath the text field.
Andriyanto Andriyanto

2012/7/15

#
ok thanks.. oh yes..thank you.. :D
Andriyanto Andriyanto

2012/7/15

#
error incompatible types - found java.lang.String but expected greenfoot.GreenfootImage
SPower SPower

2012/7/15

#
You're trying to put a string into an array for GreenfootImages, that won't work. You have to create another array for strings.
Andriyanto Andriyanto

2012/7/15

#
i dont understand..still error..please give me complete example..
SPower SPower

2012/7/15

#
I'll explain arrays to you: Arrays can only hold one type of object. Example:
1
GreenfootImage[] anArray;
the anArray array can only hold GreenfootImages and subclasses of GreenfootImage. By doing something like this:
1
anArray = new GreenfootImage[10];
You create an array, and put that into the anArray which is already declared. Now, you can put some images into it:
1
2
3
anArray[0] = new GreenfootImage(10,10);
anArray[1] = new GreenfootImage(20,20);
// etc.
But when you do this:
1
anArray[10] = something;
your program crashes: the array indexes start at 0, so this is out of bounds. When you already know what to put inside the array, you can also do this:
1
2
3
4
GreenfootImage[] anotherArray = {
new GreenfootImage(10,10);
new GreenfootImage(20,20);
};
This will create an array called anotherArray, and it puts 2 GreenfootImages inside it. Note that this array can only hold 2 GreenfootImages (you gave 2 at creation), and you can only use the {} at creation, so this won't compile:
1
2
3
4
GreenfootImage[] someArray;
someArray = {
new GreenfootImage(10,10);
}
but this will:
1
2
3
GreenfootImage[] someArray = {
new GreenfootImage(10,10);
}
You also can't do this:
1
anArray[3] = "A string";
because the anArray array can only hold images, and a String IS NO GREENFOOTIMAGE, and no subclass either! I hope you understand arrays better right now...
Andriyanto Andriyanto

2012/7/31

#
can you help me..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
if (getImage().getWidth() < 640)
{
    double d = Math.sqrt(640 - getImage().getWidth()) / (640 - getImage().getWidth());
    getImage().scale((int)(getImage().getWidth() + Math.sqrt(640 - getImage().getWidth())), (int)(getImage().getHeight() + d * (480 - getImage().getHeight())));
    getImage().setTransparency((int)(getImage().getWidth() / 640d * 190));
    if (getImage().getWidth() >= 640)
    {
        btnFadeA = getImage().getTransparency();
        btnFadeB = btnFadeA;
         
        back = new GreenfootImage(640, 480);
        back.setColor(new Color(0, 0, 0, btnFadeA));
        back.fillRect(0, 0, 640, 480);
 
        back.drawImage(title, (600 - title.getWidth()) / 2, 20);
         
        back2 = new GreenfootImage(640, 480);
        back2.setColor(new Color(0, 0, 0, btnFadeA));
        back2.fillRect(0, 0, 640, 480);
 
        back2.drawImage(title, (600 - title.getWidth()) / 2, 20);
    
        back2.drawImage(title2, (300- title.getWidth()) / 2, 240);
        
    }
}
i put
1
back2.drawImage(title2, (300- title.getWidth()) / 2, 240);
in another state if mouse clicked i want remove back2.drawImage(title2, (300- title.getWidth()) / 2, 240); and change with
1
back2.drawImage(title3, (300- title.getWidth()) / 2, 240);
you know how to remove back2.drawImage?
SPower SPower

2012/7/31

#
I would say, create an instance variable:
1
private GreenfootImage[] backImages = new GreenfootImage[2];
in your constructor:
1
2
images[0] = *the original back2 image*;
images[1] = new GreenfootImage(images[0].getWidth(), images[0].getHeight());
then, replace this:
1
back2.drawImage(title2, (300- title.getWidth()) / 2, 240);
by this:
1
backImages[1].drawImage(title2, (300- title.getWidth()) / 2, 240);
Now, you're drawing onto a layer. If you would normally draw the back2 image, you now have to draw the 2 images from the array I gave you. With the layer functionality, you can replace this:
1
back2.drawImage(title3, (300- title.getWidth()) / 2, 240);
by this:
1
2
backImages[1].clear();
backImages[1].drawImage(title3, (300- title.getWidth()) / 2, 240);
and then redraw the 2 images from the array again.
You need to login to post a reply.