This question already has an answer here:
paintComponent is not being called in JPanel
(1 answer)
Closed 3 years ago.
Sorry for a probably already solved problem, but I've searched everywhere and cannot find a solution. I've just found that no matter what, paintComponent is not called no matter where I put repaint()
I have already tried putting it in several different methods and tried to call it from different areas but no matter what, it seems to never be called.
import java.awt.*;
import java.awt.event.*;
import java.awt.Component;
import javax.swing.*;
import javax.swing.BoxLayout;
import javax.swing.event.*;
import java.awt.event.KeyEvent;
import java.util.Scanner;
public class LevelOne extends JPanel implements KeyListener
{
int width = 0;
int height = 0;
int bx = 0;
int hx = 0;
int by = 0;
int hy = 0;
Image joe = new ImageIcon("upgrademan.png").getImage();
ImagePanel2 panel2 = new ImagePanel2(new ImageIcon("levelone.png").getImage());
JFrame frame = new JFrame ("Level One");
public LevelOne()
{
frame.getContentPane().add(panel2);
Game game1 = new Game();
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.WHITE);
frame.pack();
frame.setVisible(true);
levelOne();
requestFocus();
}
public void levelOne()
{
repaint(); // this doesn't call paintComponent below?
}
public void paintComponent(Graphics g)
{
super.paintComponent(g); //draw background color
System.out.println("this enters paintComponent");
g.drawImage(joe,100,100,200,200, null); // this doesn't seem to be drawing
requestFocus();
}
public void keyPressed(KeyEvent e) // all they keyListener methods
{
}
public void keyTyped(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
}
}
class ImagePanel2 extends JPanel { // this entire class simply exists to call in order to set a picture as a background
Image img;
public ImagePanel2(String img) { // just sets img in method to class img variable
this(new ImageIcon(img).getImage());
}
public ImagePanel2(Image img) { // sets size of picture
this.img = img;
Dimension dims = new Dimension(600,600);
setPreferredSize(dims);
setMinimumSize(dims);
setMaximumSize(dims);
setSize(dims);
setLayout(null);
}
public void paintComponent(Graphics g) { // draws image
g.drawImage(img, 0, 0, this);
}
}
I expected the ImagePanel class to print a background, which it did, but I also expected the image called joe to print, which it did not.
The only place you display "joe" is in the paintComponent() method of instances of LevelOne. But you never add a LevelOne instance to your frame, or to any panel in that frame.
Related
I've made some code for my tile, where I draw 2 objects, but I can't load a background in the tile as it extends JFrame.
What am I doing wrong?
I used buffered image to read the picture, made it suitable for any screen size, set it as a JLabel, but still isn't working.
Square1 and obstacle are graphics of rectangles imported, one acting as the player moving and the other as an obstacle respectively.
public class BasicTwoPlayer extends JFrame implements Runnable {
static Square1 p1 = new Square1();
static Square2 p2 = new Square2();
static obstacle o1 = new obstacle();
static Thread p1t;
static Thread p2t;
static Thread o1t;
KeyADAPT a = new KeyADAPT();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
final int widthScreen = screenSize.width;
final int heightScreen = screenSize.height;
public BasicTwoPlayer() {
try {
BufferedImage backgroundImage = ImageIO.read(new File("P://My Pictures//background1.jpg"));
JLabel background = new JLabel(new ImageIcon(backgroundImage));
Image scaleBackground = backgroundImage.getScaledInstance(widthScreen, heightScreen, Image.SCALE_SMOOTH);
ImageIcon imageIcon = new ImageIcon(scaleBackground);
setContentPane(new JLabel(imageIcon));
addKeyListener(a);
setSize(new Dimension(widthScreen, heightScreen));
setUndecorated(true);
setBackground(Color.WHITE);
setLayout(null);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
p1t = new Thread(p1);
p2t = new Thread(p2);
o1t = new Thread(o1);
p1t.start();
p2t.start();
} catch (IOException ex) {
Logger.getLogger(BasicTwoPlayer.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void paint(Graphics g) {
background.paint(g);
}
public void draw(Graphics g) {
p1.draw(g);
o1.draw(g);
repaint();
}
public static void main(String[] args) {
BasicTwoPlayer sf = new BasicTwoPlayer();
Thread mt = new Thread(sf);
mt.start();
t1.start();
}
I just want an answer like everyone else :/
You'd be surprised how much I'd prefer to learn how to answer my own questions ;)
You will need to understand:
Performing Custom Painting
Painting in AWT and Swing
It wouldn't hurt to understand:
Reading/Loading an Image
Laying Out Components Within a Container
"How to add a background image" ...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.setContentPane(new BackgroundPane());
frame.add(new OverlayPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class OverlayPane extends JPanel {
public OverlayPane() {
setOpaque(false);
setLayout(new GridBagLayout());
JLabel label = new JLabel("You'd be suprised who far a little knowledge will take you");
label.setForeground(Color.WHITE);
add(label);
}
}
public class BackgroundPane extends JPanel {
private BufferedImage backgroundImage;
public BackgroundPane() {
setLayout(new BorderLayout());
try {
backgroundImage = ImageIO.read(BackgroundPane.class.getResource("background.jpg"));
} catch (IOException ex) {
ex.printStackTrace();
}
}
#Override
public Dimension getPreferredSize() {
return backgroundImage == null ? new Dimension(200, 200) : new Dimension(backgroundImage.getWidth(), backgroundImage.getHeight());
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (backgroundImage == null) { return; }
int x = (getWidth() - backgroundImage.getWidth()) / 2;
int y = (getHeight() - backgroundImage.getHeight()) / 2;
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawImage(backgroundImage, x, y, this);
g2d.dispose();
}
}
}
You are going to want to learn about:
Concurrency in Swing
How to Use Swing Timers
How to Use Key Bindings
based on you the direction of this and all your other questions to date. They will help you avoid common pitfalls.
I don't doubt you'll have issues coming to grips with these concepts and I encourage you to have a play and if and when, post a question out them, specifically.
Still trying to grasp how classes and methods work in Java. To experiment, I tried to create a graphics class, with a void draw box method inside. Then, I try to call that method in the main method to try to draw those boxes. I'm getting "cannot be resolved to variable" errors which I believe means the main class can't see my other class for some reason?
Boxymain.java:
import java.awt.*;
import javax.swing.JFrame;
public class Boxymain extends Canvas {
public static void main(String[] args){
BoxyMethod c = new BoxyMethod();
c.drawBox(window, Color.RED, 200, 300);
JFrame win = new JFrame("Boxy Main");
win.setSize(800,600);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Boxymain canvas = new Boxymain();
win.add(canvas);
win.setVisible(true);
}
}
BoxyMethod.java:
import java.awt.*;
import javax.swing.JFrame;
public class BoxyMethod {
public void drawBox(Graphics window, Color c, int x, int y){
window.setColor(c);
window.fillRect(x, y, 100, 100);
window.setColor(Color.WHITE);
window.fillRect(x+10,y+10,80,80);
}
}
Error text: "window cannot be resolves to a variable."
The error message is telling you exactly what is wrong. You're passing in a window variable into the drawBox method, but you don't declare or initialize such a variable in the main method before doing so, and so this cannot be done in Java.
BoxyMethod c = new BoxyMethod();
// *** window variable below is used but never declared prior to use
c.drawBox(window, Color.RED, 200, 300);
More importantly though, you're not doing Swing drawing correctly.
Instead, you should create a class that extends JPanel, give it a paintComponent(Graphics g) method override, and draw in that method. Then place that JPanel in a JFrame and display the JFrame. Please check out the Performing Custom Painting Swing graphics tutorial for more detail on how to do Swing graphics.
As an aside, do not follow that tutorial that you've linked to as it is 30 years out of date.
For example:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.*;
public class BoxyTest {
private static void createAndShowGui() {
JFrame frame = new JFrame("Boxy Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new BoxyPanel(200, 300));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
#SuppressWarnings("serial")
class BoxyPanel extends JPanel {
private static final int PREF_W = 800;
private static final int PREF_H = 650;
private int myX;
private int myY;
public BoxyPanel(int myX, int myY) {
this.myX = myX;
this.myY = myY;
}
#Override // so my JPanel will be big enough to see
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
#Override
protected void paintComponent(Graphics g) {
// call super method so that the JPanel can do housekeeping painting
super.paintComponent(g);
g.fillRect(myX, myY, 100, 100);
g.setColor(Color.WHITE);
g.fillRect(myX + 10, myY + 10, 80, 80);
}
}
I don't understand why the picture painted on my background isn't resized on JFrame resize.
Basically I create a JFrame and a JPanel inside this frame.
Then I set my background picture (animated gif) with the method "setBackground" that creates an object of another class. In this new class called "ImagePanel" I draw the picture and paint another smaller picture over the background picture. The result is ok unless I resize the JFrame.
The background picture "follows" the resize, while the smaller picture remains at the beginning point. I draw both the pictures with the paintComponent(), and I set the dimension of the pictures on the base of the Width and Height of the JFrame, but it doesn't seem to work.
I added an action listener too, to listen to the resize on the JFrame. When it listens the resize it calls "repaint()", but it doesn't work (I also tried "revalidate()" both on the JFrame and the JPanel without success).
It seems that when you enlarge the JFrame almost covering all the screen it has an update, on a fixed point the smaller picture is "updated" but in a wrong way, and only if the JFrame is very large.
Here is the code. The "GameWindow" class:
import java.awt.BorderLayout;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class GameWindow extends JFrame {
private JPanel mapPicture = new JPanel();
public GameWindow(){
super("WELCOME!!!");
setSize(768, 768);
addComponentListener(new ResizeListener(this));
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setBackground();
setVisible(true);
}
private void setBackground() {
mapPicture = new ImagePanel("map.gif", this);
add(mapPicture, BorderLayout.CENTER);
mapPicture.setLayout(new BorderLayout());
}
public class ResizeListener implements ComponentListener {
private JFrame parentFrame;
public ResizeListener(JFrame parentFrame) {
this.parentFrame = parentFrame;
}
public void componentHidden(ComponentEvent e) {}
public void componentMoved(ComponentEvent e) {}
public void componentShown(ComponentEvent e) {}
public void componentResized(ComponentEvent e) {
repaint();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new GameWindow2();
}
});
}
}
And here is the "ImagePanel" class:
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImagePanel extends JPanel {
private ImageIcon background;
private JFrame parentFrame;
private ImageIcon smallerImage = new ImageIcon(getClass().getResource("car.png"));
public ImagePanel(String imgPath, JFrame parentFrame) {
this.parentFrame = parentFrame;
background = new ImageIcon(getClass().getResource(imgPath));
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.drawImage(background.getImage(), 0, 0, getWidth(), getHeight(), this);
g2d.drawImage(smallerImage.getImage(), 119*((parentFrame.getWidth()/768)), 172*((parentFrame.getHeight())/768), 32*((parentFrame.getWidth())/768), 32*((parentFrame.getHeight())/768),this);
}
}
I don't understand why the smaller picture isn't redrawn with the new proportions.
PS: I can't use a layout manager to set the position of the second picture, because I will have a lot of objects (in the future) to put in a casual way on the map (and possibly animate them).
it is because you are using integer arithmetic and dividing before multiplying :119*(parentFrame.getWidth()/768)
instead you floating point arithmetic and cast to an int : (int)(119.0*(parentFrame.getWidth()/768.0))
I have created a KeyAdapter class within my JPanel class, and I tried debugging the click using System.out.println(String par1);
However it doesnt seem to work...
Here is the class:
package net.ryanshah;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Menu extends JPanel
{
private GameFrame gf;
private Image bg = new ImageIcon("res/bg.png").getImage();
public Menu(final GameFrame parent) {
this.gf = parent;
int width = parent.getWidth();
int height = parent.getHeight();
setFocusable(true);
addKeyListener(new MenuOperator());
setBackground(Color.black);
setLayout(null);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
g2d.drawImage(bg, 0, 0, null);
}
public class MenuOperator extends KeyAdapter {
#Override
public void keyPressed(KeyEvent event) {
int keyCode = event.getKeyCode();
if(keyCode == KeyEvent.VK_SPACE) {
System.out.println("LOL");
}
}
}
}
Does anyone know what the problem might be? I have set the key handler in the frame as well as setting the frame to be focusable..
Thanks in advance
~RyanS.
Things not considered right in the code (IMHO) :
The way you accessing the images in your code. Consider ImageIO, over using the constructor of ImageIcon, as the former will notify you, in case something goes wrong. For more info, please refer, Loading Image Icon Exception, for more insight.
KeyListeners are for AWT, Swing on the other hand uses KeyBindings.
Before, setting background on any JComponent prefer to first set the opaque property of the said JComponent to true. Since, opaque property is usually dependent on LookAndFeel used.
Avoid the use of setLayout(null), as much as possible. Please walk through the first paragraph of Absolute Positioning for more information :-)
THe use of ImageObserver as already mentioned in the comments.
This blog Motion Using the Keyboard by #camickr, will again add loads to your knowledge, as to why KeyListeners are not considered a better option and why KeyBindings is more suited for such needs :-)
Please have a look at this example :
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class TestMenu extends JPanel {
private Image bg;
public TestMenu() {
try {
bg = ImageIO.read(
new URL("http://i.imgur.com/Aoluk8n.gif"));
} catch (Exception e) {
e.printStackTrace();
}
getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "SPACE key");
getActionMap().put("SPACE key", keyAction);
setOpaque(true);
setBackground(Color.black);
}
private Action keyAction = new AbstractAction() {
#Override
public void actionPerformed(ActionEvent ae) {
System.out.println("LOL");
}
};
/*
* Make this a customary habbit to override
* this method for the JPanel/JComponent,
* when ever you extend any of the above classes
*/
#Override
public Dimension getPreferredSize() {
return (new Dimension(200, 200));
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int w = getWidth();
int h = getHeight();
/*
* Here the last part of the drawImage is the
* ImageObserver, here it is a good practise
* to keep the instance of the JPanel on which
* we drawing the image to be notified, till the
* end, till the image loading is not complete
*/
g2d.drawImage(bg, 0, 0, this);
}
private void displayGUI() {
JFrame frame = new JFrame("Painting Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setContentPane(this);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new TestMenu().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
Setting your panel focusable is not enough - panel cannot get focus on its own - you have to request the focus on some kind of event. For example you can add a mouse listener to your panel and call panel.requestFocusInWindow() when you click on it.
Key listener will work only if component on which you add it has focus.
Trying ti change the BG image of a jpanel but i cant call poaint on any normal method, it works great when i'm building the constructor but i don't want to rebuild the constructor.
....
Sort of found a solution by putting a label in my center frame and calling setIcon but i need to be able to pull the relelevant info so i need to find a way to store a value into my Jtoggle button (the id of the Race or class so i can fetch it's picture and change the icon)
thoughts? Everything compiles outside the iff statement which is my sticking point
RaceButtons_lft[i] = new JToggleButton();
RaceButtons_lft[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JToggleButton cb = (JToggleButton)ae.getSource();
for (int j=0; j<MyRaceArray.size(); j++)
{
if (MyRaceArray.get(j).getraceID() == combo_contents.getIndex()){//here is my sticking point, i need to find a way to match MyRaceArray's getRaceID to some value saved withthe Toggle button
final ImageIcon BGCSMs = ScaledImageIcon("Fantasy_Landscape_01.jpg", "Profile Pic", (468-(60*2)), 285);
picLabel.setIcon(BGCSMs);
}//if
}//for
}//action performed;
});//button add action listener
Calling
super.paintComponent(..)
will probably - depending on the super class - fill the component with the background color.
public void paintComponent(Graphics g) {
// Let UI Delegate paint first, which
// includes background filling since
// this component is opaque.
super.paintComponent(g);
g.drawString("This is my custom Panel!",10,20);
redSquare.paintSquare(g);
}
(see A Closer Look at the Paint Mechanism). You don't need repaint(..) in this case.
You could be suffering from a number of problems, which we can't see because we don't have enough context...
You could have a reference issue, instead of trying to repaint the component on the screen, you've inadvertently gotten the wrong reference...
You could be shadowing your variables...
You could be painting to a opaque component...
Assuming that the code you have posted is linear (ie, it appears in you code in this exact order or close enough to it), I can see one possible problem...
ImageIcon RCicon = createImageIcon(temp_race.getActiveHeadshot(), temp_race.getRaceNameString(race.getraceID()));
Image RCimg = RCicon.getImage();
RCimg = RCimg.getScaledInstance((468-(60*2)), 285, java.awt.Image.SCALE_SMOOTH);
portraitCenterOptions.setBackground(Color.White){
protected void paintComponent(Graphics h)
{
//...//
// There is no way that this reference can be valid...
// The image created above will only have a local reference unto itself
// suggestion that you're shadowing your variables...
final ImageIcon bodypicSM = new ImageIcon(RCimg);
//...//
}
};
But without a working example, it's impossible to know...
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.text.DateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ChangeBackground {
public static void main(String[] args) {
new ChangeBackground();
}
public ChangeBackground() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
final PaintPane pane = new PaintPane();
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(pane);
JButton change = new JButton("Change");
change.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
pane.changeBackground();
pane.repaint();
}
});
frame.add(change, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class PaintPane extends JPanel {
private BufferedImage bg;
private int changes = 0;
public PaintPane() {
changeBackground();
}
public void changeBackground() {
bg = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bg.createGraphics();
FontMetrics fm = g.getFontMetrics();
g.setColor(getForeground());
String[] text = {
"I've been changed " + changes + " times",
"Last changed at " + DateFormat.getDateTimeInstance().format(new Date())};
int y = (200 - (fm.getHeight() * 2)) / 2;
for (String value : text) {
int x = (200 - fm.stringWidth(value)) / 2;
g.drawString(value, x, y + fm.getAscent());
y += fm.getHeight();
}
g.dispose();
changes++;
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x = (getWidth() - bg.getWidth()) / 2;
int y = (getHeight() - bg.getHeight()) / 2;
g.drawImage(bg, x, y, this);
}
}
}
Call repaint() on the component after you have finished your changes to the background
so ultimately tried a few thing and just got lazy, added a label in the center and called "SetIcon, does what I need it to do, thanks for the ideas though.