I am working on a game in which I am use Canvas to display my graphics. I haven't (and don't plan on) using any Swing components...
In my render method:
I am using a BufferStrategy that I get from the canvas.
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
Later in the same method I end up drawing images(entities, trees, map, etc.), disposing of g, and showing the reference 'bs'.
...drawings onto canvas(entities, trees, map, etc.)
//END RENDER
g.dispose();
bs.show();
In between when I //END RENDER and finish drawing to g I ask if the game is paused. If so I am create a BufferedImage from what was made by g. I render that BufferedImage and then render my inventory screen on top of that:
BufferedImage img = new BufferedImage(this.width, this.height, BufferedImage.TYPE_3BYTE_BGR);
Graphics gx = img.getGraphics();
game.paint(gx);
g.drawImage(img, 0, 0, Color.CYAN, this);
interfaceLayer.render(g);
I am getting a black BufferedImage in my reference img.
Am I doing something wrong by using a BufferStrategy? Should I not be using two different Graphics objects?
Here is a picture of what it looks like before and after I pause the game to get an idea.
Instead of:
g.drawImage(img, 0, 0, Color.CYAN, this);
I changed it to:
g.drawImage(img, 20, 20, Color.CYAN, this);
Just some arbitrary x,y so you can get the idea of whats going on with they layering.
Dont mind the gray box. It is a fillRect() that I will eventually be expanding as part of the graphics in my inventory screen.
Why the black image?
Related
I'm currently making Mario as a school graphics project.
I have most of the collisions done, but I just want the land and bricks to actually look like land and bricks instead of just colored rectangles. I have an ImageIcon for the "land" in my graphics project. The problem is that it is only 16x16 pixels large. In order to make enough land by just making each part of the land one 16x16 pixel, it would essentially be horribly inefficient.
I was wondering if I could get the ImageIcon or possibly buffered image and use it as the "color" for a rectangle to make the chunks of land easier. If that's not possible, can you offer other suggestions on how to go about this problem?
Using a background of tiled images, a texture:
private BufferedImage image;
URL url = getClass().getResource("/mytexture.png");
assert url != null;
image = ImageIO.read(url);
#Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Rectangle rect = new Rectangle(0, 0, width, height);
Rectangle textureRect = new Rectangle(0, 0, image.getWidth(), image.getHeight());
TexturePaint paint = new TexturePaint(image, textureRect);
g2.setPaint(paint);
g2.fill(rect);
}
In general create your own JPanel.
Gimp and other tools allow to create a tilable image by ensuring that a line running to a border will enter at the opposite border.
I've created a Java program that generates snowflakes and I'd like to save the image created as a .png file once the program finishes drawing.
I've searched on Internet, but I've found only programs using BufferedImage, while I use a BufferStrategy, so I don't know exactly where to start.
The draw method in my program uses a BufferStrategy to create the Graphics component.
For example, to draw a simple line the method is:
bs = display.getCanvas().getBufferStrategy();
if (bs == null) {
display.getCanvas().createBufferStrategy(3);
return;
}
g = bs.getDrawGraphics();
g.clearRect(0, 0, width, height);
g.setColor(Color.BLACK);
g.drawLine(0, 0, 50, 50);
What I would like is to get an exact copy of what has been drawn on the screen by the program to be saved as a .png image.
Hope you can help me.
Why not take a screenshot and then past it onto MS paint or some other(and better) image editing software like Photoshop or fire alpaca? That should solve your problem.
The common denominator between BufferedStrategy and BufferedImage is Graphics, so you want to write a paint routine so that you can simply pass a reference of Graphics to it
public void render(Graphics g) {
g.clearRect(0, 0, width, height);
g.setColor(Color.BLACK);
g.drawLine(0, 0, 50, 50);
}
Then you can pass what ever context you want.
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_RGB);
Graphics2D g2d = img.createGraphics();
render(g2d);
g2d.dispose();
Then you can use ImageIO.write to write the image to disk. See Writing/Saving an Image for more details
I am trying to make my second java game (2D) but i have run in a problem: The game will be in fullscreen, but how can i make it so it is scaled properly for each screen? While researching i found out that (almost) everyone is saying the same thing: render it on a Buffered image first and then render the image on the screen resolution, which i understund how to do (
BufferedImage i = new BufferedImage(width, height, 1);
BufferStrategy bs = this.getBufferStrategy();
Graphics2D g2 = i.createGraphics();
if(bs == null){
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
//g2.draw....
g.dispose();
bs.show();
)
but wont that make everything look squeezed or streched?
P.s. if you know any way of making it scale only by keeping the starting resolution (leaving black on both sides) i would accept it
P.s.Sorry for my english.Hope i explained it good enough (if not ask me to clarify)
I've made a code to draw on a Jpanel
frame1.add( new JPanel() {
public void paintComponent( Graphics g ) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.white);
g2.fillRect(0, 0, width, height);
//Drawnig Part
});
Every thing is OK
Now My question is how to save what I've drawn on my JPanel in a file a.PNG or any other type
I have spent a lot of time to write the Drawing Part, So it will be helpful if you can suggest a solution by changing the required parts of my code, instead of rewriting the whole code.
I suggest that you buffer your drawing operations with a BufferedImage, like so:
// This should not be done in the draw method, but rather when
// the frame is created. You should also make a new buffer image when
// the frame is resized. `frameWidth` and `frameHeight` are the
// frame's dimensions.
BufferedImage bufferImage = new BufferedImage(frameWidth, frameHeight,
BufferedImage.TYPE_INT_ARGB);
Graphics2D bufferGraphics = bufferImage.createGraphics();
// In your draw method, do the following steps:
// 1. Clear the buffer:
bufferGraphics.clearRect(0, 0, width, height);
// 2. Draw things to bufferGraphics...
// 3. Copy the buffer:
g2.drawImage(bufferImage, null, 0, 0);
// When you want to save your image presented in the frame, call the following:
ImageIO.write(bufferImage, "png", new File("frameImage.png"));
The Java Tutorial on Creating and Drawing to an Image, along with the ImageIO API reference might be helpful should you require more information.
Have a look at Writing/Saving an Image for more details, but essentially you can do something like...
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
// Draw what ever you want to to the graphics context
g2d.dispose();
ImageIO.write(img, "png", new File("My Awesome Drawing.png"));
If you can't separate the drawing logic from your panel, you could simply use the Graphics context from the BufferedImage to paint the the component with.
Have a look at Exporting a JPanel to an image and Print the whole program layout for examples
Im making a small program that needs previous graphics to stay "put" and visible, even after a repaint causes a variable location to change.
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.red);
g.fillOval(mouseLocX,mouseLocY,30,30);
}
this is all i have in the paint class, and i want to change the mouseLocX and mouseLocY values, and call repaint without having the previous location there. Ive done this before, and most people want the opposite, but i forgot how. Im calling repaint from a MouseMotionListener using mouseDragged();
If you want to preserve what's already been painted so that you get a trail of red ovals instead of a single red oval as the mouse moves, then you shouldn't paint directly on the Graphics object provided by paint(). Instead, use a BufferedImage to store your state. Then render the BufferedImage onto the Graphic provided by paint().
private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
public void paint(Graphics g) {
super.paint(g);
Graphics imageGraphics = image.getGraphics();
imageGraphics.setColor(Color.red);
imageGraphics.fillOval(mouseLocX,mouseLocY,30,30);
g.drawImage(image, 0, 0, null);
}
The BufferedImage provides the persistence for the previous draw operations.