Java在屏幕上绘制图像,然后将其删除 [英] Java draw an image on the screen and then remove it

查看:83
本文介绍了Java在屏幕上绘制图像,然后将其删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

我想在屏幕上绘制图像,然后将其删除,但是有一个小问题.

我可以轻松地绘制它,但是随后要移除它时,我只是不知道该怎么做.
不,不仅仅是用背景颜色填充图像的位置,这不是那么容易.

我试图开发的程序是一个游戏,实际上是一条蛇.一条无尽的,所以我的蛇没有端点,只有一条正在旋转的头.

因此,当我绘制图像时,我希望程序在蛇下绘制它,所以我已经绘制的图像不应删除.

当蛇获得奖金(即图像)时,应通过重画顶部的图形将其从屏幕上移除.

图片:
http://imageshack.us/m/840/2449/nvtelenx.png [ ^ ]

Hello!

I would like to draw an image on the screen, and then remove it, but there''s a little problem.

I can easily draw it, but then when it comes to remove, I just don''t know how.
No, not simply filling the image''s location with the background color, it isn''t that easy.

The program I am trying to develop is a game, actually a snake. An endless one, so my snake doesn''t have an endpoint, just a head which is going.

So when I draw the image I want the program to draw it UNDER the snake, so which I have already draw should not be delete.

When a snake takes the bonus (which is the image), it should be removed from the screen by redrawing which was on the top of it.

A picture of it:
http://imageshack.us/m/840/2449/nvtelenx.png[^]

推荐答案

重写paint方法以显示有效内容.
编写代码,使其重新计算位置(在计时器上)并调用repaint().
Override the paint method to display the valid content.
Write the code so that it it recalculates the postions [on a timer probaly] and the call repaint().


您不能仅删除背景图像.您将不得不无效化并重新绘制渲染背景图像的整个部分.

您如何执行此操作取决于您如何构造应用程序.但是就像Nagy Vilmos所说的那样,您可以重画画布的一部分.您可以通过调用重新绘制来完成此操作坐标和大小的方法.这将触发剪辑范围内的所有子画布对象也重新绘制.

请记住,您的实际解决方案首先取决于您在画布上的绘画方式.您是否正在使用一个画布并手动在其上绘制所有形状.还是每个形状都有自己的画布?
You can''t only remove the background image. You will have to invalidate and repaint the entire section that the background image is rendered on.

How you need to do this depends a bit on how you structured your application. But like Nagy Vilmos said you can redraw part of your canvas. You can do this by calling the repaint method with the coordinates and size. This will trigger all child canvas objects within the clipbounds to also repaint.

Keep in mind your actual solution strongly depends on how you are painting on the canvas in the first place. Are you using one canvas and painting all shapes on it manually. Or does each shape have its own canvas?


您需要考虑一叠图层(JPanels/Composites).必须有

-背景层(彩色)
-边界层(与游戏区域的边界透明)
-至少一层奖金层(用于水果,也透明)
-动作层(对于蛇也透明)
-动画层(?)

... 等等.这使您可以更轻松地在上面绘制东西.您还可以在运行时添加和删除图层(思考游戏关卡...).

还要检查以下内容:
如何使用分层窗格@ oracle.com [
You need to think of a stack of layers (JPanels/Composites). There must be

- a background layer (colored)
- a border layer (transparent with the borders of the game field)
- at least one bonus layer (for the fruits, also transparent)
- a action layer (for the snake, also transparent)
- a layer for animations(?)

... and so on. That makes it easier for you to draw things on it. You can also add and delete layers while runtime (thinking of game levels...).

Also check this:
How to Use Layered Panes @ oracle.com[^]

regards
Torsten


这篇关于Java在屏幕上绘制图像,然后将其删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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