It's not a secret anymore!
The moose likes Swing / AWT / SWT and the fly likes Swing graphics problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing graphics problem" Watch "Swing graphics problem" New topic
Author

Swing graphics problem

Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 101
Hi All.

Copying from the oriley head first java book, which i recommend. The following won't compile:


Error message:

C:\Users\rob\Documents\SimpleGui1B.java:30: cannot find symbol
symbol : class Graphics
location: class MyDrawPanel
public void paintComponent(Graphics g)
^
C:\Users\rob\Documents\SimpleGui1B.java:32: cannot find symbol
symbol : variable Color
location: class MyDrawPanel
g.setColor(Color.orange);
^
2 errors

Tool completed with exit code 1



any ideas what's wrong?

Thanks,
Rob.
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
You have to import two class: Graphics and Color.

paste it...

import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 101
Thanks, should have realised the imports.

What the book doesn't say is how to get Grapics "g" to display. Any ideas?
Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19738
    
  10

No, that's not right. You don't get a Graphics to display. A Graphics object is an internal object which can be used by classes which need to display data, such as JPanel.

Looking at that code, I see you have declared a class which extends JPanel, but you haven't used any instances of that class anywhere. That means your GUI isn't going to contain any MyDrawPanel objects.
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
Paul Clapham wrote:No, that's not right. You don't get a Graphics to display. A Graphics object is an internal object which can be used by classes which need to display data, such as JPanel.

Looking at that code, I see you have declared a class which extends JPanel, but you haven't used any instances of that class anywhere. That means your GUI isn't going to contain any MyDrawPanel objects.


Eclipse shows errors if you don't import these classes...
Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19738
    
  10

Dawid Skrzypczynski wrote:
Paul Clapham wrote:No, that's not right. You don't get a Graphics to display. A Graphics object is an internal object which can be used by classes which need to display data, such as JPanel.

Looking at that code, I see you have declared a class which extends JPanel, but you haven't used any instances of that class anywhere. That means your GUI isn't going to contain any MyDrawPanel objects.


Eclipse shows errors if you don't import these classes...


That's true. But was your statement supposed to be about what I posted in some way?
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 101
Yes Paul. How do i display the graphics object?

Thanks.
Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19738
    
  10

Paul Clapham wrote:No, that's not right. You don't get a Graphics to display. A Graphics object is an internal object which can be used by classes which need to display data, such as JPanel.

Looking at that code, I see you have declared a class which extends JPanel, but you haven't used any instances of that class anywhere. That means your GUI isn't going to contain any MyDrawPanel objects.
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 101
Ok.

So how do i display the rectangle defined in the PaintComponent method? I'm a bit lost with swing at the moment.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14991
    
  27

You'd have to create an instance of class MyDrawPanel and add it to the frame. Right now MyDrawPanel is an unused class in your code. Just declaring the class by itself doesn't automatically show it in your JFrame. Right now you're only adding a button to the JFrame (in line 18).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 20128
    
  30

And you should always start by calling super.paintComponent(g) when overriding paintComponent.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44433
    
  33
The reason for calling super.paintComponent is that you always start with a blank canvas after any changes.

I think this thread would sit better on the GUIs forum: moving.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 5038
    
  10

It's a common misconception the a Grphics object represents some kind of image. No. Think of a Graphics as a paintbrush, which paints to a surface -- commonly a screen element aka Component, or an image like BufferedImage, or, when printing with the Print API, to the paper in the printer.

Review the Graphics methods with this in mind and it'll be a lot clearer.


luck, db
There are no new questions, but there may be new answers.
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 101
Solved.

Thanks loads guys
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 5038
    
  10

Time for a tutorial: Lesson: Performing Custom Painting
Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 101


This displays an empty pane, what is missing guys?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 5038
    
  10

Check your spelling.

In general, when you intend to override a method, add the @Override annotation above the method.What do you get?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44433
    
  33
. . . and shouldn't that method have protected access?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swing graphics problem
 
It's not a secret anymore!