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
76
votes
5 answers

How does paintComponent work?

This might be a very noob question. I'm just starting to learn Java I don't understand the operation of paintComponent method. I know if I want to draw something, I must override the paintComponent method. public void paintComponent(Graphics g) { …
Hải Phong
  • 4,864
  • 6
  • 28
  • 49
50
votes
4 answers

How to draw in JPanel? (Swing/graphics Java)

I'm working on a project in which I am trying to make a paint program. So far I've used Netbeans to create a GUI and set up the program. As of right now I am able to call all the coordinated necessary to draw inside it but I am very confused with…
Nick R
  • 513
  • 2
  • 6
  • 13
33
votes
2 answers

Difference between paint, paintComponent and paintComponents in Swing

What is the actual difference between paint(), paintComponent() and paintComponents() in Java Swing? I tried to understand what explained in Oracle docs but I am not clear.
Abhishek Choudhary
  • 7,569
  • 18
  • 63
  • 118
23
votes
9 answers

How to draw a circle with given X and Y coordinates as the middle spot of the circle?

I have developed a telecommunication application for locating signal strengths from the towers. I have used java swing and I'm having a problem when drawing the circle around the given point of the mobile signal transmitter tower location. I have…
нαƒєєz
  • 1,119
  • 4
  • 13
  • 27
22
votes
4 answers

Draw a circle with a radius and points around the edge

I'm really stuck on how to go about programming this. How to draw a circle in Java with a radius and points around the edge? I need to draw a circle within a JFrame with a radius and points around the circumference. i can mathematically calculate…
alchemey89
  • 445
  • 1
  • 6
  • 8
21
votes
2 answers

How to make a rectangle in Graphics in a transparent colour?

I'm trying to paint a rectangle on my application in a red shade but I need to make it sort of transparent so that the component under it will still show. However I still want that some colour will still show. The method where I'm drawing is the…
ict1991
  • 1,900
  • 4
  • 24
  • 31
18
votes
5 answers

JPanel doesn't update until resize Jframe

I subclass JPanel to overwrite paintComponent(Graphics), I want to draw an image onto jpanel in a jframe. But my image hasn't shown up until I make a change to jframe's size. This is my code: public class ImagePanel extends JPanel{ public void…
nautilusvn
  • 615
  • 2
  • 9
  • 20
15
votes
2 answers

Java - rounded corner panel with compositing in paintComponent

From the original question (below), I am now offering a bounty for the following: An AlphaComposite based solution for rounded corners. Please demonstrate with a JPanel. Corners must be completely transparent. Must be able to support JPG…
Ben
  • 47,286
  • 44
  • 159
  • 208
13
votes
2 answers

Difference between paint() and paintcomponent()?

I have tried tutorials on this but I still don't quite understand it. Basically my question is which method is better and why? Should I use paint or paintComponent? Please try to keep the answer simple, thanks.
Boogley Beegly
  • 95
  • 1
  • 2
  • 11
10
votes
3 answers

Drawing an object using getGraphics() without extending JFrame

How can I draw an object without a class (which extends JFrame)? I found getGraphics method but it doesnt draw the object. import javax.swing.*; import java.awt.*; public class Main { public static void main(String[] args) { JFrame frame…
user2110286
10
votes
5 answers

Simulating rain

I am making a game in java and I want to create a simulation of a cloud that is pouring rain. The cloud is supposed to move to the right while raining. Moving the cloud is no problem. It's the rain that I am struggling with. What I was thinking of…
9
votes
4 answers

JPanel Graphics clearing and repainting?

I have a JPanel with a paintComponent() function. I'll call it once, then when the user clicks a different JButton, I'll set some flag and want to call this function again as it will do something slightly different after the flag is set. So here's…
JDS
  • 14,991
  • 41
  • 142
  • 202
9
votes
3 answers

Draw a car wheel

I have a very simple problem. I am learning Java, and was given an assignment to draw a car. I did this all in one class that extends JPanel, and did the drawing within paintComponent(). I realize this is poor object-oriented programming, and…
Josue Espinosa
  • 4,642
  • 14
  • 43
  • 80
9
votes
3 answers

about drawing a Polygon in java

hi there i'm trying to improve myself about java2D and first of all i'm dealing with drawing polygons. However, i can not see the polygon on frame. I read some tutorials and examples but as i said i face with problems. here is the sample code of…
quartaela
  • 2,289
  • 14
  • 52
  • 89
8
votes
6 answers

Painting over the top of components in Swing?

I have a JPanel added to a JViewport, and the panel has several other panels added to it. I'm trying to implement a dragging selection, where you can select more than one component by dragging the mouse. The only problem I'm facing is that the…
rtheunissen
  • 6,831
  • 5
  • 30
  • 60
1
2 3
99 100