Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Drawable is most likely an <a href="http://docs.oracle.com/javase/tutorial/java/concepts/interface.html" rel="nofollow">interface</a> the tutorial you are following implements to allow for polymorphism, for keeping all the drawables in one collection for a rendererclass to easily access and render (like you are trying to do here).</p> <p>Try looking at the tutorial you are following, most likely the interface is declared there, if not you can declare it yourself. Here is a very simple example implementing an interface like the one you intend to:</p> <pre><code>public static interface Drawable { public void draw(Graphics2D g); } public static class BlueRectangle implements Drawable { private Rectangle rectangle; public BlueRectangle(int x, int y, int width, int height) { this.rectangle = new Rectangle(x, y, width, height); } @Override public void draw(Graphics2D g) { Color gColor = g.getColor(); g.setColor(Color.blue); g.fill(rectangle); g.setColor(gColor); } } public static class RedCircle implements Drawable { private Ellipse2D.Double circle; public RedCircle(double x, double y, double width, double height) { this.circle = new Ellipse2D.Double(x, y, width, height); } @Override public void draw(Graphics2D g) { Color gColor = g.getColor(); g.setColor(Color.red); g.fill(circle); g.setColor(gColor); } } // This allows you to for instance create collections of Drawables for // rendering and a renderer class that doesn't care how each specific class // implements rendering, it just uses its graphics object to render them public static class Renderer extends JPanel { List&lt;Drawable&gt; drawables; private Random random = new Random(); public Renderer() { this.drawables = new ArrayList&lt;&gt;(); setUpDrawAbles(); setBackground(Color.black); } @Override @Transient public Dimension getPreferredSize() { return new Dimension(1000, 1000); } private void setUpDrawAbles() { // Add some circles for (int i = 0; i &lt; 5; i++) { Drawable circle = new RedCircle(random.nextDouble() * 1000, random.nextDouble() * 1000, random.nextInt(200), random.nextInt(200)); drawables.add(circle); } // Add some rectangles; for (int i = 0; i &lt; 5; i++) { Drawable rectangle = new BlueRectangle(random.nextInt(1000), random.nextInt(1000), random.nextInt(200), random.nextInt(200)); drawables.add(rectangle); } } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); // This class doesn't know what a BlueRectangle or a RedCircle is, // it just tells them // "here is the graphicsobject to my canvas, use it to draw yourself" for (Drawable drawable : drawables) { drawable.draw(g2d); } } } public static void main(String[] args) { JFrame frame = new JFrame(); Renderer render = new Renderer(); frame.getContentPane().add(render); frame.pack(); frame.setVisible(true); } } </code></pre> <p><strong>Edit:</strong> Yes, it is true you may not need it at all since you only have one object (a gladiator) that needs rendering but if in the future you decide to implement alot of visual objects having a common interface for rendering will make life easier for you and addition of content much much easier and faster, it reduces interdependancy between the gamemanagers alot when they can communicate over a contract (the interface) that tells how stuff should behave and not how stuff is built.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload