Richardweber32
Richardweber32

Reputation: 168

java graphics2D inside JPanel

I am trying to draw some simple shapes onto a JPanel but am having some difficulty. Apologies if this question appears to have been answered before but the other answers don't seem to help.

I followed a simple tutorial and was successful in drawing some basic shapes onto a JFrame, but when I moved the code how it was into a new class which extends JPanel, nothing appears on screen.

public class TestingGraphics extends JFrame{

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here

    new TestingGraphics();

}

public TestingGraphics(){

    this.setSize(1000,1000);
    this.setPreferredSize(new Dimension(1000,1000));
    this.setTitle("Drawing tings");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.add(new TestingPanelGraphics(), BorderLayout.CENTER);
    this.setVisible(true);

}

public class TestingPanelGraphics extends JPanel {

public TestingPanelGraphics(){

    this.setSize(1000,1000);
    this.setPreferredSize(new Dimension(1000,1000));
    this.add(new DrawStuff(), BorderLayout.CENTER);
    revalidate();
    repaint();
    this.setVisible(true); //probably not necessary

}

private class DrawStuff extends JComponent{

    @Override
    protected void paintComponent(Graphics g){
        super.paintComponent(g);

        Graphics2D graph2 = (Graphics2D) g;

        graph2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);



        Shape rootRect = new Rectangle2D.Float(100, 100, 1000, 500);

        graph2.setColor(Color.BLACK);
        graph2.draw(rootRect);
}

I've tried setting preferred size, and revalidating and repainting. I've added a call to super.paintComponent, although neither of those two were necessary when I was drawing straight onto a JFrame. I ensured I was overriding paintComponent and changed it from public to protected. All following advice from similar threads, but nothing seems to work. I've stepped through in debugger mode and ensured it goes into the right method, and even watched it go into the paintManager, but still nothing appears on the window.

Upvotes: 1

Views: 6219

Answers (1)

Sergiy Medvynskyy
Sergiy Medvynskyy

Reputation: 11327

You've forget to set the appropriate layout manager.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestingGraphics extends JFrame{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        new TestingGraphics();

    }

    public TestingGraphics(){

        this.setSize(1000,1000);
        this.setPreferredSize(new Dimension(1000,1000));
        this.setTitle("Drawing tings");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.add(new TestingPanelGraphics(), BorderLayout.CENTER);
        this.setVisible(true);

    }

    public class TestingPanelGraphics extends JPanel {

        public TestingPanelGraphics(){
            setLayout(new BorderLayout());
            this.setPreferredSize(new Dimension(1000,1000));
            this.add(new DrawStuff(), BorderLayout.CENTER);
            revalidate();
            repaint();
            this.setVisible(true); //probably not necessary

        }

        private class DrawStuff extends JComponent{

            @Override
            protected void paintComponent(Graphics g){
                super.paintComponent(g);

                Graphics2D graph2 = (Graphics2D) g;

                graph2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);



                Shape rootRect = new Rectangle2D.Float(100, 100, 1000, 500);

                graph2.setColor(Color.BLACK);
                graph2.draw(rootRect);
            }
        }
    }
}

When you extends JFrame your default layout is the BorderLayout, but when you extends JPanel your default layout is the FlowLayout.

Upvotes: 5

Related Questions