Class Rect extends from class FillableShape
and FillableShape extends from class Shape.
Class Shape and FillableShape are two abstract classes.
I have to define the method Constrain in the subclass (class Rect) one more time. So the method Constrain can be more specified.
The problem is that when I define the Contrain method in the class Rect, the compiler show the following text:
The field Shape.box is not visible
The field Shape.box is not visible
The field Shape.box is not visible
The field Shape.box is not visible
The field Shape.x is not visible
The field Shape.dx is not visible
..... and much more, simliar like above.
As I have learned already, the subclasses inherit all the instance variabels from the superclass.
Why it's doesn't work here?
import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; abstract public class Shape { private double x, y; // Postion private double dx, dy; // Hastighet private Color color; private Rectangle box; // "Lådan", ytan figuren kan röra sig inuti. public Shape(double x, double y, Color color) { this.x = x; this.y = y; this.color = color; } public double getX() { return x; } public double getY() { return y; } public double getDX() { return dx; } public double getDY() { return dy; } public Color getColor() { return color; } public Rectangle getBox() { return box; } public void setVelocity(double dx, double dy) { this.dx = dx; this.dy = dy; } /** Förflyttar figuren ett steg (dx, dy). */ public void move() { x += dx; y += dy; constrain(); // Håll figuren inom lådan, se nedan } public void setBoundingBox(Rectangle box) { this.box = box; } /** Håller figuren inom lådan */ public void constrain() { // Hämta lådans mått (box.x, box.y) double x0 = box.x, y0 = box.y; double x1 = x0 + box.width; double y1 = y0 + box.height; // Om utanför lådan - byt riktning if(x < x0) dx = Math.abs(dx); if(x > x1) dx = -Math.abs(dx); if(y < y0) dy = Math.abs(dy); if(y > y1) dy = -Math.abs(dy); } /** Ritar figuren, abstrakt metod */ abstract public void paint(Graphics g); }
import java.awt.Color; abstract public class FillableShape extends Shape { private boolean filled; public FillableShape(double x, double y, Color color) { super(x, y, color); } public void setFilled(boolean v) { filled = v; } public boolean getFilled() { return filled; } public void constrain() { // Hämta lådans mått (box.x, box.y) double x0 = box.x, y0 = box.y; double x1 = x0 + box.width; double y1 = y0 + box.height; // Om utanför lådan - byt riktning if(x < x0) dx = Math.abs(dx); if(x > x1) dx = -Math.abs(dx); if(y < y0) dy = Math.abs(dy); if(y > y1) dy = -Math.abs(dy); } }
import java.awt.Color; import java.awt.Graphics; public class Rect extends FillableShape { private double x2; private double y2; public Rect(double x, double y, double x2, double y2, Color color) { super(x, y, color); this.x2 = x2; this.y2 = y2; } public double getX2() { return x2; } public double getY2() { return y2; } public void constrain() { // Hämta lådans mått (box.x, box.y) double x0 = box.x, y0 = box.y; double x1 = x0 + box.width - x2; double y1 = y0 + box.height - y2; // Om utanför lådan - byt riktning if(x < x0) dx = Math.abs(dx); if(x > x1) dx = -Math.abs(dx); if(y < y0) dy = Math.abs(dy); if(y > y1) dy = -Math.abs(dy); } public void paint(Graphics g) { /* if(filled == true) { g.fillOval( (int)this.getX(), (int)this.getY(), (int)this.getX2(), (int)this.getY2()); } */ g.setColor(this.getColor()); g.drawRect( (int)this.getX(), (int)this.getY(), (int)x2, (int)y2); } }
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.Random; public class BouncePanel extends JPanel implements Runnable { private int width = 400, height = 300; private Thread thread; // En array av referenser till ritbara objekt. private Shape[] shapes; Rectangle box = new Rectangle(width, height); private boolean filled; public BouncePanel() { // Skapa ritbara figurer och initiera dessa // . . . // . . . shapes = new Shape[3]; // Create a new Line shapes[0] = new Line(0.0, 0.0, 2.0, 3.9, Color.blue); shapes[0].setBoundingBox(box); shapes[0].setVelocity(1.2, 2.5); // Create a new Circle shapes[1] = new Circle(0.0, 0.0, 10.0, Color.red); shapes[1].setBoundingBox(box); shapes[1].setVelocity(5.2, 1.5); // Create a new Rect shapes[2] = new Rect(0.0, 0.0, 20.0, 30.0, Color.gray); shapes[2].setBoundingBox(box); shapes[2].setVelocity(2.3, 2.6); // Sätter ritytans (BouncePanel) storlek. setPreferredSize(new Dimension(width, height)); // Skapar ett Thread-objekt, som exekverar metoden run som // en separat aktivitet, tråd. thread = new Thread(this); thread.start(); } /** Anropas av repaint(). Definierar vad som ska ritas. */ public void paintComponent(Graphics g) { super.paintComponent(g); // Rita alla figurer // . . . // . . . shapes[0].paint(g); shapes[1].paint(g); shapes[2].paint(g); } /** Uppdaterar bollens position och ritar upp den. * Denna metod exekveras av tråden thread. */ public void run() { while(true) { // Flytta alla figurer // . . . . // . . . . shapes[0].move(); shapes[1].move(); shapes[2].move(); repaint(); // Anropar paintComponent(Graphics g) // Vila i 20 ms innan nästa uppritande try { Thread.sleep(20); } catch(InterruptedException e) {} } } }
import javax.swing.*; /** Denna klass innehåller main där ett fönster (frame) för * applikationen skapas. */ public class Bounce { public static void main(String[] args) { // Skapa ett fristående fönster för programmet. JFrame frame = new JFrame("Bounce"); // Skapa en rityta (BouncePanel) med en boll och // placera denna i fönstret. BouncePanel panel = new BouncePanel(); frame.getContentPane().add(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }