本文将介绍将图像作为JPanel的背景的详细情况,特别是关于将这张图片作为背景的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于android–9个补
本文将介绍将图像作为JPanel的背景的详细情况,特别是关于将这张图片作为背景的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于android – 9个补丁图像作为背景的问题、android – 如何在LibGDx中加载图像作为背景?、Android以编程方式平铺图像作为背景、cordova – SVG图像作为windows phone 8和phonegap中的背景图像的知识。
本文目录一览:- 将图像作为JPanel的背景(将这张图片作为背景)
- android – 9个补丁图像作为背景的问题
- android – 如何在LibGDx中加载图像作为背景?
- Android以编程方式平铺图像作为背景
- cordova – SVG图像作为windows phone 8和phonegap中的背景图像
将图像作为JPanel的背景(将这张图片作为背景)
我是Java新手,目前正在创建带有图形的游戏。我有从扩展的此类JFrame
。在本课程中,我有很多JPanel
需要图像作为背景的。据我所知,为了能够在JPanel中绘制图像,我需要从JPanel扩展一个单独的类,并且该类的paintComponent
方法可以完成工作。但是我不想为每个类创建单独的类JPanel
,我的类太多了。而且我只关心背景。我怎样才能做到这一点?带有匿名内部类吗?怎么样?
为了更好地理解,我提供了一些代码:
public GUI extends JFrame { private JPanel x; ... public GUI() { x = new JPanel(); // put an image background to x }
答案1
小编典典为什么不制作一个包含Image
??的类?
public class ImagePane extends JPanel { private Image image; public ImagePane(Image image) { this.image = image; } @Override public Dimension getPreferredSize() { return image == null ? new Dimension(0, 0) : new Dimension(image.getWidth(this), image.getHeight(this)); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); g2d.drawImage(image, 0, 0, this); g2d.dispose(); }}
您甚至可以提供有关应在何处绘画的提示。
这样,您可以在需要时仅创建一个实例
更新
另一个问题是,为什么?
您可以只使用a即可JLabel
为您绘制图标,而无需进行任何其他工作。
有关更多详细信息,请参见如何使用标签。
这实际上是一个坏主意,因为JLabel
在计算首选大小时不使用其子组件,在确定首选大小时仅使用图像的大小和文本属性,这可能导致组件的大小设置错误
android – 9个补丁图像作为背景的问题
奇数/偶数元素的交替背景图像.我想设定
通过计算位置动态绘制背景.它
正常位图正常工作.但是当我试图使用
ninepatch图像它打破了UI,所有的元素都被扭曲了.什么
我做错了吗这可能是如何创建或创建ninepatch图像
与正常情况相比,使用ninepatch图像有一种不同的方式
位图.
我的列表视图XML就是这样的
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/id01" android:background="@drawable/my_9patch_bg_image"> <ImageView /> <RelativeLayout> <ImageView /> <TextView /> <TextView /> </RelativeLayout> </RelativeLayout>
可能解决方案here可能为我的问题工作.这是确切的,虽然我必须尝试.
解决方法
我有同样的问题,你需要设置为零(即使你不需要它).
祝你好运.
android – 如何在LibGDx中加载图像作为背景?
01-10 10:37:18.206: E/AndroidRuntime(1045): FATAL EXCEPTION: GLThread 99 01-10 10:37:18.206: E/AndroidRuntime(1045): com.badlogic.gdx.utils.GdxRuntimeException: Texture width and height must be powers of two: 480x800
我的代码看起来像:
public class Tapeta implements ApplicationListener { private SpriteBatch batch; private Texture texture; private TextureRegion region; @Override public void create() { texture = new Texture(Gdx.files.internal("data/cat.jpg")); Texture.setEnforcePotimages(false); } @Override public void dispose() { texture.dispose(); } @Override public void render() { Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); batch.begin(); batch.draw(texture,0); batch.end(); } @Override public void resize(int width,int height) { } @Override public void pause() { } @Override public void resume() { } }
我正在寻找我的问题的解决方案,我没有找到.
解决方法
Texture width and height must be powers of two: 480x800
因此纹理宽度和高度必须是以下之一:2,4,8,16,32,64,128,256,512,1024,2048(该列表中既不是480也不是800 ……)
你有这条神奇的线条:
Texture.setEnforcePotimages(false);
但它在纹理加载后.如果你把它放在纹理加载之前,异常会消失吗? (在您的问题中包含更多的异常回溯会很有帮助).我相信这种方法仅适用于您知道底层OpenGL ES 1.0系统支持非二次幂纹理扩展的情况.设置此标志不会修复真正不支持2次幂纹理的硬件. (我不知道这个世界上剩下多少这样的硬件,所以这些日子可能是无害的.)
二次幂纹理要求是硬件和OpenGL ES的限制.此要求已从OpenGL ES 2.0中删除.
解决这个问题的另一种方法是构建一个两个大小的纹理图集,它包含你的非二能力纹理.然后渲染图集图像的子部分.见http://bitiotic.com/blog/2012/05/10/tutorial-for-texturepacker-and-libgdx/
Android以编程方式平铺图像作为背景
button_inner_shadow是图像
navigation_background是用于平铺上面图像的xml
1:直接平铺图像
BitmapDrawable navigationBackground = new BitmapDrawable(BitmapFactory.decodeResource( getResources(),R.drawable.button_inner_shadow)); navigationBackground.setTileModeX(Shader.TileMode.REPEAT); navigationTextViews[id].setBackgroundDrawable(navigationBackground);
2:使用xml来平铺图像
navigationBackground = new BitmapDrawable(BitmapFactory.decodeResource( getResources(),R.drawable.navigation_background)); navigationTextViews[id].setBackgroundDrawable(navigationBackground);
navigation_background
<?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:dither="true" android:src="@drawable/button_inner_shadow" android:tileMode="repeat" />
我究竟做错了什么?
我还使用setBackgroundResource来设置更改程序另一部分的背景颜色,并认为这是一个问题.我添加了navigationTextViews [id] .setBackgroundResource(0);,这应该删除后台资源,这不适用于我使用的上述解决方案.
编辑:navigationTextViews []是一个TextViews数组
解决方法
cordova – SVG图像作为windows phone 8和phonegap中的背景图像
.loyaltystar { position:absolute; margin-top:3px; background:url(../img/available-icon.svg) no-repeat; width:21px; height:21px; } <li><a href="a.html"></a></li>
是phonegap 2.4支持Windows Phone 8的SVG.
解决方法
我们今天的关于将图像作为JPanel的背景和将这张图片作为背景的分享就到这里,谢谢您的阅读,如果想了解更多关于android – 9个补丁图像作为背景的问题、android – 如何在LibGDx中加载图像作为背景?、Android以编程方式平铺图像作为背景、cordova – SVG图像作为windows phone 8和phonegap中的背景图像的相关信息,可以在本站进行搜索。
本文标签: