repaint() call from the constructor - java

I'm trying to display loaded resource-image.
And it works, but only after user resized the window.
import javax.swing.*; import java.awt.*;
class MyMainWindow extends JFrame{
Image img;
public MyMainWindow(){
setSize(300,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
img = MyResourceLoader.getImage("res.jpg");
repaint();
setVisible(true);
}
#Override
public void paint (Graphics g){
super.paint(g);
if (img != null)
g.drawImage(img, 0,0, null);
}
}
public class ResourcesProgram {
public static void main(String[] args) {
new MyMainWindow();
}
}
Why the constructor's repaint() call doesn't work?
And what should I do to show this image from the start?
Resource Loader class:
import java.awt.*;
public class MyResourceLoader{
static MyResourceLoader rl = new MyResourceLoader();
static public Image getImage(String fileName){
return Toolkit.getDefaultToolkit().getImage(rl.getClass().getResource("images/" + fileName));
}
}

Okay, so a bunch of things...
First...
Because the window has not yet been realised on the screen (attached to a native peer), there is nothing for repaint to paint...
Second...
Toolkit.getDefaultToolkit().getImage(rl.getClass().getResource("images/" + fileName));
Passes the actually loading of the image to a background thread, meaning that while you get a non-null value returned, the actual image data might not have been fully loaded.
You should consider using ImageIO.read instead, it will only return AFTER the image data has been fully loaded and will also throw an exception if something goes wrong (unlike Toolkit.getImage). See Reading/Loading an Image for more details...
Third...
All Swing components implement the ImageObserver interface, this means when you call Graphics#drawImage, you should pass the object as the ImageObserver parameter
g.drawImage(img, 0,0, this);
This allows the component to monitor updates from the loading of the image and reschedule repaint as required.
Fourth...
You should avoid overriding paint of top level containers, apart from not been double buffered, there is a bunch of components between the frame and the user, which can interfere with the painting process.
Instead, you should extend from something like JPanel and override it's paintComponent method, performing your custom painting there. You should then add this panel to an instance of JFrame (or some other component as needed)
Take a look at Painting in AWT and Swing and Performing Custom Painting for more details
Fifth...
You should be making sure that you create and modify your UI only from within the context of the Event Dispatching Thread, see Initial Threads for more details

Paint is immediately called when the code is initialized. To see this, get rid of repaint(); in your constructor, and add in your paint method, g.drawLine(0, 0, 300, 300);

Try this, you may have to add some imports:
import javax.swing.*; import java.awt.*;
class MyMainWindow extends JFrame{
Image img;
public MyMainWindow(){
setSize(300,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
img = MyResourceLoader.getImage("res.jpg");
setVisible(true);
}
#Override
public void paint (Graphics g){
super.paint(g);
if (img != null)
g.drawImage(img, 0,0, this);
}
}
public class ResourcesProgram {
public static void main(String[] args) {
new MyMainWindow();
}
}
Resource Loader:
import javax.swing.*; import java.awt.*;
public class MyResourceLoader {
public static Image getImage(String fileName){
ImageIcon icon = new ImageIcon(getClass().getResource(fileName));
return icon.getImage();
}
}

Related

Java animation freeze

I am trying to write a simple 2d animation engine in Java for visualizing later programming projects. However, I am having problems with the window refresh. On running, the frame will sometimes display a blank panel instead of the desired image. This begins with a few frames at a time at apparently random intervals, worsening as the program continues to run until the actual image only occasionally blinks into view. The code for processing each frame is run, but nothing in the frame is actually displayed. I believe the problem may come from my computer more than my code (certainly not from bad specs though), but am not sure. Help much appreciated.
Three classes. Code here:
package animator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.AudioClip;
public class APanel extends JPanel
{
public APanel(int l, int h){
setPreferredSize(new Dimension(l,h));
setLocation(80, 80);
setVisible(true);
setFocusable(true);
}
public Graphics renderFrame(Graphics g){
return g;
}
public void paintComponent(Graphics g) {
requestFocusInWindow();
renderFrame(g);
}
}
package animator;
import java.awt.*;
public class Animator extends APanel
//extending the APanel class allows you to code for different animations
//while leaving the basic functional animator untouched
{
public static final int SCREEN_X = 700;
public static final int SCREEN_Y = 700;
int frameNum;
public Animator() {
super(SCREEN_X, SCREEN_Y);
frameNum = 0;
}
public Graphics renderFrame(Graphics g) {
frameNum++;
g.drawString(""+frameNum,5,12);
return g;
}
}
package animator;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;
public class Runner {
int framerate = 30;
Animator a = new Animator();
JFrame j = new JFrame();
public Runner(){
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.add(a);
start();
j.setSize(a.getPreferredSize());
j.setVisible(true);
}
public void start() {
Timer t = new Timer(1000/framerate, new ActionListener() {
public void actionPerformed(ActionEvent e){
j.getComponent(0).paint(j.getComponent(0).getGraphics());
//The following line of code keeps the window locked to a preferred size
// j.setSize(j.getComponent(0).getPreferredSize());
}
});
t.start();
}
public static void main(String[] args){
Runner r = new Runner();
}
}
There are some serious mistakes in your code which could be the cause or a factor of your problem...
j.setSize(a.getPreferredSize()); is irrelevant, simply use JFrame#pack, you get better results as it takes into account the frame decorations
j.setSize(j.getComponent(0).getPreferredSize()); use JFrame#setResizable and pass it false instead...
NEVER do j.getComponent(0).paint(j.getComponent(0).getGraphics()); this! You are not responsible for the painting of components within Swing, that's the decision of the RepaintManager. Just call j.repaint()
super(SCREEN_X, SCREEN_Y);...just override the getPreferredSize method and return the size you want.
setLocation(80, 80); irrelevant, as the component is under the control of a layout manager
setVisible(true); (inside APanel)...Swing components are already visible by default, with the exception of windows and JInternalFrames
And finally...
public void paintComponent(Graphics g) {
requestFocusInWindow();
renderFrame(g);
}
There is never any need for this method to be made public, you NEVER want someone to be able to call it, this will break the paint chain and could have serious ramifications in the ability for the component to paint itself properly.
You MUST call super.paintComponent before performing any custom painting, otherwise you could end up with all sorts of wonderful paint artifacts and issues
Never modify the state of a component from within a paint method...while it "might" not be an immediate issue calling requestFocusInWindow(); within your paintComponent could have side effects you don't know about...
Instead, it should look more like...
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
renderFrame(g);
}

Is there ever a reason drawImage won't actually draw an image?

private void renderLevelBackground(Graphics2D g2) {
Image backgroundImage = model.getBackgroundImage();
g2.drawImage(backgroundImage, 0, 0, null);
}
This is my code, I know that the backgroundImage is loaded properly because if I getWidth/Height it gives me the correct values. Is there anyway to test if it's the image or if it's the method somehow? It just won't draw on my screen despite the fact that I've used the drawImage method many times in this project already, all of which work flawlessly.
Thanks
"Is there ever a reason drawImage won't actually draw an image?"
Possibility, there is no image due to a bad path provided. Use ImageIO.read() which will cause an exception if the image file is not found. For example
public class ImagePanel extends JPanel {
private BufferedImage image;
public ImagePanel() {
try {
image = ImageIO.read(getClass().getResource("/resources/image.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
If the image is not found, it will throw an IO exception.
Possibility, the panel to which you are drawing, has no preferred size (0 x 0), and you are adding it a container wit layout that respects preferred sizes, so the ImagePanel will not be able to show the image. For example
public class ImagePanel extends JPanel {
protected void paintComponent(...) {
...
}
JPanel panel = new JPanel(); // default FlowLayout that respects preferred sizes
panel.add(new ImagePanel());
To fix this, you can override the gerPreferredSize() of the ImagePanel
public class ImagePanel extends JPanel {
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
}
Possibility, you are not calling your method within the graphics context of the paint[Component] method.
protected void paintComponent(Graphics 2d) {
super.paintComponent(g);
Grapchics2D g2 = (Graphics2D)g;
renderLevelBackground(g2);
}
Other than that, these are all just guesses, and you should provide some more code code (preferably an MCVE) to help us better help you with the problem.
Possible Alternative to your approach. It seems like (from the little code snippet and it method signature semantics) you want to change the background image, when a level have changed. Consider using setBackgroundImage(BufferedImage image) method in your panel class, when the image is drawn. You can then set the background, whenever need be. Something like
public class BackgroundPanel extends JPanel {
private BufferedImage backgroundImage;
public void setBackgroundImage(BufferedImage image) {
this.backgroundImage = image;
repaint();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Grpahics2D g2 = (Graphics2D)g;
if (image != null) {
g2.drawImage(backgroundImage, 0, 0, this);
}
}
}
So whenever you call setBackgroundImage, the image will change, and be repainted.
You may need to explicitly tell the JComponent that you are painting onto that it needs to be redrawn with revalidate() and repaint()

How to access repaint from the main?

I am working on a Java program that basically renders an image from a file source and then paints that image onto a panel (which is on a frame).
Now what I am able to do is invoke a line of code of the form
printpanel.getGraphics().drawImage(myimage.globalimage, 0,0, null);
where myimage is a class that contains the image.
As many of you know, this only prints the image 1 time, and should I resize the frame, the image disappears.
Now the way to fix this would be to put the line into the repaint method, but i'm in the main method right now so how do I access the definition of the repaint method and change it from within the main method?
Thanks!
=====================================================================================================
My code:
MAIN CLASS:
package imagetester;
import java.awt.image.*;
import javax.swing.*;
import javax.imageio.*;
import java.awt.Graphics2D.*;
import java.io.*;
public class Imagetester
{
public static void main(String[] args)
{
JFrame printframe = new JFrame("The drawing frame");
JPanel printpanel = new JPanel();
printframe.setSize(700,700);
printpanel.setSize(700,700);
printframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
printframe.add(printpanel);
printpanel.setVisible(true);
printframe.setVisible(true);
Imageobject myimage = new Imageobject();
try
{
myimage.setImage("word.jpg");
}
catch(IOException e)
{
System.out.println("the image failed!");
}
printpanel.getGraphics().drawImage(myimage.globalimage, 0,0, null);
printpanel.repaint();
System.out.println("hi");
}
}
myimage class:
package imagetester;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Imageobject
{
BufferedImage globalimage;
public void setImage(String filename) throws IOException
{
globalimage = ImageIO.read(new File(filename));
}
public void Imagebject()
{
}
}
I'm not sure I understand fully, but if what you want to do is display a panel with an image inside a window, you should subclass JPanel (or whatever other panel you'd like), and override the paintComponent method to paint the image. Something along the lines of:
public class ImagePanel extends JPanel {
private Image image;
public ImagePanel(Image image) {
this.image = image;
}
#Override
protected void paintComponent(graphics g) {
g.drawImage(image, 0, 0, this);
}
}
Now what i am able to do is invoke a line of code of the form
printpanel.getGraphics().drawImage(myimage.globalimage, 0,0, null);
No don't do that. Your printPanel should already have it's paintComponent method, with the a drawImage in it.
Image image;
...
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
g.drawImage(image, ..., this); // this is the ImageObserver. Should not be null
}
}
Then you can just have a setter for it if you want to change it
public void setImage(Image image) {
this.image = image;
repaint();
}
Just call that method with the image you want to change
EDIT
There's no way around it. You need to #Override the paintComponent method of the JPanel. When you resize the frame, repaint() will automatically be called, leaving the image there. The image should be drawn in the paintComponent method. You can have the panel's constructor take an Image argument if you want to instantiate that way, with the image from the ImageObject
As many of you know, this only prints the image 1 time, and should I resize the frame, the image disappears.
What is your requirement when the frame resizes?
Do you paint the image at its original size? If so then just use a JLabel with an ImageIcon and add the label to the frame
Do you want the image to scale with the size of the frame? Then you need to do custom painting. You can create a custom component as demonstrated in other answers. Or you can use a JLabel with a Stretch Icon which will stretch to fill the space available to the label.

Graphics dont appear

Whatever I do, I can not display rectangle/line/oval on the screen. I checked other sources where they paint graphics, but when I even execute those codes, I don't get any graphics displayed on the windows. Below is the example from the text book.
import java.awt.*;
import javax.swing.*;
class PlotGraph
{
public static void main (String [] args) {
JFrame win;
Container contentPane;
Graphics g;
win = new JFrame("testing");
win.setSize(300,200);
win.setLocation(100,100);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setVisible(true);
contentPane = win.getContentPane();
g = contentPane.getGraphics();
g.drawRect(10, 30, 50, 50);
}
}
Ouch. You should change your text book then. First of all, all the accesses to Swing components must be done in the event dispatch thread.
Second, you should not get the graphics of a component and paint on it. Instead, you should extend a JComponent or JPanel, override its paintComponent(Graphics) method, and paint using the Graphics object passed as argument (and which is in fact a Graphics2D instance).
That's not how graphics work in Swing.
You need to add a component to your frame, not just draw on it. You never want to draw directly on the frame. The reason why it's not doing anything is because your drawing code is being overridden.
If you want your component to have custom drawing code, make a subclass of JComponent and override the paintComponent(Graphics) method. An example of how you should do this is as follows:
import java.awt.*;
import javax.swing.*;
class PlotGraph {
public static void main(String[] args) {
JFrame win;
win = new JFrame("testing");
win.setSize(300, 200);
win.setLocation(100, 100);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.setVisible(true);
win.setContentPane(new MyComponent());
}
}
class MyComponent extends JComponent {
#Override
public void paintComponent(Graphics g) {
g.drawRect(10, 30, 50, 50);
}
}
I would highly encourage you to check out the Java GUI tutorial online.

Swing overriding object's paintComponent

If I have a JPanel object that I can't modify, is there a way I can modify the paintComponent method of it without using injection?
One approach I was thinking of was getting the JPanel's Graphics object, passing it to paintComponent(), performing operations on this Graphics object, and finally painting that in my custom JPanel. The problem with this, is I need to be able to do this every time the original JPanel's paintComponent() is called.
I don't need to replace what's in paintComponent(), I just need to add on to it.
For example:
JFrame frame = null;
for (Frame f : JFrame.getFrames()) {
if (((JFrame) f).getTitle().equals("Title")) {
JPanel panel = null;
// ... Cycle through all components and get the one that's a JPanel
// I want to use ColorConvertOp to make panel greyscale
}
}
One approach would be to use the Decorator Pattern to wrap the existing class. Your decorator can then implement paintComponent to first delegate to the original component and after that paint on top of it. For this approach you need to actually gain control over the creation of the components, or you need to replace them after the component hierarchy has been created (using getComponents() of the parent container to find the components to be altered).
I think one possibility is to use a GlassPane and position it exactly over your JPanel (maybe let it follow the panel using listeners if the panel changes its location). Then you can simply draw your stuff in the glasspane and it will be overlayed.
Of course this is not really elegant... But I don't see any possibility to change the paintComponents behaviour of an already existing instance without injection. (Proof me wrong, Java geeks of this world! :P)
I guess this would complete #Durandal's answer:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestPanels {
private static final boolean GRAY_SCALE = true;
protected void initUI() {
final JFrame frame = new JFrame(TestPanels.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel unmodifiablePanel = new JPanel(new GridBagLayout());
JLabel label = new JLabel("Some unmodifiable test label");
unmodifiablePanel.add(label);
unmodifiablePanel.setBackground(Color.GREEN);
JPanel wrappingPanel = new JPanel(new BorderLayout()) {
private ColorConvertOp op = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
private BufferedImage image;
#Override
public void paint(Graphics g) {
if (!GRAY_SCALE) {
super.paint(g);
return;
}
BufferedImage bi = getImage();
if (bi != null) {
Graphics big = bi.createGraphics();
super.paint(big);
big.dispose();
bi = op.filter(bi, null);
g.drawImage(bi, 0, 0, null);
}
}
protected BufferedImage getImage() {
if (image == null) {
if (getWidth() > 0 && getHeight() > 0) {
image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
}
} else if (image.getWidth() != getWidth() || image.getHeight() != image.getHeight()) {
image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
}
return image;
}
};
wrappingPanel.add(unmodifiablePanel);
frame.add(wrappingPanel);
frame.setSize(200, 200);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TestPanels().initUI();
}
});
}
}
You can turn the GRAY_SCALE flag to false, to see how it renders normally.
If you had the ability to modify the construction of your class, you could extend that class and then call super.paintComponent(g) in your extended class. Example:
public class NewPanel extends OldPanel{
public void paintComponent(Graphics g){
super.paintComponent(g);
// Insert additional painting here
}
}
In this case you will need a new class in order to build onto the existing class's painting.
What this does is execute everything that was done in the parent class's painting, and give you the option to do more (which seems to be what you're looking for).
EDIT
BUT... given that you don't have access to this, your options get more limited. If the panel uses a null layout manager, you could add a child jpanel who paints over the parent (a layout manager would restrict the amount of area the child could paint over though). This is a long shot.
You can also use reflection to but about the only option you've got (besides byte code injection). This seems about equally ugly as byte code injection - here's a decent overview of what you'd be looking to do: Java reflection: How do I override or generate methods at runtime?
My personal preference is to decompile the class, modify it the way you want, recompile it and insert it back into the original jar. This will probably void some warrenties, licenses and get you in other trouble... but at least it's maintainable.

Categories

Resources