• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Swing graphics problem

 
Rob Brew
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • 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.
 
Dawid Skrzypczynski
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Yes Paul. How do i display the graphics object?

Thanks.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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).
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you should always start by calling super.paintComponent(g) when overriding paintComponent.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • 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.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Solved.

Thanks loads guys
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator


This displays an empty pane, what is missing guys?
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 50175
79
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and shouldn't that method have protected access?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic