0

Hello,

I created two classes, one extends JButton and the other extends JPanel. In both cases, both overrides the paint method. Basically I'm extending JPanel so I can place an image as the back ground. The issue I'm having is that every time the button is drawn, if it's drawn past 100,100 , it will not show up. Also the area that listening to the mouse is a bit off from where the image is drawn. If I draw components through my class it shows up fine, but it still draws the off set one. What method to I have to override to stop the other one from being drawn.

2
Contributors
3
Replies
4
Views
9 Years
Discussion Span
Last Post by Mr.UNOwen
0

For one thing, override paintComponent() instead of paint() and be sure that you call super.paintComponent() before your custom code.

As for the specific of your question, it's a bit unclear. Post the relevant sections or example code that demonstrates the problem behavior.

0

The border now shows, but the problem is still there. The image I want displayed for the button is off center

Here is the code that use those two classes with (FYI: the class extends JFrame)

super("LED CUBE ANIMATOR");
      try
      {
         setIconImage(ImageIO.read(new File("icon.png")));
         button = new PlayButton();
         button.addActionListener(this);
         panel = new DecoratedPanel(new File("bknd.png"));
         panel.setSize(50,50);
         button.setSize(new Dimension(190, 190));
         button.setLocation(10, 10);
         panel.add(button);
         add(panel);

      }
      catch(Exception e)
      {
         System.out.println(e);
         System.exit(1);
      } 
      setSize(new Dimension(501,501));
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
      setResizable(false);
      setVisible(true);

Here is the button class

import java.awt.Button;
import java.awt.Dimension;
import java.io.File;
import java.awt.Image;
import java.awt.Graphics;
import javax.imageio.ImageIO;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import java.awt.event.*;
public class PlayButton extends JButton

{
   private Image play;
   private Image pause;
   private boolean playState;

   public PlayButton() throws Exception
   {
      super();
      setText("play");
      playState = true;
      play = ImageIO.read(new File("play.png"));  
      pause = ImageIO.read(new File("pause.png"));  
   }

   public void changeState()
   {
      playState= !playState;
      if(playState)
      {
         setText("play");
      }
      else
      {
         setText("pause");
      }
      repaint();
   }

   public void paintComponent(Graphics g)
   {
      if(playState)
      {
         g.drawImage(play, getX(), getY(), getWidth(), getHeight(), null); 
      }
      else
      {
         g.drawImage(pause, getX(), getY(), getWidth(), getHeight(), null); 
      }
   }
}

Here is the panel class

import java.awt.Image;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

import java.io.File;
import javax.swing.JPanel;
import java.awt.Component;
import java.awt.Panel;
import javax.swing.JComponent;


public class DecoratedPanel extends JPanel
{
   private Image bknd;

   public DecoratedPanel(File image) throws Exception
   {
      super(null);
      bknd = ImageIO.read(image);
   }

   public void paintComponent(Graphics g)
   { 
      super.paintComponent(g);
      g.drawImage(bknd,0,0, getWidth(), getHeight(), null);
   }
}
0

Never mind problem solved.....

graphics already takes into account where the button is, so I should of displayed the image at 0,0 and not its position.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.