Reputation: 51
I am doing a game in java, but I didnt put image, I already tried to put it by "drawimage", but it is not working right = ( I do not know where would be the best place to save the image and how would be the best method to call the image.`
public void paint(Graphics2D g2){
g2.setColor(getRandomColor());
g2.fillOval((int)x, (int)y, (int)diametro, (int)diametro);
}
`
This code is from my class asteroides they are a circle (because I'm going to test the collision with circles). Thanks
Upvotes: 2
Views: 12632
Reputation: 46
I'm pretty sure you mean you want do draw an image on the Panel. First of all, I'd recommend you create a resources folder inside the projects src folder, and add all images there. Once you're done, you have to load the image with imageIO and draw it with drawImage. Here's a short example:
package asteroid;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class Nave {
BufferedImage iconeNave;
public Nave( ... ) {
try{
iconeNave = ImageIO.read(getClass().getResource("/resources/nave.png"));
}catch(IOException e){e.printStackTrace();}
catch(Exception e){e.printStackTrace();}
}
@Override
public void paint(Graphics2D g2){
AffineTransform at = new AffineTransform();
at.translate((int)x + radius/2.5,(int)y + radius/2.5);
at.rotate(Math.PI/2 + angle);
at.translate(-iconeNave.getWidth()/2, -iconeNave.getHeight()/2);
g2.drawImage(iconeNave, at, null);
}
}
Upvotes: 3
Reputation: 171
In order to create an image in Java, you will first need to create a JPanel (essentially a window on your screen). It will look something like this (taken from javacodex.com):
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import javax.imageio.ImageIO;
public class JPanelExample {
public static void main(String[] arguments) throws IOException {
JPanel panel = new JPanel();
BufferedImage image = ImageIO.read(new File("./java.jpg"));
JLabel label = new JLabel(new ImageIcon(image));
panel.add(label);
// main window
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("JPanel Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// add the Jpanel to the main window
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
However, I am assuming that by "image," you mean a shape drawn out onto the screen instead (since you are using a paint method that draws a circle in your example). Then, you have your code in the correct method but need to implement a JPanel (since without a JPanel or JFrame you can't really display anything onto the screen graphics-wise).
Here is an example taken from Board.java of my Atari Breakout mini-game:
public class Board extends JPanel implements KeyListener {
// Other Game Code
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLACK);
// Shows lives left and score on screen
g2.drawString("Lives left: " + lives, 400, 600);
g2.drawString("Score: " + score, 400, 500);
// Paints Walls (separate paint method created in wall class)
topWall.paint(g2);
bottomWall.paint(g2);
leftWall.paint(g2);
rightWall.paint(g2);
// Paints 2 Balls (separate paint method created in ball class)
b.paint(g2);
b2.paint(g2);
// Paints Paddle (separate paint method created in paddle class)
paddle.paint(g2);
// Paints Bricks based on current level (separate paint method created in brick class)
// Bricks were created/stored in 2D Array, so drawn on screen through 2D Array as well.
if (level == 1) {
for (int x = 0; x < bricks.length; x++) {
for (int i = 0; i < bricks[0].length; i++) {
bricks[x][i].paint(g2);
}
}
}
if (level == 2) {
for (int x = 0; x < bricks.length; x++) {
for (int i = 0; i < bricks[0].length; i++) {
bricks[x][i].paint(g2);
}
}
}
}
Here is an example of a Paint Method in the Paddle Class:
// Constructor
public Paddle(int x, int y, int w, int h){
xpos = x;
ypos = y;
width = w;
height = h;
r = new Rectangle(xpos, ypos, width, height);
}
public void paint(Graphics2D g2){
g2.fill(r);
}
Output (What is shown on Screen):
Hope this helps you in drawing your circle!
Upvotes: 0