Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,048 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,567 people online right now. Registration is fast and FREE... Join Now!




Trouble getting a Graphics2D in a JPanel to display anything

 
Reply to this topicStart new topic

Trouble getting a Graphics2D in a JPanel to display anything, It works from paintComponent...

penguin2
5 Aug, 2008 - 07:06 AM
Post #1

D.I.C Head
Group Icon

Joined: 22 Jul, 2008
Posts: 79



Thanked: 1 times
Dream Kudos: 25
My Contributions
I have a JFrame containing a JPanel, which is actually extended like this and has a Graphics2D:
CODE

import java.awt.*;
import javax.swing.*;


public class Main {


    public static void main(String[] args) {
        JFrame f1 = new JFrame();
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f1.setSize(300, 300);
        f1.setVisible(true);
        DotPanel d = new DotPanel();
        d.setBackground(new Color(123,21,15));
        f1.add(d);
        d.g2.setColor(new Color(0,0,0));
        d.g2.fillOval(50, 50, 50, 50);
        
    }

}




class DotPanel extends JPanel
{
    public Graphics2D g2;
    @Override//What exactly does this do?  Does it have anything to do with my problem?
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g2 = (Graphics2D) g;
    }
    

}


I wanted that to give me a black circle. It does not. Any suggestions/explanations would be appreciated.

Edited to much simpler version of program with the same problem.

This post has been edited by penguin2: 5 Aug, 2008 - 10:21 AM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Trouble Getting A Graphics2D In A JPanel To Display Anything
5 Aug, 2008 - 03:38 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,095



Thanked: 201 times
Dream Kudos: 75
My Contributions
This is not the way it works

OK have your DotPanel

You don't need (and don't want) a Graphics2D instance variable

When the GUI will determine that you need to repaint your panel (component added, frame resized, another window over your app, ...) it will call the paint() method of your DotPanel passing to it the Graphics object to use. (Not the one you think you have stored/saved/created)

If you decide that you want to redraw your panel according to a context change in your application do: d.repaint() (d beging your DotPanel instance) and wait for the GUI ( afew milliseconds) to call back your paint() method whith the Graphics object to use

Then in your paint() method you can draw what you want (you can cast the Graphics object to a Graphics2D object if you plan to use a Graphics2D method)

CODE

public void paint(Graphics g) {
   Graphics2d g2 = (Graphics2D) g;      // local variable not an instance one
   g2.setColor(....
   g2.DrawOval(....
}

User is online!Profile CardPM
+Quote Post

penguin2
RE: Trouble Getting A Graphics2D In A JPanel To Display Anything
5 Aug, 2008 - 04:00 PM
Post #3

D.I.C Head
Group Icon

Joined: 22 Jul, 2008
Posts: 79



Thanked: 1 times
Dream Kudos: 25
My Contributions
Thank you very much. I was really stuck there and could not find the answer.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 05:32PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month