Kevin Gurney
Kevin Gurney

Reputation: 1471

Access Graphics Rendering Method from Another Class in Java

I have created a class called SimpleCanvas which extends the JPanel class, and I would like to render some graphical objects onto a SimpleCanvas object. I have also created another class called Rectangle, and I would like to render a simple Rectangle object onto a SimpleCanvas object named mainCanvas. However, my issue is that I can not figure out how to access the render method of my Rectangle class, and allow it to be called from within my SimpleCanvas class when a redraw event is called for the mainCanvas object.

Here is my code:

SimpleCanvas Class:

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

public class SimpleCanvas extends JPanel {
    private int x, y, width, height;
    private Vector objects = new Vector();

    public SimpleCanvas(int x, int y, int width, int height) {
        setPosition(x, y);
        setWidth(width);
        setHeight(height);
    }

    public void setPosition(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public void addObject(Object object) {
        this.objects.add(object);
    }

    public void paintComponent(Graphics g) {
        for (int i = 0; i < this.objects.size(); i++) {
            this.objects.get(i).render(g);
        }
    }
}

SimpleWindow Class:

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

public class SimpleWindow extends JFrame {
    private int x = 0;
    private int y = 0;
    private int width = 0;
    private int height = 0;
    private Color color = Color.WHITE;
    private String name = "DEFAULT_NAME";

    public SimpleWindow(int x, int y, int width, int height, Color color, String name) {
        this.name = name;
        setSize(width, height);
        setLocation(x, y);
        setBackground(color);
        setVisible(true);
    }
}

Rectangle Class:

import java.awt.*;

public class Rectangle {
    private int x = 0;
    private int y = 0;
    private int width = 0;
    private int height = 0;
    private Color color = Color.WHITE;
    public Rectangle(int x, int y, int width, int height, Color color) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.color = color;
    }
    public void render(Graphics g) {
        g.setColor(this.color);
        g.fillRect(this.x, this.y, this.width, this.height);
    }
}

Main Class:

import java.awt.*;

public class Main {
    public static void main(String[] args) {
        SimpleWindow mainWindow = new SimpleWindow(0, 0, 640, 360, Color.WHITE, "Simple Windowing System");
        SimpleCanvas mainCanvas = new SimpleCanvas(0, 0, 640, 360);
        Rectangle mainRectangle = new Rectangle(0, 0, 50, 50, Color.BLUE);
        mainCanvas.addObject(mainRectangle);
        mainWindow.setContentPane(mainCanvas);
    }
}

I feel as though I also might be approaching the problem of rendering graphics to the mainCanvas object the wrong way.

Thank you in advance for any help!

Upvotes: 3

Views: 2138

Answers (1)

trashgod
trashgod

Reputation: 205785

Define a model that represents the objects your canvas view can render, and let the view invoke the render method of each. The model need be nothing more than the List<Node> and List<Edge> seen in this example.

Upvotes: 3

Related Questions