Accessing a graphics rendering method from another class in Java

I created a class called SimpleCanvaswhich is a extendsclass JPanel, and I would like to map some graphic objects to an object SimpleCanvas. I also created another class called Rectangle, and I would like to make a simple object Rectangleon an object SimpleCanvasnamed mainCanvas. However, my problem is that I cannot figure out how to access the method of rendermy class Rectangleand allow it to be called from my class SimpleCanvaswhen the event redrawfor is mainCanvascalled.

Here is my code:

Class SimpleCanvas:

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);
        }
    }
}

Class SimpleWindow:

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);
    }
}

, mainCanvas .

!

+3
1

, , -, . List<Node> List<Edge> .

+3

All Articles