Why does the image not show up on the JPanel? - java

I want to draw an image on a JPanel which is displayed on a JFrame. I tested the paint method without the frame and it seemed to work, but as soon as I added the image on the frame it won't get shown. Instead I only see a very small square which displays a small area of the image.
Here's my code so far:
TestFrame class:
public class TestFrame extends JFrame {
private JFrame frame = new JFrame();
private JPanel jp = new JPanel();
public MyFrame() {
frame.setTitle("test");
frame.setSize(300, 300);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setLayout(new BorderLayout());
jp.setBackground(Color.YELLOW);
frame.add(jp, BorderLayout.CENTER);
jp.add(new DrawPanel());
jp.repaint();
frame.setVisible(true);
}
}
DrawPanel class:
public class DrawPanel extends JPanel {
private BufferedImage img;
public DrawPanel() {
try {
img = ImageIO.read(getClass().getResourceAsStream("/resources/heart.jpg"));
}
catch (IOException e) {
e.printStackTrace();
}
}
#Override protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, null);
}
}

Try with this in the method paintComponent():
g.drawimage(img.getImage(), 0, 0, null);

Related

JInternalFrame camera image repaint issue

I use JInternalFrame to view a camera image, I get an image but it flashes/blinks very frequently, so I can barely see the image. I extend JFrame instead of JInternalFrame, everything works perfectly fine. Maybe JInternalFrame works differently to JFrame but I can't figure out the issue with the update.
public class CameraView extends JInternalFrame{
private JPanel contentPane;
VideoCap videoCapture = new VideoCap();
public CameraView(){
setSize(400, 400);
setLocation(100, 100);
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(null);
new MyThread().start();
}
#Override
public void paint(Graphics g){
g = contentPane.getGraphics();
g.drawImage(videoCapture.getOneFrame(), 0, 0, this);
}
class MyThread extends Thread{
#Override
public void run() {
for (;;){
repaint();
try { Thread.sleep(30);
} catch (InterruptedException e) { }
}
}
}
}
You have to remove
g = contentPane.getGraphics();
and
contentPane.setLayout(null);

My custom component cannot be painted

I'm trying to create a class(called Stock) which can be identified as a Component--and let me use Listener on it.
However, it is not painted unless you specify (not a good idea!) to paint it in panel.paint().
The program runs as this:
JFrame frame = new JFrame ("..");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
final Stock theStock=new Stock();
JPanel primary = new JPanel();
primary.setPreferredSize(new Dimension(500,500));
primary.add(theStock);
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
The Stock will not be painted unless primary(JPanel) is overriden so that there is a specification calling the stock to draw:
#Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.black);
g.drawRect(x-2, y-2, 74, 124);
}
Here's my code--I guess the problem may happen because I did not override anything else than paint method inherited from Component.
**********Modified to be testable, please see below**
import java.awt.*;
import javax.swing.*;
#SuppressWarnings("serial")
public class Stock extends Component{
private int x; //the position for image
private int y;//position for image
public Stock() {
x=285;
y=280;
}
#Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.black);
g.drawRect(x-2, y-2, 74, 124);
}
public void works(){
JFrame frame = new JFrame ("..");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
final Stock theStock=new Stock();
JPanel primary = new JPanel(){
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
theStock.paint(g);
}
};
primary.setPreferredSize(new Dimension(500,500));
primary.add(theStock);
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
public void doesNotWork(){
JFrame frame = new JFrame ("..");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
final Stock theStock=new Stock();
JPanel primary = new JPanel();
primary.setPreferredSize(new Dimension(500,500));
primary.add(theStock);
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
Stock a = new Stock();
a.works();
//a.doesNotWork();
}
}
p.s.1. I'm not using JComponent because JComponent has more functionalities than I need to use. Component, as from which Container and Button inherit, should work well.
p.s.2. Even you use JComponent you get the same result. Code below.
import java.awt.*;
import javax.swing.*;
#SuppressWarnings("serial")
public class Stock extends JComponent{
private int x; //the position for image
private int y;//position for image
public Stock() {
x=285;
y=280;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.black);
g.drawRect(x-2, y-2, 74, 124);
}
public void works(){
JFrame frame = new JFrame ("..");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
final Stock theStock=new Stock();
JPanel primary = new JPanel(){
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
theStock.paint(g);
}
};
primary.setPreferredSize(new Dimension(500,500));
primary.add(theStock);
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
public void doesNotWork(){
JFrame frame = new JFrame ("..");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
final Stock theStock=new Stock();
JPanel primary = new JPanel();
primary.setPreferredSize(new Dimension(500,500));
primary.add(theStock);
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
Stock a = new Stock();
//a.works();
a.doesNotWork();
}
}
Your problem is one of layouts, size and preferredSize. Since you're adding a your Stock Component to a FlowLayout using JPanel, the Component will size itself at its preferred size which is 0, 0. If you gave the JPanel a different layout, say
primary.setLayout(new BorderLayout());
and then added your Stock object, it would fill the primary JPanel and it would paint itself.
Other options include setting your Stock objects preferredSize, or even overriding its getPreferredSize() method so that it's large enough:
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}

Visibility of JFrame by adding JPanel in Netbean

I have two classes: one is JFrame and second is JPanel. When i add JPanel object in JFrame class it makes not error but did not show JPanel result. It only shows blank JFrame.
This is my JPanel class:
public class grafix extends JPanel {
#Override
public void paintComponent(Graphics g){
super.paintComponents(g);
Graphics2D g2= (Graphics2D) g;
Rectangle r = new Rectangle(15,10,200,300);
g2.draw(r);
g2.setColor(Color.blue);
g2.fillOval(50, 50, 30,30);
g2.drawString("Hello World", 120, 50);
}
}
And this is my JFrame Class:
public class JFrame extends javax.swing.JFrame {
public JFrame() {
initComponents();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
grafix gf = new grafix(); // object created of JPanel class
frame.getContentPane().add(gf);// by adding object
frame.pack();
frame.setVisible(true);
}
});
}
}
No error being mentioned in Netbean but it did not show any drawing but frame only.
The graphics are shown but your frame is too small to show the panel graphics. Your Grafix panel component is using the default preferred size of 0x0 to the frame reveals nothing. Override getPreferredSize in the class to allow the correct size to be set when the frame is packed
#Override
public Dimension getPreferredSize() {
return new Dimension(250, 400);
}

JButton copied when repainting?

I have a JFrame with 2 JPanel in it: a PaintPanel (with a paint() method) and a ButtonPanel (with buttons). When I invoke the repaint() of the PaintPanel (but clicking the button) the button of the ButtonPanel is being painted in the PaintPanel! It isn't clickable or anything, it is just there.
I tried to recreate the problem with this code:
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("frame");
frame.setSize(400,400);
frame.setLayout(new GridLayout(2,1));
PaintPanel paint = new PaintPanel();
ButtonPanel buttons = new ButtonPanel(paint);
frame.add(paint);
frame.add(buttons);
frame.setVisible(true);
}
}
public class PaintPanel extends JPanel{
public void paint(Graphics g){
g.drawRect(10, 10, 10, 10);
}
}
public class ButtonPanel extends JPanel implements ActionListener{
private PaintPanel paintPanel;
public ButtonPanel(PaintPanel paintPanel){
this.paintPanel=paintPanel;
JButton button = new JButton("button");
button.addActionListener(this);
add(button);
}
#Override
public void actionPerformed(ActionEvent arg0) {
paintPanel.repaint();
}
}
This sould recreate the problem I have (sorry for the odd code markings, can't seem to get it right).
I really hope one of you knows what is happening here because i don't...
First of all, you should override paintComponent() instead of paint(). It's part of the best practices in Swing when it comes to do some panel customization.
Secondly, here is the code that works for me (I don't know why yours doesn't though :S):
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("frame");
frame.setSize(400, 400);
// frame.setLayout(new GridLayout(2, 1));
PaintPanel paint = new PaintPanel();
ButtonPanel buttons = new ButtonPanel(paint);
// frame.add(paint);
// frame.add(buttons);
frame.setVisible(true);
JPanel pan = new JPanel(new BorderLayout());
pan.add(paint);
pan.add(buttons, BorderLayout.SOUTH);
frame.add(pan);
}
}
class PaintPanel extends JPanel {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(new Color(new Random().nextInt()));
g.drawRect(10, 10, 10, 10);
}
}
class ButtonPanel extends JPanel implements ActionListener {
private final PaintPanel paintPanel;
public ButtonPanel(PaintPanel paintPanel) {
this.paintPanel = paintPanel;
JButton button = new JButton("button");
button.addActionListener(this);
add(button);
}
#Override
public void actionPerformed(ActionEvent arg0) {
if (getParent() != null) {
getParent().repaint();
}
}
}

how can i put a JButton on an image?

I am trying to fix a JFrame where there will be a background image and on the image JButtons which will do some commands. I try to do it without layout because i want to put small buttons in some specific locations on the JFrame but every time i do it, the background image comes to the front or the JFrame has size equal to the JFrame size. With the following code, the JButton has the same size to JFrame. I have tried to change the size and location of the JButton but nothing. Can you help me please?
here is the code
public final class Test extends JComponent
{
private Image background;
private JFrame frame;
private Dimension dimension;
public Test()
{
dimension = new Dimension(15, 15);
frame = new JFrame("Iphone");
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(this);
frame.setBounds(641, 0, 344, 655);
frame.setVisible(true);
test = displayButton("tigka");
frame.getContentPane().add(test);
}
public void update(Graphics g)
{
paint(g);
}
public void paintComponent(Graphics g)
{
super.paintComponents(g);
g.drawImage(background, 0, 25, null); // draw background
// label();
test = displayButton("test");
}
public JButton displayButton(String name)
{
JButton button = new JButton(name);
button.setSize(100, 100);
button.setPreferredSize(dimension);
return button;
}
You need to change the content pane to get a background for your Frame.
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame("Test");
frame.setContentPane(new JPanel() {
BufferedImage image = ImageIO.read(new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png"));
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, 300, 300, this);
}
});
frame.add(new JButton("Test Button"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
Output:
Have you tried using a JLabel with HTML in the label? Something like this:
import javax.swing.*;
public class SwingImage1
{
public static void main( String args[] )
{
JFrame frm = new JFrame( "Swing Image 1" );
JLabel lbl = new JLabel( "<html><body><img src=\"http://liv.liviutudor.com/images/liv.gif\"></body></html>" );
frm.getContentPane().add( lbl );
frm.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frm.pack();
frm.setVisible( true );
}
}
then on top of your label you can add your button?
You should swap those two lines:
super.paintComponents(g); //paints the children, like the button
g.drawImage(background, 0, 25, null); // draw background later possibly overwriting the button
Thus it should be this order:
g.drawImage(background, 0, 25, null);
super.paintComponents(g);
Additionally, note that the content pane's default layout is BorderLayout. Thus you'd set the layout of your content pane to null explicitly.
/*it is simple to put button on image first set image by making object then make button object & add the button object direct to image object rather then add to frame.*/
package frame;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class frame
{
public frame()
{
JFrame obj = new JFrame("Banking Software");
JButton b1 = new JButton("Opening Account");
JLabel image = new JLabel(new ImageIcon("money.jpg"));
image.setBounds(0,0, 1600, 1400);
obj.setExtendedState(JFrame.MAXIMIZED_BOTH);
obj.add(image);
b1.setBounds(500,400, 100, 40);
image.add(b1);
obj.setVisible(true);
}
public static void main(String args[])
{
new frame();
}
}

Categories

Resources