简体   繁体   English

强制从另一个类重新粉刷-Swing

[英]Force repaint from another class - Swing

I basically have 1 class called ClueGame dedicated to painting the main gui using swing. 我基本上有1个名为ClueGame的课程,专门用于使用秋千绘画主要gui。

In this class, I iterate through a list of BoardCell objects, and call boardcell.draw(g) from within paintComponents(Graphics g), which draws rectangles to the board. 在此类中,我遍历BoardCell对象的列表,并从paintComponents(Graphics g)中调用boardcell.draw(g),后者将矩形绘制到板上。

However, in a different class entirely, the WalkwayCell class (a child of BoardCell), I need to update the color of specific rectangles. 但是,在完全不同的类WalkwayCell类(BoardCell的子类)中,我需要更新特定矩形的颜色。 I'm thinking something like cell.updateColor() 我在想像cell.updateColor()

But obviously I need to get draw to do that some how. 但是很明显,我需要抽签才能做到这一点。 I don't know how to update the color of one object (rectangle) on the board because I can't call draw because I don't have a graphics object. 我不知道如何更新板上一个对象(矩形)的颜色,因为我没有图形对象,因此无法调用绘图。

Hopefully that makes sense. 希望这是有道理的。 I can post code if someone requests it. 如果有人要求,我可以发布代码。

You have any number of options... 您有许多选择...

You Could 你可以

Pass a reference of your ClueGame to the instances of WalkwayCell which would then be capable of calling repaint on the ClueGame reference directly. 通过您的参考ClueGame到的实例WalkwayCell那么这将是能够调用的repaintClueGame直接参考。

The problem with this is you expose the entire ClueGame class to ever instance of WalkwayCell , which allows them to do whatever they want to it... 这里的问题是,你暴露整个ClueGame类的实例过WalkwayCell ,这使得他们能够为所欲为吧...

You Could 你可以

Use an observer pattern , to allow the ClueGame to monitor changes to the state of the WalkwayCell s 使用观察者模式 ,允许ClueGame监测变化的状态WalkwayCell小号

This means that WalkwayCell doesn't really care, it will simply provide some kind of event notification when the state of the object changes and doesn't expose parts of your application to other parts that have no right to know about... 这意味着WalkwayCell并不在乎,它只会在对象状态发生变化时提供某种事件通知,并且不会将应用程序的某些部分暴露给其他无权知道的部分。

Swing makes use of the observer pattern for it's listener API. Swing的监听器API使用了观察者模式。 You could take a look at Writing Event Listeners for some more ideas... 您可以看一下编写事件监听器以了解更多想法...

When creating a Swing GUI, it's important to create GUI model classes. 创建Swing GUI时,创建GUI模型类很重要。 The model classes hold the data for the GUI. 模型类保存GUI的数据。 Having model classes makes coding the Swing view classes so much simpler. 具有模型类使对Swing视图类的编码变得非常简单。

In your case, you would change the state of the instance of the WalkwayCell class, using a method like cell.updateColor(), as you suggested. 在您的情况下,您将按照建议使用诸如cell.updateColor()之类的方法来更改WalkwayCell类实例的状态。 First, you update the model instances. 首先,您更新模型实例。 Then you draw the model instances on the view. 然后在视图上绘制模型实例。

I've explained this concept in more detail with a working example in my Hangman Swing GUI article. 我在Hangman Swing GUI文章中通过一个工作示例更详细地解释了此概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM