Where to place the drawing code in a JPanel used as canvas - java

I've a JPanel where I'm drawing with the mouse events and after resizing, or minimizing-restoring it, it doesnt display what has to be drawn, despite a "degub" println shows me the function is being called, but for some reason, it reamains blank
I thought I should have added my drawing function in the paintComponent function but it seems not to be working right, so what I'm doing wrong or where should I place that drawElements() call?
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawElements();
}
void drawElements() {
Graphics2D g = (Graphics2D)this.getGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for (Element el : elements) {
el.draw(g);
}
}

Pass the graphics object to the drawElements method.
(Right answer provided by #rafalopez79)

Related

How to prevent shapes in a JFrame from disappearing after resizing the window

public void actionPerformed(ActionEvent e)
{
try
{
//récupérer les coordonnées(x,y) du text area
int x=Integer.parseInt(f.x.getText());
int y=Integer.parseInt(f.y.getText());
int puissance=Integer.parseInt(f.p.getText());
f.APs.add(new AccessPoint (x,y,f.APs.size(),puissance));
String ch="Point d'accés "+String.valueOf(f.APs.size())+" Center xc = "+String.valueOf(x)+" yc= "+String.valueOf(x);
System.out.println(ch);
f.t.add(ch);
Graphics g ;
g= f.getGraphics();
paintComponent(g);
}
catch(Exception e1){System.out.println("Erreur");}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if(f.APs.size()!=0)
{
try {
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
int currPoint =f.APs.size()-1;
int puissance =f.APs.get(currPoint).p;
Color C= new Color(128,puissance,puissance,puissance);
Shape circle = new Ellipse2D.Float(f.APs.get(currPoint).x-(f.APs.get(currPoint).diametre/2),
f.APs.get(currPoint).y-(f.APs.get(currPoint).diametre/2),
f.APs.get(currPoint).diametre,f.APs.get(currPoint).diametre);
g2d.draw(circle);
g2d.setPaint(C);
g2d.fill(circle);
}catch(Exception e2){System.out.println("Erreur");}}
}
g= f.getGraphics();
paintComponent(g);
Don't use getGraphics(). Any painting done using that approach will only be temporary (as you have noticed)
Don't invoke paintComponent() directly. Swing will invoke the paintComponent(...) method as required and pass in the proper Graphics object.
The painting method should only ever do painting. It should not change the state of the component.
So if you want to dynamically add shapes to be painted you have two approaches:
Keep an ArrayList of the shapes to be painted. Create a method like addShape(..) to update the ArrayLIst. Then your painting code will iterate through the ArrayList to paint each shape.
Paint directlyl to a BufferedImage. Then paint the BufferedImage.
Working example of both approaches can be found in Custom Painting Approaches

Draw and redraw on a canvas in swing

I know there's no direct replacement for java.awt.Canvas in swing, and I know I'm supposed to use a JPanel and override paintComponent, for example like so:
public void paintComponent(Graphics g) {
g.setColor(Color.black);
g.drawOval(0, 0, 100, 100);
}
And this would draw a black circle on the JPanel when it is created. The problem I have is that I want a dynamic canvas: I want to be able to draw things in response to user input, and redraw continuously, not just once when the application starts. An example would be having a moving object on a canvas, that would need to be redrawn at a rate of say 60 frames per second. How could I achieve this without using AWT components?
EDIT: what I mean is, in an actual canvas, I'd be able to arbitrarily call, say, drawOval anywhere in my code, and that would draw an oval on the canvas; is this doable with JPanel?
Store the information to be drawn (e.g. a Shape or a group of them) and call repaint() from a Swing Timer. Each time the paintComponent(..) method is called, first call the super(..) method to erase the previous drawings, then iterate the list of shapes, move them if necessary, and draw each one.
Here's one way to do it:
public class Renderer extends JComponent implements ActionListener {
private int x;
public Renderer() {
Timer timer = new Timer(1000/60, this);
timer.start();
x = 0;
}
#Override
public void paintComponent(Graphics g) {
super.paint(g);
// drawing code
g.setColor(Color.black);
g.drawOval(x, 0, 100, 100);
}
private void update() {
this.x++;
}
#Override
public void actionPerformed(ActionEvent e) {
update();
repaint();
}
}
Now just add this to your component (JPanel or whatever):
comp.add(new Renderer());

Why is paint(Graphics g) not getting called from child JComponent?

I'm having a simple JComponent that functions as a PdfPage but is basically just a rendered Image.
However, this PdfPage is supposed to hold Highlight annotations e.g. to mark an error inside a PDF Document.
Highlight is just another JComponent that is just painting itself as a rectangle:
public class Highlight extends JComponent {
private Rectangle rectangle;
private Color borderColor = new Color(0, 0, 0, 0);
private Color fillColor;
public Highlight(Rectangle rectangle, Color fillColor) {
this.rectangle = rectangle;
this.fillColor = fillColor;
}
#Override
public void paint(Graphics g) {
g.setColor(this.fillColor);
g.fillRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
g.setColor(this.borderColor);
g.drawRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
}
}
The problem is that adding a Highlight to a PdfPage does not cause it to be painted:
for (DatasheetError datasheetError : datasheetErrorList) {
Highlight highlight = createErrorHighlight(datasheetError);
int pageNumber = datasheetError.getPage() - 1;
PdfPage pdfPage = pdfPages[pageNumber];
pdfPage.add(highlight);
pdfPage.repaint() // Does not help here!
pdfPage.invalidate(); // Does not help here!
}
That is why I have a loop over all child Component objects of a PdfPage and call paint(Graphics g) on each inside PdfPage which looks like this:
public class PdfPage extends JComponent {
// ...
#Override
public void paint(Graphics g) {
// Paint the pdf page ..
g.setColor(Color.black);
g.fillRect(0, 0, getWidth(), getHeight());
if (pageImage != null) {
int x = 0, y = 0, width = pageImage.getWidth(null), height = pageImage.getHeight(null);
g.drawImage(pageImage, x, y, width, height, null);
}
// Paint all child components such as "Highlight"
for(Component component : this.getComponents()) {
component.paint(g);
}
}
}
Is this how I am supposed to do that or is there a better way to draw child components of that PdfPage? I have tried to call repaint() and invalidate() on each PdfPage but that didn't work.
Why doesn't the pain(Graphics g) method get called from each Highlight? Can I put that in execution somehow?
You are not supposed to paint components directly, that is the job of Swing. Get rid of the code attempting to paint the highlights from the PdfPage class.
When you add components to a container, the child components are painted by the paintChildren(...) method. Read the section from the Swing tutorial on A Closer Look at the Paint Mechanism for more information.
As mentioned in the comment, custom painting is done in the paintComponent() method. In your case you override paint() but don't invoke super.paint() so the paintChildren() method is never invoked so the child components don't get painted.
However when you fix this, a JComponent does not use a layout manager so when you add components to your PdfPage class they will not be painted. So, you need to specify the size and location of the components. In your case is looks like the size/location would be based on the Rectangle.
So fix the code and let the normal Swing painting mechanism work instead of trying to do it yourself.
Or a different approach, if you want to do custom painting, would be to have an addHighlight(...) as a method of your PdfPage class. Then in this case you would keep a list of Rectangles that you want to paint and iterate through this list in the paintComponent() method of the PdfPage clase. Check out theDrawOnComponent example from Custom Painting Approaches for a working example of this approach.

Overriding paintComponent twice

This is more of a conceptual question, so it's hard to post a small workable code sample. But, I have a class that overrides paintComponent here:
public abstract class BasePanel extends JPanel {
...
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
this.standardDraw(drawObjects,g2);
}
}
Basically, I'd like this to be the "standard way" this base panel draws if paintComponent is not overridden in a derived class. So, I have a derived class called AspectRatioPanel which I'd like to re-specify how it draws things:
public class AspectRatioPanel extends BasePanel {
...
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
// Get ViewPort Bounding Box to clip
BoundingBox viewPortBoundingBox = this.getViewPortBoundingBox();
// Clip to viewport
g2.setClip((int)viewPortBoundingBox.topLeft.getX(),(int)viewPortBoundingBox.topLeft.getY(),(int)viewPortBoundingBox.getWidth(),(int)viewPortBoundingBox.getHeight());
this.standardDraw(drawObjectsBuf,g2);
}
}
The problem I'm having is the call super.paintComponent(g) in the derived class. I intend for it to be calling paintComponent in JComponent, but it is going through BasePanel first. Is there a better way to approach this problem? I could delete the paintComponent method in BasePanel, but having a standard way of drawing things is useful for me. I also can't seem to call JComponent.paintComponent directly since it's protected. Is there a solution for this? Also, am I doing something conceptually wrong?
Probably I misunderstood your problem, but I would separate standard and custom painting
public abstract class BasePanel extends JPanel {
...
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
provideCustomPainting(g);
}
protected void provideCustomPainting(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
this.standardDraw(drawObjects,g2);
}
}
public class AspectRatioPanel extends BasePanel {
protected void provideCustomPainting(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
// Get ViewPort Bounding Box to clip
BoundingBox viewPortBoundingBox = this.getViewPortBoundingBox();
// Clip to viewport
g2.setClip((int)viewPortBoundingBox.topLeft.getX(),(int)viewPortBoundingBox.topLeft.getY(),(int)viewPortBoundingBox.getWidth(),(int)viewPortBoundingBox.getHeight());
this.standardDraw(drawObjectsBuf,g2);
}
}
You could simply not call super.paintComponent(Graphics); by overwriting paintComponent() you have 3 options:
a.) Call super at the beginning of the method; you paint code paints on top of what has been painted by the super class.
b.) Don't call super at all - your paintComponent needs to ensure it paints everything that is needed; if the components is opaque that means you need to paint the entire area the component occupies.
c.) Call super at your convinience; whatever is painted in super appears to be "layered" in sequence where you call it. Makes only sense if the super method doesn't paint the entire area.
If you insist to use paintComponent from a specific class of your inheritance hierarchy as super.paintComponent, regardless of inheritance hierarchy in between, thats also possible:
BasePanel extends JPanel {
protected final defaultPaintComponent(Graphics g) {
super.paintComponent(g);
}
}
Child classes can call "defaultPaintComponent" instead of super.paintComponent, thus bypassing any implementation classes in between in the hierarchy defined (I suggest declaring it final to prevent accidental overwrites).

java.awt.Graphics change color after drawing

I have asked a similar question a while ago here, but didn't get an answer. The original question was about changing the color of a shape after clicking on it. But I am puzzled on how to access the shape at all after it is drawn.
This is my paintComponent method
#Override
protected void paintComponent(Graphics graph) {
super.paintComponent(graph);
Graphics2D g = (Graphics2D) graph;
// smooth graphics
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// moving to the middle of the panel
g.translate(this.getWidth()/2, this.getHeight()/2);
// painting colored arcs
for(int i = 0; i < 4; i++) {
g.setColor(dimColors[i]);
g.fill(arcs[i]);
}
// painting borders
g.setColor(Color.BLACK);
g.setStroke(new BasicStroke(5F));
g.drawLine(-98, 0, 98, 0);
g.drawLine(0, -98, 0, 98);
g.draw(circle);
// painting central white circle
g.setColor(Color.WHITE);
g.fill(smallCircle);
g.setColor(Color.BLACK);
g.draw(smallCircle);
}
the arcs[] array contains a bunch of Arc2D's that are drawn on the panel. My question is now, if I want to change the color of, for example arcs[0], how do I do that?
Thanks!
EDIT: I now have this MouseAdapter event
private class MyMouseAdapter extends MouseAdapter {
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
Component c = getComponentAt(p);
Graphics g = c.getGraphics();
dimColors[1] = Color.RED;
paintComponent(g);
}
}
And it works, it changes the color of arc[1] because arcs[1] has dimColors[1] set as color when drawing it.
However, I still can't figure out how to check wether the right arc was clicked. Right now you just click anywhere on the graphics panel and it changes the color of that specific arc
This doesn't answer your earlier question, however it does answer your question of click detection. To do this it is best to use Graphics2D because it is a lot easier to write than most other options. Here is an example:
public class GraphicsPanel extends JPanel implements MouseListener
{
private Rectangle2D rect;
First we create our Graphics2D rectangle rect.
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)(g);
g2d.setColor(Color.GREEN);
rect = new Rectangle2D.Double(70, 70, 100, 100);
g2d.fill(rect);
this.addMouseListener(this);
}
And then we override the paintComponent method and create our new Rectangle2D.Double object.
We then fill the rectangle with g2d.fill() and then add a mouse listener to the JPanel.
public void mousePressed(MouseEvent e)
{
if(rect.contains(e.getX(), e.getY()))
System.out.println("Rectangle clicked");
}
}
Finally, we need to see if that rectangle contains the point where the user clicked. To do this, simply see if the rectangle we created contains the user's click location by using the Rectangle2D.double's contains(int x, int y) method. That's it!
if I want to change the color of, for example arcs[0], how do I do that?
A line (or whatever) only exists as a bunch of pixels that were painted in the original color. To change its color you must change the current color and draw it again.

Categories

Resources