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

2012/5/6

Need a little help with java.awt.Graphics

1
2
darkmist255 darkmist255

2012/5/6

#
I don't want to be a pain since this isn't too big of a problem, but can anyone recommend a scenario that uses java.awt.Graphics that has the source code published? I want to take a look at some code examples of the package but can't seem to find any on Google. Thanks :D! I'll be taking a look through the gallery tomorrow but I've got to get some sleep!
Builderboy2005 Builderboy2005

2012/5/6

#
My scenario Nightmare uses Graphics and Graphics2D extensively to achieve the shadowy effects, so you could check that out if you like! I assume you have also taken a look at the API? Do you have anything specific that you were looking for?
darkmist255 darkmist255

2012/5/6

#
Well for now I'm just trying to use lines in a game but I'll eventually want to get into more advanced stuff. I'll take a good read through Nightmare, that has tons of shapes.
darkmist255 darkmist255

2012/5/6

#
Reading through code but I'll just explain my main confusion: How does one create a new line or polygon? How does one then modify that shape? How does one then destroy or remove that shape? I'm looking through the API but something's not clicking into place in my head.
Builderboy2005 Builderboy2005

2012/5/7

#
You don't 'create' shapes as much. Think about it like a canvas, you can paint onto it, but not erase. You can use the draw(shape) method (in the graphics2D class) to draw onto the image with many different shapes including lines and polygons, but removing or modifying the shape directly is not the way the API works. If you have drawn a square onto the image, but then want to modify or change the square, you will need to find a way to 'erase' the square, and then redraw it again with the proper modifications. There are a couple ways to 'erase' changes made to the image. 1 is to simply redraw the entire image each frame. This is simple, but it can take lot of time if you need to draw a lot of things. Another way is to save a copy of the image before you draw to it, that way you can restore the image back to the way it was.
darkmist255 darkmist255

2012/5/7

#
How would I get a line onto the screen then? I'm a bit confused by the canvas concept. Would you be able to give just a quick example of how to draw a line? I should be able to figure it out once I know what works.
Builderboy2005 Builderboy2005

2012/5/7

#
If you are just wanting to draw a line onto an image, you can use the built-in drawLine() method in the Graphics class. If our input is a Graphics object graph, and the coordinates for a line, the code would look like: graph.drawLine(x1,y1,x2,y2); Also it might be worthy to note that Graphics2D is a subclass of the Graphics class, and so anything that you can do to a Graphics object can also be done to a Graphics2D object.
darkmist255 darkmist255

2012/5/7

#
Would one have to do "Graphics graph = new Graphics();" before you could do graph.drawLine? Would that be how we determine the object to draw on?
Builderboy2005 Builderboy2005

2012/5/7

#
Graphics objects are used to draw onto other Image based objects. Creating a graphics objects by calling 'new Graphics()' is pretty meaningless, because the Graphics object is not an image itself. A more meaningful way to create a Graphics object is if you have, say, a BufferedImage. BufferedImage is a type of Image class, and by itself it doesn't have any way to alter the image. However, it does have a method called CreateGraphics(), which returns a Graphics2D object that you can use to draw onto that specific BufferedImage. So for instance, if you had a BufferedImage image, and you wanted to draw something onto it, the code would look a little something like:
Graphics2D graphics = image.createGraphics();
graphics.drawLine(blah blah);
darkmist255 darkmist255

2012/5/12

#
I'm making some good progress now, I read over Nightmare and a few other scenarios (especially image painting type scenarios) and I'm understanding MUCH better now! Thanks for your help :D!
darkmist255 darkmist255

2012/5/12

#
Okay so here's a good question. I'll show you the code for this simple method before I tell you what it's doing.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Graphics;
import java.awt.Color;

public class NormalShot extends awtBased
{
    GreenfootImage canvas;
    int anchorX, anchorY, topX, topY;
    
    int shotMoveSpeed = 1;
    
    public NormalShot(int anchorXtemp, int anchorYtemp)
    {
        anchorX = anchorXtemp;
        anchorY = anchorYtemp;
        topX = anchorX;
        topY = anchorY;
    }

    public void addedToWorldExtended() //called by superclass from addedToWorld()
    {
        setLocation(getWorld().getWidth() / 2, getWorld().getHeight() / 2);
        canvas = new GreenfootImage(getWorld().getWidth(), getWorld().getHeight());
        canvas.setColor(Color.BLACK);
        setImage(canvas);
    }
    
    public void actExtended()  //called by superclass from act()
    {
        drawShot();
    }
    
    public void drawShot()
    {
        topY = topY - shotMoveSpeed;
        canvas.drawLine(anchorX, anchorY, topX, topY);
    }
}
Simple enough right, it works as it's supposed to; a line that extends upwards over time? Well for every instance of NormalShot() that I put into the world, my CPU usage for Java goes up by about 10%... Shouldn't
        topY = topY - 1;
        canvas.drawLine(anchorX, anchorY, topX, topY);
not be CPU stressing in the slightest?
Builderboy2005 Builderboy2005

2012/5/13

#
Each time you add a new object, that creates a new image which is just as large as the world. If you have a world that is particularly large, that is a lot of extra memory that is added. A better way might be to draw directly to the background of the world itself, instead of having a different image for each line
darkmist255 darkmist255

2012/5/13

#
Here's the result of attempt #1: I attempted doing this: "canvas = getWorld().getBackground();" The CPU usage still jumps to 100% if I fire several shots, just this time the world's background comes to the forefront, hiding everything behind it. I'm sure I could change this with setPaintOrder() but the CPU usage still isn't changed. I'll keep thinking for attempt #2.
danpost danpost

2012/5/13

#
I did not think the world background could be anywhere but in the BACK, behind all the objects!
darkmist255 darkmist255

2012/5/13

#
Same here! I was surprised when all of the actors suddenly disappeared, but I could still move around because when I would shoot it would come from a different location. Also, in an attempt to reduce CPU usage I think I might just keep a little variable so that it will only update the image once every five act cycles, see how that helps.
There are more replies on the next page.
1
2