aspose file tools*
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
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: 99
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: 99
Thanks, should have realised the imports.

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

Joined: Oct 14, 2005
Posts: 18570
    
    8

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
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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: 99
Yes Paul. How do i display the graphics object?

Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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: 99
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: 14156
    
  19

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 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

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


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

Joined: Oct 13, 2005
Posts: 38881
    
  23
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: 4546
    
    5

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: 99
Solved.

Thanks loads guys
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4546
    
    5

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

Joined: Jun 23, 2011
Posts: 99


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

Joined: May 03, 2008
Posts: 4546
    
    5

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: 38881
    
  23
. . . and shouldn't that method have protected access?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Swing graphics problem