Questions tagged [paintcomponent]

paintComponent (JComponent.paintComponent(Graphics g)) is a method that Java Swing engine calls on JComponent, in order to draw it using the passed Graphics.

paintComponent is responsible for drawing contents of the JComponent, excluding border and possible children. Same as Canvas.paint, it may be overridden but is seldom called directly.

For custom Swing JComponent, it is overridden to paint a deeply custom content, in somewhat similar fashion as paint(Graphics) is overridden for Canvas (border and children as still painted as by default).

The passed parameter (Graphics) is actually Graphics2D and can be casted to use advanced features.

Standard Swing components delegate the call to the attached ComponentUI that paints the component by the rules of the current skin (look & fell).

1614 questions
8
votes
4 answers

Compare sorting algorithm

I implemented different type of sorting (bubble, insertion, selection). Know I want to compare their implementations like the following for each sort (here's an example with the bubble sort) : For example, here's my bubble sort : private static…
user2336315
  • 14,237
  • 9
  • 37
  • 63
8
votes
2 answers

Why does calling dispose() on Graphics object cause JPanel to not render any components

After learning that dispose() should be called on Graphics/Graphics2D object after use, I went about changing my game to incorporate this. When I added g2d.dispose() in overridden paintComponent(Graphics g) of JPanel, my components which I added…
David Kroukamp
  • 34,930
  • 13
  • 72
  • 130
8
votes
2 answers

paintComponent draws other components on top of my drawing

I'm trying to build a simple paint tool. The mouseDrag events creates a new ellipse and causes my JPanel to repaint(). This works fine so far. However, if I press any button (or any other UI component) before firing the mouseDrag event for the first…
Reini
  • 1,221
  • 3
  • 19
  • 33
8
votes
3 answers

Java Transparency rendering error

I am currently developing a kiosk style Java program to display weather, time, forecast etc. Here is a shot of what the program should look like (screenshot taken before the time updated) The various sections are created as JPanels with the…
Peaches491
  • 1,219
  • 16
  • 25
7
votes
3 answers

Painting pixels images in Java

Which method is the best way to create a pixel image with java. Say, I want to create a pixel image with the dimensions 200x200 which are 40.000 pixels in total. How can I create a pixel from a random color and render it at a given position on a…
Tom1983
  • 71
  • 1
  • 1
  • 2
7
votes
1 answer

Drawing a rounded rectangle with opacity on a BufferedImage

I have been trying to implement basic text bubbles for a small game I am developing. Not wanting to go too fancy, I started with a basic rounded rectangle with a border containing some text : Then, I decided that text bubbles should fade out after…
Niss36
  • 305
  • 3
  • 8
7
votes
2 answers

Java game 2D overlapping shadows with Swing

I am currently developing a 2D Java game using Swing as my primary drawing component. Every object has a shadow (BufferedImage) but every shadow overlaps other shadows. Is it possible to only have the shadows not overlap each other? Because I still…
7
votes
3 answers

Creating a simple custom JComponent in Java?

I want to start building my own customized JComponent's for a project at work. I have a simple example below that should just create a ball on the screen. (I found most of it on the internet) but it does provide a decent starting point. My question…
user3376708
7
votes
2 answers

Java JPanel getGraphics()

Since Java only supports single inheritance, I desire to paint directly on an instance of a JPanel that is a member of the class Panel. I grab an instance of Graphics from the member and then paint whatever I desire onto it. How can I not inherit…
Mushy
  • 2,225
  • 9
  • 30
  • 50
7
votes
1 answer

Adding a background image to a JPanel

I'm working on building a board game in Java. For the game board itself I was trying to place the image of the board as the background of the entire JPanel, which fills the JFrame. I found a way to do this, but only with the file stored locally,…
Matthew G
  • 95
  • 1
  • 1
  • 9
7
votes
3 answers

Why does swing draw simple component twice?

Here is simple example of drawing an oval. public class SwingPainter extends JFrame{ public SwingPainter() { super("Swing Painter"); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); getContentPane().add(new…
Dragon
  • 2,379
  • 10
  • 40
  • 61
7
votes
2 answers

Why gif animation doesn't animate when using it in paintComponent()?

I'm using paintComponent() to paint a gif animated image at the backgound of JPanel. It shows up the gif but doesn't animate. I use java 1.5 and i know that i can use label with icon. Does any body know why and how to fix it? private static…
itro
  • 6,260
  • 23
  • 69
  • 98
6
votes
1 answer

Repaint() method calling in another class

I have a problem with repaint() method in my Java code. I want to call it in another class but I can't, something doesn't work at all. I've searched on forums, but nothing was able to help me out. My Main class: public class Main { public static…
McDaniel
  • 83
  • 4
6
votes
1 answer

.gif image doesn't moves on adding it to the JTabbed pane

I have a JFrame. In that i have two containers i.e two JPanel. one Panel holds a image. other holds a JButton. Then these two are added to JTabbedPane. My problem is on using a .gif image the image becomes static as any other normal .jpg image. Can…
user3320152
  • 113
  • 1
  • 7
6
votes
2 answers

AffineTransform seeming to ignore component bounds

I have the following: public class ParametricEQView extends JPanel implements PluginView { private static final int BAND_WIDTH = 3; private static final int THROW_HEIGHT = 64; private static final int WIDTH = 128*BAND_WIDTH + 2*MARGIN; …
MattPutnam
  • 2,619
  • 2
  • 17
  • 22