Drawimage java method
Java provides the Graphics class, which allows you to draw ellipses, squares, lines, display text and also has many other drawing methods. For any programmer, an understanding of the Graphics class is essential before getting into drawing in Java.
In order to be able to paint, a program needs a valid graphics context, represented by an instance of the Graphics class. But this class cannot be instantiated directly; so we must create a component and pass it to the program as an argument to the paint() method.
In addition, the Graphics class maintains a graphic context: a current drawing area, a Background and a Foreground drawing color, a Font with all its properties, etc. The axes are located in the upper left corner. The coordinates are always measured in pixels.
The paint method is executed every time the JFrame must be redrawn and arrives as parameter an object of the Graphics class. This object allows us to access the JFrame background and using the graphics primitives to draw lines, rectangles, ellipses, etc.
Java graphics class
Almost all Swing components and containers have an associated paint(g) method for drawing them on the screen. Java invokes this method automatically when it has to display, in a standard way, the component or container in question (i.e., its borders, its title, if it has one, etc.).
Where g is an object of the abstract class Graphics. Every container or component that can be drawn on the screen has an associated g object of this class, with the information about the screen area that the component or container has.
When redefining the paint(g) method, we always start with a super.paint(g) invocation to the superclass method, thus ensuring that the standard part of the container or component we are handling is drawn.
Draw in java
I am making a little program in which I have run into a detail when trying to draw a polygon in a JPanel in which I want the color of its edges to be totally configurable as well as its background, I have made an attempt with the following code segment:
As you can see you really see the color change only on certain edges of the figure, this depends on the shape of the figure.I have thought of making the entire figure of the same color and then simply draw some lines that would join all the vertices with the color of the edges so that they overlap the edge of the figure.I would appreciate if you would share with me a more formal way or another way other than that raised that could give me solution to this problem, thank you.
If you take advantage of using Graphics2D you can, if you want, highlight the overlapping of the ‘edges’ of the figure with the help of the setStroke() method and improve the rendering with setRenderingHints():
Repaint java, what is it used for?
As I mentioned before, the Graphics class has different methods to draw figures, we are going to start with the method fillOval(x,y,anc,alt) and drawOval(x,y,anc,alt) that allow us to draw ovals, to understand how they work we are going to make the following image:
To make it easier to locate the coordinates we will use the paint tool, to make our drawing, the simplest way to do it is to go putting figure by figure and copy its coordinates in the following way:
We are going to add the eyes, for this we need to change the color so that they can be distinguished from the rest, we will make use of the method setColor(obj Color), which allows us to change the color of a section (everything we put below this invocation will have the chosen color):