用Java结合两个图形对象 [英] Combine two graphics objects in Java

查看:152
本文介绍了用Java结合两个图形对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Graphics对象,是否可以将它们合并为一个?



Hovercraft Full of Eels的答案很有用,但不是我的意思。假设我有两个Graphics对象, private Graphics gr1 private Graphics gr2 。现在,我应该如何将它们合并到 paintComponent(Graphics)方法中以将它们绘制到我的(例如JPanel)?



这个问题不是关于图像,而是关于仅仅是图形对象。抱歉,第一段引入误导。



编辑:

回应气垫船充满鳗鱼:



如果您绘制 paintComponent()方法,我发现它很烦人,如果您重绘屏幕,你想移动一些你必须保存的东西的坐标和尺寸,调整它们并在Graphics对象中得到它们。

我问自己的问题是accely:什么对象最适合或保存图形对象。一个图形对象,我想。但问题是,如果你有(例如)两个矩形,一个移动到左边,一个移动到右边,如果你有1个Graphics对象,你不能。

我的解决方案是多个图形对象,它们可以被转换为模拟运动,然后绘制到屏幕上。

@Hovercraft充满鳗鱼我想你(和大多数SO'ers)认为这不是一个好的解决方案,看着你的答案。
但是,一个答案说你这样做是错的,你最好停止编程根本没有帮助我,所以请给我一个替代方案。 en.wikipedia.org/wiki/Impositionrel =nofollow noreferrer> impose 图片,使用适当的布局,如图所示 here compose a>图片,请使用适当的复合规则,如此处所示。




附录:@HFOE是正确的。一个 Graphics Graphics2D 的实例,有时称为图形上下文,是一种暂时的手段应用渲染方法;目标设备或离屏图像必须独立存在。实现 Shape 接口是封装有关什么在给定上下文中呈现的信息的好候选者。 AffineTest GraphPanel 就是例子。


I got two Graphics objects, is it possible to combine them into one?

The answer of Hovercraft Full of Eels is useful, but not what I meant. Say I have two Graphics objects, private Graphics gr1 and private Graphics gr2. Now, how should I merge them in the paintComponent(Graphics) method to draw them to my (for example JPanel)?

The question is not at all about images, but about mere Graphics objects. Sorry, the first paragraph was misleading.

Edit:
In response to Hovercraft Full of Eels:

If you draw in the paintComponent() method, I find it annoying everything is gone if you redraw your screen, and if you want to move something you have to save the coordinates and dimensions of everything, adjust those and get them someway in your Graphics object.
The question I asked myself was accely: What object is best suited or saving a Graphics object. A Graphics object, I thought. but the problem is that if you have (for example) two rectangles, and one moves to the left and one moves to the right you can't if you have 1 Graphics object.
My solution was multiple graphics objects, which can be transformed to simulate movement and then drawn to the screen.
@Hovercraft Full of Eels I think you (and most SO'ers) think this is not a good solution, looking at your answer.
But, an answer saying You're doing it all wrong, you'd better stop programming doesn't help me at all, so please give me an alternative.

解决方案

  • To impose images, use an appropriate layout, as shown here.

  • To compose images, use an appropriate composite rule, as shown here.

Addendum: @HFOE is correct. An instance of Graphics or Graphics2D, sometimes called a graphics context, is a transitory means to apply rendering methods; the target device or off-screen image must exist independently. Classes implementing the Shape interface are good candidates for encapsulating information about what to render in a given context. AffineTest and GraphPanel are examples.

这篇关于用Java结合两个图形对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆