I am making a project with a graphics environment which uses buffered images from files. I searched and the way I was using (Java.IO file) wouldn't work after exporting the jar, and the way to do it was with getClass().getResourceAsStream() but I'm having troubles figuring out filepath
here is my files at eclipse-IDE:
some people said I should move my resources folder inside the src
but I still don't know what would be the text for a image1.png inside of res
I did try searching and implementing but that gave me the errors:
https://pastebin.com/MChSyWH5
here is how the class I'm trying to draw the images is without implementing getClass().getResourceAsStream() :
package src;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
public class Menu {
public Rectangle Playbutton = new Rectangle( Game.WIDTH/2-25, 60, 50, 10);
public Rectangle Exitbutton = new Rectangle( Game.WIDTH/2-25, 80, 50, 10);
public static String desc = null;
private BufferedImage imagePlay;
private BufferedImage players1;
private BufferedImage players2;
private BufferedImage imageExit;
private BufferedImage IMPOSSIBLE;
private BufferedImage Hard;
private BufferedImage Easy;
private BufferedImage Normal;
private BufferedImage imageTitle;
private BufferedImage imageResume;
private BufferedImage imageMain;
private BufferedImage OPTIONS;
private BufferedImage BACK;
public static int brightloss = 0;
public static int stibaum = 0;
public static boolean sti = true;
public static boolean showc = false;
public static enum STATE{
MAIN,
P1P2,
INGAME,
DIFICULTY,
OPTIONS
};
public static STATE State = STATE.MAIN;
InputStream input = null;
public static STATE Statepre = State;
public static boolean dark = true;
public static Slider slider;
public static void SAVESTATE(){
Statepre = State;
}
public Menu() {
try {
input = new FileInputStream("res/Playbutt.png");
} catch (IOException e) {
e.printStackTrace();
}
String current;
try {
current = new java.io.File( "." ).getCanonicalPath();
System.out.println("Current dir:"+current);
String currentDir = System.getProperty("user.dir");
System.out.println("Current dir using System:" +currentDir);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
slider = new Slider(100, 100, 50, false, brightloss);
System.out.println(new File("res/Playbutt.png").getAbsolutePath()+"-arquivo");
System.out.println(new File("/res/Playbutt.png").exists()+"-arquivo2");
try { // ImageIO.read(new File("res/Playbutt.png"));
imagePlay = ImageIO.read(input);
IMPOSSIBLE = ImageIO.read(new File("res/IMPOSSIbutt.png"));
Hard = ImageIO.read(new File("res/Hardbutt.png"));
Easy = ImageIO.read(new File("res/Easybutt.png"));
Normal = ImageIO.read(new File("res/Normalbutt.png"));
players1 = ImageIO.read(new File("res/1P.png"));
players2 = ImageIO.read(new File("res/2P.png"));
imageExit = ImageIO.read(new File("res/Exitbutt.png"));
imageTitle = ImageIO.read(new File("res/Title.png"));
imageResume = ImageIO.read(new File("res/Resumebutt.png"));
imageMain = ImageIO.read(new File("res/Mainbutt.png"));
OPTIONS = ImageIO.read(new File("res/Optbutt.png"));
BACK = ImageIO.read(new File("res/Backbutt.png"));
} catch (IOException ex) {
// handle exception...
}
}
public void render(Graphics g) {
g.setFont(new Font("DefaultFont", Font.PLAIN, 10));
g.setColor(Colors.Trans_GREEN3);
g.fillRect(0, Game.HEIGHT-10, Game.WIDTH,10);
g.fillRect(0, Game.HEIGHT-9, Game.WIDTH,9);
g.fillRect(0, Game.HEIGHT-8, Game.WIDTH,8);
g.fillRect(0, Game.HEIGHT-7, Game.WIDTH,7);
g.fillRect(0, Game.HEIGHT-6, Game.WIDTH,6);
g.fillRect(0, Game.HEIGHT-5, Game.WIDTH,5);
g.setColor(Colors.Trans_GREEN3);
g.fillRect(0, 0, Game.WIDTH,10);
g.fillRect(0, 0, Game.WIDTH,9);
g.fillRect(0, 0, Game.WIDTH,8);
g.fillRect(0, 0, Game.WIDTH,7);
g.fillRect(0, 0, Game.WIDTH,6);
g.fillRect(0, 0, Game.WIDTH,5);
g.setColor(Colors.Trans_GREEN3);
g.fillRect(0, 0, 10,Game.HEIGHT);
g.fillRect(0, 0, 9,Game.HEIGHT);
g.fillRect(0, 0, 8,Game.HEIGHT);
g.fillRect(0, 0, 7,Game.HEIGHT);
g.fillRect(0, 0, 6,Game.HEIGHT);
g.fillRect(0, 0, 5,Game.HEIGHT);
g.setColor(Colors.Trans_GREEN3);
g.setColor(Colors.Trans_GREEN3);
g.fillRect(Game.WIDTH-10, 0, 10,Game.HEIGHT);
g.fillRect(Game.WIDTH-9, 0, 9,Game.HEIGHT);
g.fillRect(Game.WIDTH-8, 0, 8,Game.HEIGHT);
g.fillRect(Game.WIDTH-7, 0, 7,Game.HEIGHT);
g.fillRect(Game.WIDTH-6, 0, 6,Game.HEIGHT);
g.fillRect(Game.WIDTH-5, 0, 5,Game.HEIGHT);
Graphics2D g2d = (Graphics2D) g;
if(State==STATE.MAIN) {
if(MouseInput.mx>=95 && MouseInput.mx<=145) {
if(MouseInput.my>60&&MouseInput.my<70) {
g.setColor(Colors.outline);
g.fillRect(Game.WIDTH/2-26, 59, 54, 14);
}
else if(MouseInput.my>80&&MouseInput.my<90) {
g.setColor(Colors.outline);
g.fillRect(Game.WIDTH/2-26, 79, 54, 14);
}
else if(MouseInput.my>40&&MouseInput.my<50) {
g.setColor(Colors.outline);
g.fillRect(Game.WIDTH/2-26, 39, 54, 14);
desc=("");
g.setColor(Color.green);
g.drawString(desc,45 , Game.HEIGHT/2+50);
}
}
g2d.drawImage(this.imageTitle,100,200,null);
g2d.drawImage(this.OPTIONS, Game.WIDTH/2-25, 60, null);
g2d.drawImage(this.imagePlay, Game.WIDTH/2-25, 40, null);
g2d.drawImage(this.imageExit, Game.WIDTH/2-25, 80, null);
double locationX = imageTitle.getWidth() / 2;
double locationY = imageTitle.getHeight() / 2;
AffineTransform tx = AffineTransform.getRotateInstance((Math.toRadians(stibaum)), locationX, locationY);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
AffineTransform old = g2d.getTransform();
g2d.drawImage(op.filter(imageTitle, null), Game.WIDTH/2-50, 10, null);
g2d.setTransform(old);
//things you draw after here will not be rotated
}
else if(State==STATE.INGAME) {
if(MouseInput.mx>=95 && MouseInput.mx<=145) {
if(MouseInput.my>70&&MouseInput.my<80) {
g.setColor(Colors.outline);
g.fillRect(Game.WIDTH/2-26, 69, 54, 14);
}
else if(MouseInput.my>90&&MouseInput.my<100) {
g.fillRect(Game.WIDTH/2-26, 89, 54, 14);
}else if(MouseInput.my>50&&MouseInput.my<60) {
g.setColor(Colors.outline);
g.fillRect(Game.WIDTH/2-26, 49, 54, 14);
}
}
g2d.drawImage(this.imageResume, Game.WIDTH/2-25, 70, null);
g2d.drawImage(this.imageMain, Game.WIDTH/2-25, 50, null);
g2d.drawImage(this.imageExit, Game.WIDTH/2-25, 90, null);
g2d.drawImage(this.imageTitle, Game.WIDTH/2-50, 10, null);}
else if(State==STATE.P1P2) {
if(MouseInput.mx>=15 && MouseInput.mx<=40) {
if(MouseInput.my>=106&&MouseInput.my<118) {
g.setColor(Colors.outline);
g.fillRect(14, Game.HEIGHT-29, 28, 15);
desc=("");
g.setColor(Color.green);
g.drawString(desc,45 , Game.HEIGHT/2+50);
}}
if(MouseInput.my>30&&MouseInput.my<90) {
if(MouseInput.mx>=60 && MouseInput.mx<=120) {
g.setColor(Colors.outline);
g.fillRect(Game.WIDTH/2-61, 29,66, 66);
}
if(MouseInput.mx>=130 && MouseInput.mx<=190) {
g.setColor(Colors.outline);
g.fillRect(Game.WIDTH/2+9, 29,66, 66);
}}
g2d.drawImage(this.players1, Game.WIDTH/2-60, 30, null);
g2d.drawImage(this.players2, Game.WIDTH/2+10, 30, null);
g2d.drawImage(this.BACK, 15, Game.HEIGHT-28, null);
}
else if(State==STATE.DIFICULTY) {
if(MouseInput.mx>=15 && MouseInput.mx<=40) {
if(MouseInput.my>=106&&MouseInput.my<118) {
g.setColor(Colors.outline);
g.fillRect(14, Game.HEIGHT-29, 28, 15);
desc=("");
g.setColor(Color.green);
g.drawString(desc,45 , Game.HEIGHT/2+50);
}}
if(MouseInput.mx>=95 && MouseInput.mx<=145) {
if(MouseInput.my>70&&MouseInput.my<80) {
g.setColor(Colors.outline);
g.fillRect(Game.WIDTH/2-26, 69, 54, 14);
g.setColor(Color.green);
desc=("for those who want challenge");
g.drawString(desc,50 , Game.HEIGHT/2+50);
}
else if(MouseInput.my>90&&MouseInput.my<100) {
g.setColor(Colors.outline);
g.fillRect(Game.WIDTH/2-26, 89, 54, 14);
desc=("for those who want to cry");
g.setColor(Color.green);
g.drawString(desc,70 , Game.HEIGHT/2+50);
}else if(MouseInput.my>50&&MouseInput.my<60) {
g.setColor(Colors.outline);
g.fillRect(Game.WIDTH/2-26, 49, 54, 14);
desc=("for those who want to play casually");
g.setColor(Color.green);
g.drawString(desc,45 , Game.HEIGHT/2+50);
}if(Ball.pmih) {
if(MouseInput.my>33&&MouseInput.my<44) {
g.setColor(Colors.outline);
g.fillRect(Game.WIDTH/2-26, 32, 54, 14);
desc=("for those who don't really have a life");
g.setColor(Color.green);
g.drawString(desc,45 , Game.HEIGHT/2+50);
}}
}
if(Ball.pmih) {
g2d.drawImage(this.IMPOSSIBLE, Game.WIDTH/2-25, 33, null);}
g2d.drawImage(this.Normal, Game.WIDTH/2-25, 70, null);
g2d.drawImage(this.Easy, Game.WIDTH/2-25, 50, null);
g2d.drawImage(this.Hard, Game.WIDTH/2-25, 90, null);
g2d.drawImage(this.imageTitle, Game.WIDTH/2-50, 10, null);
g2d.drawImage(this.BACK, 15, Game.HEIGHT-28, null);
}
else if(State==STATE.OPTIONS) {
if(MouseInput.mx>=15 && MouseInput.mx<=40) {
if(MouseInput.my>=106&&MouseInput.my<118) {
g.setColor(Colors.outline);
g.fillRect(14, Game.HEIGHT-29, 28, 15);
desc=("");
g.setColor(Color.green);
g.drawString(desc,45 , Game.HEIGHT/2+50);
}}
slider.render(g);
g2d.drawImage(this.imageTitle, Game.WIDTH/2-50, 10, null);
g2d.drawImage(this.BACK, 15, Game.HEIGHT-28, null);
}
}
public void tick() {
slider.tick();
if(stibaum<5&&sti==true) {
stibaum+=1;
}
if(stibaum>=5&&sti==true) {sti=false;}
if(stibaum>-5&&sti==false) {
stibaum-=1;
}
if(stibaum<=-5&&sti==false) {sti=true;}
if(State==STATE.OPTIONS) {
Statepre=STATE.MAIN;
}
else if(State==STATE.P1P2) {
Statepre=STATE.MAIN;
}
else if(State==STATE.DIFICULTY) {
Statepre=STATE.P1P2;
}
}
}
input = new FileInputStream("res/Playbutt.png");
This doesn't work. You can't use FIS, you'd have to use Menu.class.getResource. Not getClass().
ImageIO.read(new File("res/2P.png"));
Same here.
} catch (IOException ex) {
// handle exception...
}
This isn't how you do this stuff. Get rid of it all, and add throws IOException to the constructor. public static void main can and generally should be declared to throws Exception. You're ignoring the errors, making it harder to debug this problem. Hence, don't do it this way.
None of your code is trying to use Main.class.getResource.
res is not marked as a source dir in your eclipse project. You need to fix that. You have 2 options:
Forget about a res dir. Put your images in the exact same place as your java files are. Just move them there - and then getResource will work fine. For example, if img.png is in the same dir (the exact same dir!) as Main.java, then Main.class.getResource("img.png") will work fine.
Use maven, and then change this project to be a maven based one. Then eclipse will take care of it.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I've been trying to make a shop for my game.
This has been unsuccessful.
I've tried drawComponent, didn't work.
No errors, code executed, but didn't work.
Now i'm trying to do:
private void render() {
Graphics2D g = (Graphics2D) graphics.getGraphics();
/////////////////////
g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
/////////////////////
g.dispose();
Graphics2D g2d = (Graphics2D) getGraphics();
g2d.drawImage(img, 0, 0, null);
g2d.dispose();
}
Now i get a NullPointerException on g2d.
I've tried everything.
`Exception in thread "game" java.lang.NullPointerException
at com.johnythecarrot.game.Shop$DrawPane.access$2(Shop.java:123)
at com.johnythecarrot.game.Shop.render(Shop.java:154)
at com.johnythecarrot.game.Game.render(Game.java:75)
at com.johnythecarrot.game.Game.run(Game.java:112)
at java.lang.Thread.run(Unknown Source)`
My goals are to be able to have clickable buttons.
It DID work. But i had to restart almost everytime. Because mostly of the time to code wasn't even executed. So i tried to fix it. Now it's all messed up.
This is the code to it.
(DoubleInt is a part of my library it's nothing more than just x and y. )
public class Shop {
public BuildWindow window;
public static JWindow w;
private int WIDTH = 860, HEIGHT = 440;
private BufferedImage graphics = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
public DrawPane drawPane;
public Shop() {
//window = new BuildWindow().setSize(new DoubleInt(100, 100)).at(wi, he).setTitle("Shop").setOpacity(1).setDragable(false).showEmpty(true);
w = new JWindow();
w.setOpacity(1);
w.setSize(WIDTH, HEIGHT);
w.setLocation(800, 800);
w.setVisible(false);
w.setAlwaysOnTop(true);
//graphics = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
}
private void createShop() {
/***Graphics2D g = (Graphics2D) graphics.getGraphics();
g.setColor(Color.blue);
g.drawString("hey", WIDTH-50, HEIGHT-50);
g.fillRect(0, 0, WIDTH, HEIGHT);*/
}
public class DrawPane extends JPanel {
int width = WIDTH;
int height = HEIGHT;
private ArrayList<Shape> buttons;
private Shape btn1 = new Rectangle2D.Double(20, 60, width/2, height-20);
private Shape btnClose = new Rectangle2D.Double(width-25, 5, 20, 20);
Point wCoords;
Point mCoords;
public DrawPane() {
buttons = new ArrayList<>();
buttons.add(btn1);
buttons.add(btnClose);
addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
for(Shape s : buttons) {
if(s.contains(e.getPoint())) {
System.out.println("Clicked " + s.getBounds());
if(s == btnClose) {
w.dispose();
}
}
}
}
#Override
public void mousePressed(MouseEvent e) {
mCoords = e.getPoint();
}
#Override
public void mouseReleased(MouseEvent arg0) {
mCoords = null;
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
wCoords = e.getLocationOnScreen();
w.setLocation(wCoords.x - mCoords.x, wCoords.y - mCoords.y);
}
});
}
void repaintThis() {
repaint();
}
BufferedImage img = loadImageFrom.LoadImageFrom(Shop.class, "bar.png");
Graphics gb;
/**
* super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g.setColor(Color.red);
//g.fillRect(0, 0, width, 50);
g.drawImage(img, 0, 0, width, 50, null);
g.setColor(Color.WHITE);
g.drawString("SHOP", 15, 30);
g.drawString("X", width-20, 20);
for(Shape b : buttons) {
g2d.draw(b);
}
System.out.println("Built");
gb = g;
*/
private void render() {
Graphics2D g = (Graphics2D) graphics.getGraphics();
/////////////////////
g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
/////////////////////
g.dispose();
Graphics2D g2d = (Graphics2D) getGraphics();
g2d.drawImage(img, 0, 0, null);
g2d.dispose();
}
public void Build() {
Graphics g = gb;
Graphics2D g2d = (Graphics2D) g;
g.setColor(Color.red);
//g.fillRect(0, 0, width, 50);
g.drawImage(img, 0, 0, width, 50, null);
g.setColor(Color.WHITE);
g.drawString("SHOP", 15, 30);
g.drawString("X", width-20, 20);
for(Shape b : buttons) {
g2d.draw(b);
}
System.out.println("Built");
}
}
public void render(Graphics2D g) {
drawPane.render();
}
public void addDrawPane() {
drawPane = new DrawPane();
w.add(drawPane);
}
}
If you need access to more code, just ask me.
You should override the paintComponent method like this:
public class DrawPane extends JPanel {
// all your variables and other things
#Override
paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
// Your code goes here, use the g2d
}
}
then if you need to repaint your component, simply call repaint() on it.
I am currently in progress of making a game involving a button made via JPanel. However, at some point during the program's runtime, the JPanel used to make the button begins to give constant errors whenever a mouse event is registered in the following form:
at java.awt.AWTEventMultiCaster.mouse[eventName](UnknownSource)
It goes so fast that I can't see the initial error, and the error only begins at some point during mid-runtime. Can anybody help me out here? The following is the code for my JPanel:
JPanel trackPanel = new JPanel() {
int state = 0;
#Override
protected void paintComponent(Graphics g) {
super.paintComponents(g);
addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
if (dragMe == null) {
state = 1;
}
}
public void mouseExited(MouseEvent e) {
if (dragMe == null) state = 0;
else state = 1;
}
public void mousePressed(MouseEvent e) {
if (dragMe == null && !e.isMetaDown())
{
dragMe = new DragFrameThread(vTrack);
dragMe.start();
}
state = 2;
}
public void mouseReleased(MouseEvent e) {
if (state == 2 && e.isMetaDown()) System.exit(0);
if (dragMe != null) {
dragMe.interrupt();
dragMe = null;
state = 1;
}
}
});
g.setColor(new Color(0, 127, 127, 255));
g.drawRect(0, 0, getWidth()-1, getHeight()-1);
g.setColor(new Color(0, 127, 127, 223));
g.drawRect(1, 1, getWidth()-3, getHeight()-3);
g.setColor(new Color(0, 127, 127, 191));
g.drawRect(2, 2, getWidth()-5, getHeight()-5);
g.setColor(new Color(0, 127, 127, 159));
g.fillRect(3, 3, getWidth()-6, getHeight()-6);
g.drawImage(BootAssets.VTScaledLogo, 0, 0, getWidth(), getHeight(), null);
if (state == 1) g.setColor(new Color(255, 255, 255, 127));
if (state == 2) g.setColor(new Color(0, 0, 0, 127));
if (state != 0) g.fillRect(0, 0, getWidth(), getHeight());
repaint();
}
};
Thanks a bunch!
~Para
I believe the problem is that the paintComponent method is called when the widget is painted. Adding a mouse listener on every paint call is wrong.
Move the addMouseListener out of the paintComponent method and add it separately to the trackPanel. You will need to refactor the code since it currently has the state set and updated in the way the JPanel is defined.
private final AtomicInteger state = new AtomicInteger(0);
JPanel trackPanel = new JPanel() {
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(new Color(0, 127, 127, 255));
g.drawRect(0, 0, getWidth()-1, getHeight()-1);
g.setColor(new Color(0, 127, 127, 223));
g.drawRect(1, 1, getWidth()-3, getHeight()-3);
g.setColor(new Color(0, 127, 127, 191));
g.drawRect(2, 2, getWidth()-5, getHeight()-5);
g.setColor(new Color(0, 127, 127, 159));
g.fillRect(3, 3, getWidth()-6, getHeight()-6);
g.drawImage(BootAssets.VTScaledLogo, 0, 0, getWidth(), getHeight(), null);
if (state.get() == 1) g.setColor(new Color(255, 255, 255, 127));
if (state.get() == 2) g.setColor(new Color(0, 0, 0, 127));
if (state.get() != 0) g.fillRect(0, 0, getWidth(), getHeight());
}
};
trackPanel.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
if (dragMe == null) {
state.set(1);
}
}
public void mouseExited(MouseEvent e) {
if (dragMe == null) state.set(0);
else state.set(1);
}
public void mousePressed(MouseEvent e) {
if (dragMe == null && !e.isMetaDown())
{
dragMe = new DragFrameThread(vTrack);
dragMe.start();
}
state.set(2);
}
public void mouseReleased(MouseEvent e) {
if (state.get() == 2 && e.isMetaDown()) System.exit(0);
if (dragMe != null) {
dragMe.interrupt();
dragMe = null;
state.set(1);
}
}
});
I've been following a tutorial to learn graphics and in one program the author uses images to make texture paints. I have copied his code however I dont know where to actually put the images for it to read. I have tried making a resources folder in eclipse and setting it as a source folder build path but this didnt work. The code is below:
EDIT:
Okay, I figured out that it is taking images from the source of the class. However, lets say I wanted to pull an image from my desktop, or some other location on my hard drive, how would i do this?
class Surface extends JPanel {
private BufferedImage slate;
private BufferedImage java;
private BufferedImage pane;
private TexturePaint slatetp;
private TexturePaint javatp;
private TexturePaint panetp;
public Surface() {
loadImages();
}
private void loadImages() {
try {
slate = ImageIO.read(new File("slate.png"));
java = ImageIO.read(new File("java.png"));
pane = ImageIO.read(new File("pane.png"));
} catch (IOException ex) {
Logger.getLogger(Surface.class.getName()).log(
Level.SEVERE, null, ex);
}
}
private void doDrawing(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
slatetp = new TexturePaint(slate, new Rectangle(0, 0, 90, 60));
javatp = new TexturePaint(java, new Rectangle(0, 0, 90, 60));
panetp = new TexturePaint(pane, new Rectangle(0, 0, 90, 60));
g2d.setPaint(slatetp);
g2d.fillRect(10, 15, 90, 60);
g2d.setPaint(javatp);
g2d.fillRect(130, 15, 90, 60);
g2d.setPaint(panetp);
g2d.fillRect(250, 15, 90, 60);
g2d.dispose();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}
This can be helpful. Or just use absolute PATH to file. linux: /home/user/... widndows: C:/Users/..