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

2020/3/23

Need help

Handgottes Handgottes

2020/3/23

#
So right now I am trying to program something like paint with greenfoot. I try to use the methods from the Api GreenfootImage. Now i got a problem which i don´t really understand
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
27
28
29
30
31
32
33
34
35
36
37
38
39
import greenfoot.*;
import java.lang.Object;
import greenfoot.GreenfootImage;
import java.util.Scanner;
import java.*;
/**
 * Write a description of class Freihandzeichnen here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Stift 
{   int[] intXAchse = new int[100];
    int[] intYAchse = new int[100];
    /**
     * Constructor for objects of class Freihandzeichnen
     */
    public Stift()
    {
        {
 
        }
    }
 
    public void Rechteck()
    {
        Scanner input = new Scanner(System.in);
        System.out.print("Nenne die Länge für X:");
        int XAchse = input.nextInt();
        intXAchse[0] = XAchse;
        System.out.print("Nenne die Länge für Y:");
        int YAchse = input.nextInt();
        intYAchse[0] = YAchse;
        System.out.print("Nenne die Anzahl an Ecken:");
        int pR = input.nextInt();
        GreenfootImage.drawPolygon(intXAchse,intYAchse,pR);
    }
 
}
now ".drawPolygon" is showing an Error "non-static method drawPolygon(...) cannot be refernded from a static context. Anyone who can help me fix it?
Super_Hippo Super_Hippo

2020/3/23

#
GreenfootImage is a class. You need to create an object (or get a reference) first.
RcCookie RcCookie

2020/3/23

#
Hippo is right try something like this:
1
2
3
GreenfootImage temp = getWorld().getBackground(); //gets the image displayed right now
temp.drawPolygon(intXAchse, intYAchse, pR); //Draws the polygon onto the image temp with the given starting location
getWorld().setBackground(temp); //Sets the worlds background to the edited image
For this to work you will need to be the Stift class be an extension of Actor. Or World, in that case you have to remove the getWorld() parts. You may want to use the method fillPolygon rather than drawPolygon as it would only draw the outline. Also, no matter which one you use, they require an array of integers, because it is not the length but all of the coordinates of each corner point. Try something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
System.out.println(“Nenne die Anzahl an Eckpunkten:”);
int corners = input.nextInt();
intXAchse = new int[corners];
intYAchse = new int[corners];
for(int i=0; I<corners; i++){
    System.out.println(„Gib die X-Koordinate von Ecke „+i+„ an:“);
    intXAchse[i] = input.nextInt();
    System.out.println(„Gib die Y-Koordinate von Ecke „+i+„ an:“);
    intYAchse[i] = input.nextInt();
}
GreenfootImage temp = getWorld().getBackground();
temp.fillPolygon(intXAchse, intYAchse, corners);
getWorld().setBackground(temp);
Handgottes Handgottes

2020/3/24

#
thank you very much, I will try this
You need to login to post a reply.