I'm having problems creating a BufferedImage from an offscreen JPanel. Specifically, I'm trying a draw the image of a JPanel (which contains some Java3D elements) as a background image for an application I'm working on.
I've found several threads describing how to do get a JPanel's Image by painting the JPanel to a BufferedImage's graphics context, but when I draw the BufferedImage, all I get is a big white rectangle.
Here's my code:
SimpleWorld j3DPanel; // a custom JPanel that contains some simple Java3D elements
// CONSTRUCTOR
public GameBackgroundObject()
{
super();
// Here I set up a JPanel that contains some Java3D elements.
j3DPanel = new SimpleWorld();
j3DPanel.setSize(mainLevel.SCREENW, mainLevel.SCREENH);
j3DPanel.setBounds(0,0,mainLevel.SCREENW, mainLevel.SCREENH);
j3DPanel.doLayout();
j3DPanel.validate();
}
protected void draw(Graphics2D parentComponentGraphics, ...)
{
super.draw(parentComponentGraphics, ...);
int w = j3DPanel.getWidth();
int h = j3DPanel.getHeight();
BufferedImage j3DImg = new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
Graphics2D g = j3DImg.createGraphics();
j3DPanel.paint(g);
parentComponentGraphics.drawImage(j3DImg, null, null);
}
Also, I've tried adding my JPanel to the contents of a JFrame. It works there and shows the rendered Java3D elements correctly. However, I just get this big white rectangle whenever I try to get the BufferedImage of the JPanel and draw that image.
Drawing a Java 3D scene probably fails because its Canvas3D component is heavyweight. Using the lightweight panel 'com.sun.j3d.exp.swing.JCanvas3D' might solve this problem. Its use is a bit tricky. See the corresponding Java 3D sample 'JCanvas3DExample' or try one of the derived works from here http://www.interactivemesh.org/testspace/j3dmeetsswing.html#leightweight
Try calling:
j3DPanel.paintComponent(g);
instead of:
j3DPanel.paint(g);
Related
I'm trying to use SkiaSharp in Xamarin.forms to draw some polygons on top of an image.
The image is downloaded from a server and than cached in background. So I would prefer not to manipulate the image itself and instead draw a new canvas and place it in a new view on top of the image, like in the screenshots. (using relative layout)
As you can also see in the screenshots, placing the rectangular is not the problem but the "transparent" part is not really transparent.
Code of the first screenshot:
using (var paint = new SKPaint ()) {
paint.IsAntialias = true;
using (var path = new SKPath ()) {
path.MoveTo (0f, 0f);
path.LineTo (width, height);
path.LineTo (0, width);
path.Close ();
paint.Color = SKColors.Orange;
canvas.DrawPath (path, paint);
}
}
In the second I tried to use
canvas.Clear(SKColors.Transparent);
but it only changes to the black background.
Does anyone know whether it is possible to have completely transparent parts in a skia view?
Your question helped me solve this today:
As well as setting the canvas' colour as transparent, the containing Xamarin Forms element needs to be transparent too!
C#
canvas.Clear(SKColors.Transparent);
XAML
<views:SKCanvasView ... BackgroundColor="Transparent" />
guessing here cause you forgot the screenshots,
what you probably want to do is using clipping
something like :
canvas.Clear();
canvas.ClipPath(path);
canvas.ResetMatrix();
canvas.DrawBitmap(...)
I'm trying to figure out how to realize the HUD interface for my brand new game.
I wanted to create it by using the scene2d.ui set of widgets because I've heard that it's more suitable for a responsive UI.
So, I'm trying to create a simple HUD in which there's a score Label aligned at the top-right side of the screen and a pause Button placed on the top-left side of the screen.
Now... I'm using a root Table (as explained in the Table guide https://github.com/libgdx/libgdx/wiki/Table#quickstart) that fills the stage.
I wanted to create a custom class for handling all the hud interface such as:
public class Hud extends Table{
private Label scoreLabel;
private Button pauseButton;
public Hud(){
setFillParent(true);
BitmapFont font = (BitmapFont) MyGame.assetManager.get(Constants.COUNTERS_FONT_NAME);
LabelStyle labelStyle = new LabelStyle(font,Color.WHITE);
scoreLabel = new Label("score:0", labelStyle);
add(scoreLabel).width(2).height(1);
}
}
The issue I'm fighting with is the following: the text inside the Label does not stay inside the Label.
I mean: activating the debug mode I can see the rectangle representing the Label with the correct dimensions...but the text is completely out of bounds and does not scale.
As you can see the font I use is a BitmapFont that is generated when the Game starts via the library FreeTypeFont...The font is then added to the AssetsManager of the game.
I post the code I use for adding the font generator to the assets manager:
//Register the FreeTypeFontGeneratorLoader
InternalFileHandleResolver resolver = new InternalFileHandleResolver();
assetManager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
assetManager.setLoader(BitmapFont.class, ".ttf", new FreetypeFontLoader(resolver));
//Load fonts
FreeTypeFontLoaderParameter countersFontParameters = new FreeTypeFontLoaderParameter();
countersFontParameters.fontFileName = Constants.BASE_FONT_NAME;
countersFontParameters.fontParameters.size = 45;
countersFontParameters.fontParameters.borderColor = Color.BLACK;
countersFontParameters.fontParameters.borderWidth = 1;
assetManager.load(Constants.COUNTERS_FONT_NAME, BitmapFont.class, countersFontParameters);
As you can see I load just one BitmapFont with a size of 45 pixels.
I don't know what the problem is; I would like to try with a classic BitmapFont .fnt in order to see if the problem is in the way the BitmapFont is generated but I'd also like to keep the FreeTypeFont solution.
Thanks in advance,
Luca
P.S.
One thing about the Stage... it uses a ScalingViewport so the dimensions are not in pixels but in virtual units (because my Game uses Box2d and I need a common unit both for the rendering and the physic simulation).
I'm pretty sure that Label will not respond on automatic scaling of the viewport until you will call label.setFontScale( theScale ) explicite.
The best idea here, in my opinion, will be to create another stage exclusively for a HUD so you will have your stage with ScalingViewport (I admit I've never used this one - isn't it better to use some Fill or aomething like this one?) and another stage with FitViewport above for example.
I find it very reasonable and intuitive since it will be like having your HUD displayed on yours car windscreen when driving the car.
The code should looks like:
//in your show method
viewport = createYourScalingViewport();
stage = new Stage();
stage.setViewport(viewport);
HUDviewport = new FitViewport(screenWidth, screenHeight);
HUDstage = new Stage();
HUDstage.setViewport(HUDviewport);
...
//adding some actor to stages
stage.addActor(actor1);
stage.addActor(actor2);
//BUT
HUDstage.addActor( yourHudInstance );
...
//then in your render method act() and draw() stage then HUDstage so the HUD will be above stage
stage.act();
stage.draw();
HUDstage.act();
HUDstage.draw();
Remember of updateing HUDviewport in case of window resizing in the resize Screen method
I guess I've solved my problem.
It seems to be that it doesn't make sense to set the label dimensions.
If anyone needs to have a container that was tight to the text is rendered inside the label the best solution is to create a custom class like this below that extends an HorizonaltGroup (o VerticalGroup).
Let's imagine I need a score label for my game:
public class ScoreLabel extends HorizontalGroup{
...
}
Inside this class I create a Label property and I add it as an Actor.
public class ScoreLabel extends HorizontalGroup{
private Label scores;
public static final float FONT_SCALE = 0.9f;
public ScoreLabel(){
BitmapFont font = MyGame.assetManager.get(Constants.COUNTERS_FONT_NAME);
LabelStyle labelStyle = new LabelStyle(font, Color.WHITE);
scores = new Label(getScoreText(), labelStyle);
scores.setAlignment(Align.left);
scores.setFontScale(FONT_SCALE);
addActor(scores);
}
}
If you activate the debug mode you'll notice that there's a container that is perfectly tight to the text is rendered.
The code above simply loads a BitmapFont previously loaded inside the game assets manager.
Then a font scale is applied...I guess it could be added inside a skin property
Anyway...This is the best solution for me.
I'm still using a stage with a ScalingViewport, in this way I have not to use pixels as unit and all the dimensions are scaled to fill the device screen.
Bye,
Luca
My code is:but I can't find image in QWidget. What can I do if i want to show the image ? And what's wrong with my code?
QTtest::QTtest(QWidget *parent)
: QWidget(parent)
{
QHBoxLayout * m_viewLayout = new QHBoxLayout();
this->setLayout(m_viewLayout);
QHBoxLayout * showLayout = new QHBoxLayout();
QHBoxLayout * btnLayout = new QHBoxLayout();
m_viewLayout->addLayout(showLayout);
m_viewLayout->addLayout(btnLayout);
widget1 = new QWidget();
showLayout->addWidget(widget1);
QPixmap image("C:\\Users\\zhq\\Desktop\\1.png");
QPainter painter(widget1);
painter.drawPixmap(QPoint(0,0),image);
widget2 = new QWidget();
showLayout->addWidget(widget2);
QPushButton * btn = new QPushButton("btn");
btnLayout->addWidget(btn);
showLayout->addStretch();
}
There's a couple of thing wrong with your code.
Assuming that the image is loaded correctly, you should paint the image in the void QWidget::paintEvent(QPaintEvent * event) of your widget. Do not paint outside of paint events. Also, as you're currently doing it, the image will be overwritten the next time the widget updates/reapints itself anyway.
You might consider going an easier route and use a QLabel in your layout instead of widget1. You can then call QPixmap::fromImage() to convert the image to a pixmap and QLabel::setPixmap() on the label to display your image.
I have been working on this for several days without success. I made a sprite animation of a bird flapping it's wings. I want to make the bird fly to a specific spot in a tree. The tree is an image, which has a child image with the animation attached. The animation works fine, but I want to move it to a branch on the tree. The class listed below is attached to the child image, but the bird doesn't move toward the branch.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class FlyBird : MonoBehaviour {
public Transform target; // Target is empty game object defined in the inspector
public float MoveSpeed; // Set to 10 in the inspector
// Script is attached to an Image object and the child of the image is a sprite animation
void FixedUpdate()
{
transform.position = Vector2.MoveTowards(transform.position, target.position, MoveSpeed * Time.deltaTime);
Debug.Log(transform.position.x);
}
}
I don't know what your current behaviour is, but a possibility is that you're moving on FixedUpdate(), so you should use Time.fixedDeltaTime instead of Time.deltaTime
You're using an UI Image, if I got correctly after your edit.
For this type of object, you should use its RectTransform instead of Transform.
Try this:
rectTransform.position = Vector2.MoveTowards(rectTransform.position, target.rectTransform.position, MoveSpeed * Time.deltaTime);
Supposing you can get rectTransform from target object, of course. Another way could be to manipulate rectTransform.anchoredPosition.
Check more information about this type of Rect Transform here: http://docs.unity3d.com/460/Documentation/ScriptReference/RectTransform.html
HI all,
I wnt to develop an ImageViewer using qt. I m trying to resize big images by scaling them. My problem is , when i change the screen orientation some part of the image gets clipped and also if i open the image in landscape mode, by default the size of image remains small even when i change back to portrait mode. What am i Doin wrong?
Please help me out. Heres the code dat i hv written
ImageViewer::ImageViewer()
{
setAttribute(Qt::WA_DeleteOnClose);
QAction *back = new QAction(this);
back->setText(QString("Back"));
connect(back,SIGNAL(triggered()),this,SLOT(close()));
back->setSoftKeyRole(QAction::PositiveSoftKey);
addAction(back);
imageLabel = new QLabel();
imageLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
imageLabel->setAlignment(/*Qt::AlignLeft|*/Qt::AlignCenter);
QWidget *widget = new QWidget;
layout=new QStackedLayout();
layout->addWidget(imageLabel);
widget->setLayout(layout);
setCentralWidget(widget);
}
void ImageViewer::showImage(QString filePath)
{
QImageReader reader;
reader.setFileName(filePath);
QSize imageSize = reader.size();
imageSize.scale(size(), Qt::KeepAspectRatio);
reader.setScaledSize(imageSize);
QImage image = reader.read();
imageLabel->setPixmap(QPixmap::fromImage(image));
imageLabel->adjustSize();
}
You should re-implement QLabel's resizeEvent or install event filter to it and handle QResizeEvent there
The content of showImage method should go to handler of a resize event.
Currently you are using size() of ImageViewer widget (which seems to be derived from QMainWindow), it's better to use imageLabel.size(); or the best QResizeEvent::size() as this will prevent a problem if you will change UI layout in future.