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

2012/4/1

Little Problem with drawing ...

arialdancer arialdancer

2012/4/1

#
Hi I am really beginner and have no idea why if i have 2 drawings object in act method in actor class world class shows just one ? Is world class shows just one GreenfootImage object in the same time ? Smb help me pls. Below i show code my world and actor object class. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; public void act() { drawFiveLine(); drawNote(); } /** * Draw five line on the screen */ public void drawFiveLine(){ GreenfootImage img = new GreenfootImage(width, high); img.setColor(Color.WHITE); img.fill(); img.setColor(Color.BLACK); img.drawLine(0, 25, 475, 25); img.drawLine(0, 35, 475, 35); img.drawLine(0, 45, 475, 45); img.drawLine(0, 55, 475, 55); img.drawLine(0, 65, 475, 65); setImage(img); } /** * Draw Note */ public void drawNote(){ GreenfootImage note = new GreenfootImage(width,high); note.setColor(Color.BLACK); note.fillOval(35,80,10,10); setImage(note); } } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; public class Piano extends World { /** * Constructor for objects of class Piano. * */ public Piano() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(720, 480, 1); prepare(); } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { FiveLine fiveLine = new FiveLine(); addObject(fiveLine, 285,100); } }
danpost danpost

2012/4/1

#
From the code you have given, it appears that all you see is the note. You can only set one image to each object (but different objects of the same class can have different images). You are creating the fiveLine image and setting it to the object, then you are creating the note image and now setting it to the object (which replaces your fiveLine image). I do not see where you 'addObject' the note object into the world. From your act() method (drawFiveLine(); drawNote();), it appears that you are trying to create two different objects with the same object class. If this is so, you need to create a new sub-class of Actor called 'Note' and move the code pertaining to it there; then add to your prepare method
Note note = new Note();
addObject(note, x, y); // x and y should be replaced with values where you want the note
If the five lines are to be on the screen at all times, and never move, you might consider just drawing them on the background of the world instead of having a seperate sub-class of Actor for it.
You need to login to post a reply.