views:

127

answers:

1

I have a Graphics object of JPanel and that is working fine:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;

public class GraphicsTest extends JPanel
{
    private Graphics2D g2d;
    private String state;
    private int x, y;

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g2d = (Graphics2D) g;
        g2d.setClip(0, 0, getWidth(), getHeight());

        g2d.setColor(Color.BLACK);
        g2d.drawString("STATE: " + state, 5, 15);
        g2d.drawString("Mouse Position: " + x + ", " + y, 5, 30);

        g2d.setColor(Color.red);
        Rectangle2D r2d = new Rectangle2D.Double(x,y,10,10);
        g2d.draw(r2d);
        Test t = new Test();
        super.add(t);

        repaint();
    }

    public void setState(String state) { this.state = state; }
    public String getState() { return state; }

    public void setX(int x) { this.x = x; }
    public void setY(int y) { this.y = y; }
}

I was experimenting with a new Graphics component and when I instantiate a new Test and add it in GraphicsTest nothing happens. What is it that I am doing wrong?

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;

public class Test extends JComponent
{
    private Graphics2D g2d;
    private String state;
    private int x, y;

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g2d = (Graphics2D) g.create();

        g2d.setColor(Color.GREEN);
        g2d.fill(new Rectangle2D.Double(60, 60,
                10, 10));

        repaint();
    }

    public void setState(String state) { this.state = state; }
    public String getState() { return state; }

    public void setX(int x) { this.x = x; }
    public void setY(int y) { this.y = y; }

}

Thanks!

+2  A: 

You should add Test component only once and set a layout:

public GraphicTest()
{
    super();
    setLayout(new BorderLayout());
    add(new Test(),BorderLayout.CENTER);
}

Also, do not save Graphics2D objects because they will be invalid once paintComponent is finished and do not call repaint inside paintComponent. Use timers if you need animation and call repaint in setXXX methods that change the look of the component.

Ha
So then is it no possible to draw a new graphics object on top of a JPanel?
The Test component can have its own paintComponent method that draws something. You may want to make it transparent using `setOpaque(false)` method.
Ha
How do I add it to the JPanel though?
To add child component use `add` method. See the answer.
Ha
I have tried but they aren't displaying
Check the size and coordinates of the sub-component in debugger (`getSize` and `getLocation` methods). You need to either use a layout (recommended approach) or manage its size and position relative to parent component manually.
Ha