• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Swing graphics problem

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, should have realised the imports.

What the book doesn't say is how to get Grapics "g" to display. Any ideas?
 
Sheriff
Posts: 28400
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 28400
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Paul. How do i display the graphics object?

Thanks.
 
Paul Clapham
Sheriff
Posts: 28400
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok.

So how do i display the rectangle defined in the PaintComponent method? I'm a bit lost with swing at the moment.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And you should always start by calling super.paintComponent(g) when overriding paintComponent.
 
Marshal
Posts: 80654
477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rob Brew
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Solved.

Thanks loads guys
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Time for a tutorial: Lesson: Performing Custom Painting
 
Rob Brew
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This displays an empty pane, what is missing guys?
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80654
477
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and shouldn't that method have protected access?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic