JComponent not drawing inside JPanel - java

Simple problem for Swing regular costumers.
The idea is to load an image and use it as a background of a JPanel through a JComponent object. It seems to load just fine as the image information from toString() method is loading properly and the paintComponent() method is running, but for some reason it is not rendering correctly inside the JFrame leading to an empty frame. Here's the code:
RicochetFrame.java
public class RicochetFrame extends JFrame {
RicochetStartPanel startPanel;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
RicochetFrame window = new RicochetFrame();
window.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public RicochetFrame() throws IOException {
startPanel = new RicochetStartPanel();
this.getContentPane().add(startPanel);
this.setBounds(0, 0, 500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this.pack();
this.setVisible(true);
}
}
RicochetStartPanel.java
public class RicochetStartPanel extends JPanel {
RicochetStartPanel() throws IOException {
BufferedImage myImage = ImageIO.read(new File("frame_bg.jpg"));
this.add(new RicochetImagePanel(myImage));
this.setVisible(true);
this.validate();
this.repaint();
}
}
RicochetImagePanel.Java
public class RicochetImagePanel extends JComponent {
private Image image;
public RicochetImagePanel(Image image) {
this.setVisible(true);
this.image = image;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
}

Your RicochetImagePanel is sizing itself to its preferred size => [0, 0]. Override its getPreferredSize() to return the dimensions of the image (if not null). Better still, why not simply display the image as an ImageIcon in a JLabel?

Related

PaintComponent() not called despite setPreferredSize of JPanel

I am trying to understand why the following short piece code does not work.
I understand that when there are no Layout or the size of the component is 0, the paint component method isn't called.
But this isn't the case here.
Can you explain why I can't set the background for this?
public class Login extends JPanel {
private BufferedImage bgImage;
public Login() {
super();
initImages();
setLayout(new BorderLayout());
setPreferredSize(new Dimension(600, 600));
add(new JLabel("Hi"), BorderLayout.CENTER);
}
private void initImages() {
try {
bgImage = ImageIO.read(new File("images/login.jpg"));
System.out.println("image loaded");
} catch (IOException e) {
e.printStackTrace();
System.out.println("image not loaded");
}
}
#Override
public void paintComponents(Graphics g) {
super.paintComponents(g);
g.drawImage(bgImage, 0, 0, null);
System.out.println("repaint");
}
public static void createAndShowGui() {
JFrame frame = new JFrame();
Login login = new Login();
frame.add(login, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
If you want this to work, then you will need to change...
#Override
public void paintComponents(Graphics g) {
super.paintComponents(g);
g.drawImage(bgImage, 0, 0, null);
System.out.println("repaint");
}
to something more like...
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bgImage, 0, 0, this);
}
paintComponent is responsible for painting the "bottom" layer of the component, paintComponents is responsible for painting the children

Draw JPanel into JPanel

I have two classes that extend JPanel: MapPanel and CityPanel. I am trying to draw a CityPanel into a MapPanel but nothing appears. I do not understand why if I add a JButton in the same way it will be displayed perfectly.
Here's the code:
public class PanelMap extends JPanel {
public PanelMap() {
CityPanel city = new CityPanel();
city.setVisible(true);
this.add(city);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
}
}
public class CityPanel extends JPanel {
private BufferedImage image;
public CityPanel() {
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("Test", 0, 0); }
}
EDIT:
I have this code in CityMap. It display the string but no the image.
public CityPanel(String filePath, int red, int green, int blue) {
this.image = colorImage(filePath, red, green, blue);
this.setSize(100, 100);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 50, 50, null);
g.drawString("sdjkfpod", 50, 50);
}
Could you please replace your following constructor of PanelMap.java:
public PanelMap() {
CityPanel city = new CityPanel();
city.setVisible(true);
this.add(city);
}
By following constructor:
public PanelMap() {
String filePath = "C:\\...\\city2.png";
CityPanel city = new CityPanel(filePath, 0, 255, 255);
this.setLayout(new BorderLayout());
this.add(city, BorderLayout.CENTER);
}
and see the result?
Following changes have been made to your code:
The statement city.setVisible(true); is removed since it is not
required at all.
The statement this.add(city); was indeed adding CityPanel to
PanelMap but CityPanel took up very small space and looked as a
very small rectangle. This is the reason the BorderLayout has been
used.
Following PanelMapDemo.java adds PanelMap to a JFrame and creates an executable example.
public class PanelMapDemo extends javax.swing.JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
PanelMapDemo demoFrame = new PanelMapDemo("PanelMapDemo");
demoFrame.setVisible(true);
}
});
}
public PanelMapDemo(String title) {
super(title);
setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
add(new PanelMap());
setSize(new java.awt.Dimension(400, 200));
setLocationRelativeTo(null);
}
}
On my system when the original picture was:
Your MapPanel changes the picture to:
Hope, this helps.

g.drawImage not successful, completely stumped

Trying to draw an image and I have no idea why it's not appearing on the screen. Image is loaded fine.
System.out.printf("%nPlayer_1.img_player1 is: "+Player_1.img_player1); says that the image is loaded correctly (correct image w/h etc).
Have tried all manner of image observers, drawing the image from different classes (namely Player_1.drawSquare and MyPanel.painComponent)
There are regular repaint() requests coming in from the Control class, but I do not believe this is preventing the drawing.
No error's are thrown.
player_1.gif is located within the game_2 package, I have tried running the code with it in the src and root folder.
package test;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
public class Main{
public static void main(String[] args) {
GUI.main(args);
}
}
class GUI {
static JFrame f = new JFrame("Swing Paint Demo");
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
System.out.printf("%nCreated GUI on EDT? "+
SwingUtilities.isEventDispatchThread());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new MyPanel());
f.setSize(640,640);
f.setVisible(true);
}
}
class MyPanel extends JPanel {
public MyPanel() {
//setBorder(BorderFactory.createLineBorder(Color.black));
}
public Dimension getPreferredSize() {
return new Dimension(640,640);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.printf("%nPaincomponent on GUI");
Player_1.paintSquare(g);
System.out.printf("%nPlayer_1.img_player1 is: "+Player_1.img_player1);
}
}
class Player_1{
public static Image img_player1;
public int int_x = 0;
public int int_y = 1;
public int int_w = 1;
public int int_h = 1;
public static void paintSquare(Graphics g){
if (img_player1 == null)
img_player1 = IOControl.getImage("player_1.gif");
g.drawImage(img_player1, 32, 32, 32, 32, GUI.f);
}
}
class IOControl {
public static void main(String[] args) {
}
static BufferedImage getImage(String path)
{
BufferedImage tempImage = null;
try
{
URL imageURL = Main.class.getResource(path);
tempImage = toBufferedImage(Toolkit.getDefaultToolkit().getImage(imageURL));
}
catch(Exception e)
{
System.out.printf("%nAn error occured -" + e.getMessage());
}
System.out.printf("%n"+path);
System.out.printf("%nIOControl.path");
System.out.printf("%n"+tempImage);
System.out.printf("%nIOControl.tempImage");
return tempImage;
}
public static BufferedImage toBufferedImage(Image img)
{
if (img instanceof BufferedImage)
{
return (BufferedImage) img;
}
// Create a buffered image with transparency
BufferedImage bimage = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
// Draw the image on to the buffered image
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
// Return the buffered image
return bimage;
}
}
Don't call the main method. It is meant for the JVM to call as the starting point of your program. Also, you should only have one main method, in whichever class is the launching class.
Don't create the image in the paintXxx method. You should create in the constructor.
All the other faulty points I mentioned in my comments in the original post.
It looks like your main problem is in the getImage method. I simplified it for you and it works (given I fixed the other points I mentioned above).
public static BufferedImage getImage(String path) {
BufferedImage img = null;
try {
img = ImageIO.read(GUI.class.getResource(path));
} catch (IOException ex) {
ex.printStackTrace();
}
return img;
}
Here's the full running code. Note: you had the image location correct, with it in the same package as the Main class.
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GUI().createAndShowGUI();
}
});
}
}
class GUI {
JFrame f = new JFrame("Swing Paint Demo");
public void createAndShowGUI() {
System.out.printf("%nCreated GUI on EDT? "
+ SwingUtilities.isEventDispatchThread());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new MyPanel());
f.setSize(640, 640);
f.setVisible(true);
}
}
class MyPanel extends JPanel {
Player_1 player1;
public MyPanel() {
player1 = new Player_1(MyPanel.this);
}
public Dimension getPreferredSize() {
return new Dimension(640, 640);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.printf("%nPaincomponent on GUI");
player1.paintSquare(g);
}
}
class Player_1 {
public Image img_player1;
public int int_x = 0;
public int int_y = 1;
public int int_w = 1;
public int int_h = 1;
JPanel panel;
public Player_1(JPanel panel) {
this.panel = panel;
img_player1 = IOControl.getImage("player_1.png");
}
public void paintSquare(Graphics g) {
g.drawImage(img_player1, 32, 32, 32, 32, panel);
}
}
class IOControl {
public static BufferedImage getImage(String path) {
BufferedImage img = null;
try {
img = ImageIO.read(GUI.class.getResource(path));
} catch (IOException ex) {
ex.printStackTrace();
}
return img;
}
}

drawImage within separate jpanel draws just a piece

I'm not that familiar with Java and I'm a little clueless about my current problem.
I'm trying to draw an Image within a separate class of my Main JFrame, but it
always draw just a little piece of the picture (maybe 10x10px).
(A test with a Label worked)
Maybe I didn't used the g.drawImage method correct, or the JPanel don't have enough space??
MainWindow:
public class Deconvolutioner extends JFrame {
Draw z;
Picturearea picturearea;
class Draw extends JPanel {
public void paint(Graphics g) {
}
}
public Deconvolutioner() {
setTitle("Deconvolutioner");
setLocation(30,1);
setSize(1300,735);
super.setFont(new Font("Arial",Font.BOLD,11));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
this.setLayout(flow);
picturearea = new Picturearea();
this.add(picturearea);
add(z = new Draw());
setVisible(true);
}
class Open implements ActionListener {
public void actionPerformed(ActionEvent e) {
JFileChooser fileOpen = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter("png & jpg files", "png",
"jpg");
fileOpen.addChoosableFileFilter(filter);
int returnVal = fileOpen.showDialog(null, "Open file");
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
String path = fileOpen.getSelectedFile().getPath();
URL url = new File(path).toURI().toURL();
BufferedImage img = ImageIO.read(url);
picturearea.setPicture(img);
} catch (IOException ex) {
System.err.println("Some IOException accured (set the right path?): ");
System.err.println(ex.getMessage());
}
} else {
}
repaint();
}
}
And the separate class:
public class Picturearea extends JPanel {
public BufferedImage image;
Draw z;
public Picturearea() {
add(z = new Draw());
setVisible(true);
}
class Draw extends JPanel {
#Override
public void paint(Graphics g) {
g.drawImage(image, 0, 0, this);
}
}
public void setPicture(BufferedImage picture) {
try {
image = picture;
} catch (Exception e) {
System.err.println("Some IOException accured (did you set the right path?): ");
System.err.println(e.getMessage());
}
repaint();
}
}
I'm grateful for every help.
thanks for your time.
You are using FlowLayout for JFrame's contentPane's layout. FlowLayout obeys component's preferredSize. Try setting preferredSize of your pictureArea by pictureArea.setPreferredSize(Dimension) or overriding getPreferredSize(Dimension) function in PictureArea class.
you are using paint(Graphics g) for custom painting:
#Override
public void paint(Graphics g) {
g.drawImage(image, 0, 0, this);
}
Do not override paint() for custom painting override paintComponent(Graphics g) instead. And You may need to scale your image to fit in the size of the JPanel. you can use g.drawImage(x, y, width, height, observer) function if you need to scale your image.
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}

Java Swing - Change JComponent opacity from mouse listener

Pretty much like the title says - I've got a JPanel, and I'd like to have it at 90% opacity if inactive, and full opacity if moused over.
I've got the separate components working - the mouse over and out is OK and I can change the opacity from overriding the paintComponent method, but I suppose I'm having trouble with the connection between the MouseAdapter and the paintComponent.
It seems there's probably a really easy way to do this - any tips?
(BTW sorry left the code at home so no example for now :( unless it's not quickly solvable then I'll throw some code up tomorrow.)
If you are overriding the paintComponent method, you should be able to add an opacity variable to the JPanel extension. Modify this in the MouseAdapter (using mutators). Then refer to this variable in the overridden paintComponent method to determine how to paint.
If you override the paintComponent method already, I'd suggest you make the MouseAdapter an anonymous inner class of your Panel and let it manipulate a private boolean flag.
public class FadingPanel extends JPanel
{
private boolean active;
public FadingPanel()
{
createMouseAdapter();
}
private void createMouseAdapter()
{
this.addMouseListener(new MouseAdapter()
{
// your mouseadapter code here toggling the active flag
}
}
#Override
public boolean paintComponent(Graphics g)
{
if(active)
{
super.paintComponent(g);
}
else
{
// your semitransparent painting code here
}
}
}
Shoot, I don't have the Haase and Guy book with me, and I don't remember the way they recommend to code for translucent components, but I guess overriding either paint or paintComponent work except that overriding paint will show the effect on child components from the get-go. For example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class OpaqueJPanels {
private static void createAndShowUI() {
JPanel mainPanel = new JPanel(new GridLayout(1, 0));
mainPanel.add(new OpaqueJPanel(false, "Override paintComponent"));
mainPanel.add(new OpaqueJPanel(true, "Override paint"));
JFrame frame = new JFrame("Opaque JPanels");
frame.getContentPane().add(mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
class OpaqueJPanel extends JPanel {
private static final int OJP_WIDTH = 250;
private static final int OJP_HEIGHT = OJP_WIDTH;
private static final Composite TRANSLUSCENT_COMPOSITE = AlphaComposite
.getInstance(AlphaComposite.SRC_OVER, 0.4f);
private static final Composite NON_TRANSLUSCENT_COMPOSITE = AlphaComposite
.getInstance(AlphaComposite.SRC_OVER, 1.0f);
private boolean overridePaint;
private boolean transluscent = true;
public OpaqueJPanel(boolean overridePaint, String title) {
add(new JButton("Button"));
setBorder(BorderFactory.createTitledBorder(title));
addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
transluscent = false;
getParent().repaint();
}
#Override
public void mouseExited(MouseEvent e) {
if (!OpaqueJPanel.this.contains(e.getPoint())) {
transluscent = true;
getParent().repaint();
}
}
});
}
#Override
public Dimension getPreferredSize() {
return new Dimension(OJP_WIDTH, OJP_HEIGHT);
}
#Override
public void paint(Graphics g) {
if (!overridePaint) {
super.paint(g);
return;
}
Graphics2D g2 = (Graphics2D) g;
Composite composite = transluscent ? TRANSLUSCENT_COMPOSITE
: NON_TRANSLUSCENT_COMPOSITE;
g2.setComposite(composite);
super.paint(g);
}
#Override
protected void paintComponent(Graphics g) {
if (overridePaint) {
super.paintComponent(g);
return;
}
Graphics2D g2 = (Graphics2D) g;
Composite composite = transluscent ? TRANSLUSCENT_COMPOSITE
: NON_TRANSLUSCENT_COMPOSITE;
g2.setComposite(composite);
super.paintComponent(g);
}
}
I also found that it was better if I repainted the JPanel's parent component rather than the JPanel itself.

Categories

Resources