The window shows up and works as intended, but it still gives me a NullPointerException for some reason. Two in fact. Here's the code:
public class SwagFrame extends JFrame {
public SwagFrame() {super("Swag");}
public void paint(Graphics g) {
Image bg = null;
try {
bg = ImageIO.read(new File("swag.png"));
} catch (IOException e) {
System.out.println("Swag not turned on");
System.exit(-1);
}
g.drawImage(bg, 0, 0, null); // exception here
g.setColor(Color.GREEN);
g.fillOval(250, 250, 100, 100);
}
public static void main(String[] args) {
SwagFrame frame = new SwagFrame();
frame.setVisible(true);
frame.setSize(525, 525);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.paint(null); // exception here
}
}
If paint() requires an object in its parameters, why does it still work anyway but throw the exception after the fact?
Two things I did. I made the SwagFrame a JPanel instead of a JFrame, so I could use the paintComponent method, I deleted the frame.paint(null) and I changed the null in the drawImage to this. The code works fine now.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SwagFrame extends JPanel {
public SwagFrame() {
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
BufferedImage bg = null;
try {
bg = ImageIO.read(new File("icons/stacko/stackoverflow1.png"));
} catch (IOException e) {
System.out.println("Swag not turned on");
//System.exit(-1);
}
g.drawImage(bg, 0, 0, this); // exception here
g.setColor(Color.GREEN);
g.fillOval(250, 250, 100, 100);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new SwagFrame());
frame.setSize(525, 525);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//frame.paint(g); // exception here
}
}
Also if you want to set the size of the image, you can pass it as argument to the .drawImage() method
g.drawImage(bg, 0, 0, width, height, this);
And one more thing, make sure the image is in the right file location.
As #MadProgrammer stated in his comments:
" Generally, top level containers aren't double buffered, where as Swing components (extending from JComponent) are. Top level containers have a number of layers placed ontop of them (layered pane, content pane and glass pane), making it very complex surface to safely paint to. Also, generally, when a child component is painted, the parent container doesn't need to be this can lead to dirty paints occurring. Also, generally, extending from JFrame is discouraged as you are never actually adding any additional functionality to it
The reason the OPs code didn't work (as you seem to have duduced) was they were passing a null value to the paint method, when means when they tried to access "g", it threw a NPE, however, the the Repaint Manager schedule it's paint request, the paint method was passed a valid Graphics refernce, meaning it was able to work properly...also, the OP isn't call super.paint, bad on them" - #MadProgrammer
Related
I've tried to research how Java's 2D rendering works, but I could never understand it. Here is the code in my main class:
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main{
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setSize(new Dimension(500,500));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("JFrame testing");
frame.setVisible(true);
Frame panel = new Frame();
frame.add(panel);
}
}
And then here is for the JPanel class:
import java.awt.Graphics;
import javax.swing.JPanel;
public class Frame extends JPanel{
private static final long serialVersionUID = 1L;
public Frame() {
Graphics g = this.getGraphics();
g.drawRect(0, 0, 100, 100);
this.paintComponent(g);
}
}
I am also getting this exception, but I'm not sure what it means:
Exception in thread "main" java.lang.NullPointerException
at Frame.<init>(Frame.java:10)
at Main.main(Main.java:18)
I'm basically just trying to draw a rectangle onto a panel to be shown on the frame I've created. I've heard about the paintComponent method, but I also don't fully understand that.
You should Never use getGraphics() of a Component.
Try below code
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(0, 0, 100, 100);
//this.paintComponent(g);
}
Edit
"why is super.paintComponent(g); called again inside the method?"
The documentation of paintComponent says it pretty well:
if you do not invoker super's implementation you must honor the opaque
property, that is if this component is opaque, you must completely
fill in the background in a non-opaque color. If you do not honor the
opaque property you will likely see visual artifacts.
I'm making a program for drawing out an Image and it seems I've made a mistake and my program just doesn't want to draw out the image. Can someone please point out the mistake for mi because i really don't see it.
package basic_game_programing;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Practise extends JPanel {
public Image image;
//#####PAINT__FUNCTION#####
public void PaintComponent(Graphics g){
super.paintComponent(g);
ImageIcon character = new ImageIcon("C:/Documents and Settings/Josip/Desktop/game Šlije/CompletedBlueGuy.PNG");
image = character.getImage();
g.drawImage(image,20,20,null);
g.fillRect(20, 20, 100, 100);
}
//######MAIN__FUCTION#######
public static void main(String[]args){
Practise panel = new Practise();
//SETTING UP THE FRAME
JFrame frame = new JFrame();
//
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.add(panel);
//SETTING UP THE PANEL
//
}
}
You're miscapitalizing paintComponent by using PaintComponent instead (note the first "P").
So Change PaintComponent to paintComponent.
Use the #Override annotation above the method to let the compiler tell you when you're making this kind of mistake.
Never read an image into a painting method since this slows down a method that needs to be fast, and makes the image be read in over and over when one read is enough.
The method should be protected not public.
Use ImageIO.read(...) to read in your image, and use resources and relative path within the jar file, rather than use files or ImageIcons.
Don't call setVisible(true) on the JFrame until after adding all components, else some might not show.
Do read the tutorials as most all of this is well explained there.
e.g.,
public class Practise extends JPanel {
private Image image;
public Practice() {
// read in your image here
image = ImageIO.read(.......); // fill in the ...
}
#Override // use override to have the compiler warn you of mistakes
protected void paintComponent(Graphics g){
super.paintComponent(g);
// never read within a painting method
// ImageIcon character = new ImageIcon("C:/Documents and Settings/Josip/Desktop/game Šlije/CompletedBlueGuy.PNG");
// image = character.getImage();
g.drawImage(image, 20, 20, this);
g.fillRect(20, 20, 100, 100);
}
}
So I have my own custom JFrame, and in it I am trying to create an auto-resizing image to be contained in my JFrame's content JPanel, frameContent. My layout manager for the JPanel is MigLayout, so I figured I would create a child of JPanel again, called ImagePanel. Here is what my ImagePanel class ended up looking like:
class ImagePanel extends JPanel{
private static final long serialVersionUID = -5602032021645365870L;
private BufferedImage image;
public ImagePanel() {
try {
image = ImageIO.read(new File(getClass().getResource("../res/Title.png").toURI()));
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, getWidth(), getHeight(), null);
}
}
Now for some reason it doesn't seem like it's actually 'working'. When I am setting up my main JFrame's content, I call:
framecontent.add(new ImagePanel(), "pos 5% 5% 95% 45%");
It's not that it's not adding the component, as with this code I'm able to get the following screen:
Notice how it outlines the area against the gray background, meaning the paintComponent(g) method is being run, and the program also isn't outputting any errors, which is strange, so that means it is finding my image, just not placing it.
Here is what my file hierarchy looks like:
Project Folder >
src >
res >
Title.png (Picture I want to retrieve)
guiwork >
Working.class (Class that is running instructions/ main)
Got it all fixed up, here is the auto-resizing result:
Notice how it outlines the area against the gray background, meaning the paintComponent(g) method is being run, and the program also isn't outputting any errors, which is strange, so that means it is finding my image, just not placing it.
Not so as a null image will throw no errors. To prove this, test your image variable in the paintComponent method and you'll likely find that it is in fact null.
i.e., try this:
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("image is null: " + (image == null)); // TODO: Delete this
g.drawImage(image, getWidth(), getHeight(), null);
}
Solution: don't change your resource to a file, but instead use the resource as is, and make sure that you're looking for it in the correct location.
Edit
Oh chit, you're drawing your image beyond the bounds of your component:
g.drawImage(image, getWidth(), getHeight(), null);
try:
g.drawImage(image, 0, 0, null);
Edit 2
What if you try a simple test program, something like:
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class TestImage {
public TestImage() throws IOException {
String path = "/res/Title.png";
BufferedImage img = ImageIO.read(getClass().getResource(path));
ImageIcon icon = new ImageIcon(img);
JOptionPane.showMessageDialog(null, icon);
}
public static void main(String[] args) {
try {
new TestImage();
} catch (IOException e) {
e.printStackTrace();
}
}
}
and be sure to place this in the same location as your current class. Does it run?
For those who might have trouble understanding HoverCraft's Answer:
Say you have a Panel in which you want to add an image that scales, the following code does that for you
import net.miginfocom.swing.MigLayout;
import javax.swing.*;
import java.awt.*;
public class MasterSlidePanel extends JPanel {
private ImageIcon imageIcon;
private JLabel image;
public MasterSlidePanel() {
super(new MigLayout("", "2% [] 2%", "2% [] 2%"));
}
#Override
protected void paintComponent(Graphics g) {
image.setIcon(new ImageIcon(imageIcon.getImage().getScaledInstance(image.getWidth(), image.getHeight(), Image.SCALE_SMOOTH)));
super.paintComponent(g);
}
public void setImage(SlidePanel panel) {
imageIcon = <Where you get the imageIcon>;
image = new JLabel();
image.setMinimumSize(new Dimension(10, 10));
image.setPreferredSize(new Dimension(10, 10));
this.add(image, "width 96%, height 96%");
}
}
We add 2 new members to the panel, image and imageIcon. The moment when the application needs to paint the panel, a scaled inscance of the imageIcon is loaded and given to image.
To prevent 0 height/width errors, we add a minimumsize to the JLabel.
Hope this helps.
Credits still go to HoverCraft Full Of Eels.
I was trying to use XOR mode in Graphics to draw a 1bit texture in color against a flat background, when I encountered a behaviour in Graphics I don't understand.
Here is an example of what I mean, isolated:
package teststuff;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class XORTest extends JFrame {
public XORTest() {
super("Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 400);
setIgnoreRepaint(true);
setResizable(false);
setVisible(true);
createBufferStrategy(2);
Graphics graphics = getBufferStrategy().getDrawGraphics();
graphics.setColor(Color.black);
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.setColor(Color.green);
graphics.fillRect(30, 40, 100, 200);
graphics.setXORMode(Color.white); // (*)
graphics.fillRect(60, 80, 100, 200);
graphics.dispose();
getBufferStrategy().show();
}
public static void main(String[] args) {
XORTest test = new XORTest();
}
}
If I uncomment the line marked with (*), two green rectangles are drawn as expected. If I leave it, nothing is drawn into the component, not even the black background or green rectangle that is drawn beforehand. Even more odd, it worked once. I had the color as Color.green instead of white before. After I changed it, it drew as expected. But when I closed the application and started it again, it didn't work anymore and it hasn't since.
Is this a bug in java? In my jre? Undocumented behaviour for Graphics? I'm on Windows and running the example on the jdk7.
Screenshots: Imgur album because it won't let me post 3 links
The third screenshot is the code as it is above, the first with (*) commented and the second is how it looked the one time it worked (I created that in GIMP because I didn't take a screenshot then).
Without a compelling reason to the contrary, it's easier and more reliable to override paintComponent() in JPanel, which is double buffered by default. With a compelling reason, follow the guidelines in BufferStrategy and BufferCapabilities. Also note,
Override getPreferredSize() to specify the preferred size of a component.
Swing GUI objects should be constructed and manipulated only on the event dispatch thread.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* #see https://stackoverflow.com/a/16721780/230513
*/
public class Test {
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new XORPanel());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private static class XORPanel extends JPanel {
#Override
public Dimension getPreferredSize() {
return new Dimension(190, 320);
}
#Override
protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
graphics.setColor(Color.black);
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.setColor(Color.green);
graphics.fillRect(30, 40, 100, 200);
graphics.setXORMode(Color.white);
graphics.fillRect(60, 80, 100, 200);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Test().display();
}
});
}
}
So ive been doing a bit of java recently and i have run into a bit of a problem. I have been playing around with 2d drawing and added an image to the project.
The problem is that when the window gets resized, it redraws and duplicates the image. I have made a little workaround, but its not ideal... So why does the image duplicate?
Before:
http://i.imgur.com/PmHRZBQ.png
(window is resized)
After:
http://i.imgur.com/bhsvVZz.png
Code
main.java
import javax.swing.JFrame;
public class main
{
public static void main(String [] args) throws InterruptedException
{
JFrame f = new JFrame("Title");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Canvas testing = new Canvas();
f.add(testing);
f.setSize(800, 600);
f.setVisible(true);
}
}
canvas.java
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Canvas extends JPanel
{
public void paintComponent (Graphics g)
{
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.BLACK);
g.fillRect(25, 25, 100, 30);
g.setColor(new Color(190,81,215));
g.fillRect(25, 68, 10, 10);
g.setColor(Color.RED);
g.drawString("Matt is da best", 100, 10);
try
{
BufferedImage image = ImageIO.read(new File("C:/face.png"));
JLabel picLabel = new JLabel(new ImageIcon(image));
System.out.println("Added pic");
add(picLabel);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Don't load the image in the paintComponent(Graphics) method! It could be declared as a class attribute and loaded in the constructor.
Don't add components in the paintComponent method either! It will trigger a repaint.
This should work more reliably..
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class main
{
public static void main(String [] args) throws InterruptedException
{
JFrame f = new JFrame("Title");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Canvas testing = new Canvas();
f.add(testing);
f.setSize(800, 600);
f.setVisible(true);
}
}
class Canvas extends JPanel
{
BufferedImage image;
Canvas() {
image = new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);
JLabel picLabel = new JLabel(new ImageIcon(image));
System.out.println("Added pic");
add(picLabel);
}
public void paintComponent (Graphics g)
{
super.paintComponent(g);
this.setBackground(Color.WHITE);
g.setColor(Color.BLACK);
g.fillRect(25, 25, 100, 30);
g.setColor(new Color(190,81,215));
g.fillRect(25, 68, 10, 10);
g.setColor(Color.RED);
g.drawString("Matt is da best", 100, 10);
}
}
Other tips
Have the Canvas return a preferred size and pack() the frame rather than set a size to it.
Start and update a Swing GUI on the EDT.
Don't give your custom class the same name as a J2SE class. That can get confusing. Maybe call it JCanvas.
Take a look at your paintComponent method...
Each time paintComponent is called, you are creating a new JLabel and adding it back to the panel...
try
{
BufferedImage image = ImageIO.read(new File("C:/face.png"));
JLabel picLabel = new JLabel(new ImageIcon(image));
System.out.println("Added pic");
add(picLabel);
}
catch (IOException e)
{
e.printStackTrace();
}
This in turn will produce another repaint request and the sync will begin all over again.
NEVER modify the state of any component from within any paint method. This will simple...blow up in your face...
You should load your image and add the label within the constructor or some other method (that paintXxx doesn't call)
The important thing to remember about painting in Swing is, you don't control it. The repaint engine may update your component at any time, with or without your interaction, based on it's own needs. You can encourage a paint by calling repaint, but there is no guarantee of when or what will be repainted.