Domanda

I'm trying to make a program that shows inheritance and polymorphism. The program is supposed to show a flag (specifically, the flag of Guinea).

This is the code:

import java.awt.*;
import javax.swing.*;

public class FlagB extends JPanel
{
    public void paint (Graphics g)
    {
        Flag Guinea = new Flag(50,290,560);
        Guinea.drawFlag(g);
    }

    public static void main(String[] args)
    {
        JFrame frame = new JFrame("Bolivian Flag");
        frame.add(new FlagB());
        frame.setSize(1000,725);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

class Flag
{
    private FlagBase fb;
    private FitRect rp; //rp for rectangle parameters
    private LeftTriColor lt;
    private MiddleTriColor mt;
    private RightTriColor rt;
    private int x;
    private int y;
    private int z;


    public Flag(int x,int y,int z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
        fb = new FlagBase();
        rp = new FitRect(x,y,z);
        lt = new LeftTriColor(x,y,z);
        mt = new MiddleTriColor(x,y,z);
        rt = new RightTriColor(x,y,z);
    }

    public void drawFlag(Graphics g)
    {
        fb.FlagBase(g);
        lt.drawRect(g);
        mt.drawRect(g);
        rt.drawRect(g);
    }
}

class FlagBase // The outline of the flag
{
    public void FlagBase(Graphics g)
    {
        g.setColor(Color.BLACK);
        g.drawRect(49,49,876,562);
    }
}

class FitRect
{
    protected int l;
    protected int length;
    protected int width;
    public FitRect(int x,int y,int z)
    {
        x = l;
        y = length;
        z = width;
    }
}

class LeftTriColor extends FitRect
{
    public LeftTriColor(int x,int y,int z)
    {
        super(x,y,z);
    }

    public void drawRect(Graphics g)
    {
        g.setColor(Color.RED);
        g.fillRect(l,l,length,width);
    }
}

class MiddleTriColor extends FitRect
{
    public MiddleTriColor(int x,int y,int z)
    {
        super(x,y,z);
    }

    public void drawRect(Graphics g)
    {
        g.setColor(Color.YELLOW);
        g.fillRect(l+length+1,l,length,width);
    }
}

class RightTriColor extends FitRect
{
    public RightTriColor(int x,int y,int z)
    {
        super(x,y,z);
    }

    public void drawRect(Graphics g)
    {
        g.setColor(Color.GREEN);
        g.fillRect(l+2*length+3,l,length+1,width);
    }
}

The only thing that shows up in the new window is the flag's outline. I think it has something to do with the argument passing of FitRect. Is that it, or is it something else? I've been using JCreator.

È stato utile?

Soluzione

This should solve it.

class FitRect
{
    protected int l;
    protected int length;
    protected int width;
    public FitRect(int x,int y,int z)
    {
        l = x;         //was x = l
        length = y;    //was y = length
        width = z;     //was z = width
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top