Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with graphics

 
Brooks MacBevers
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I try to compile this program
import java.awt.*;

import javax.swing.*;

public class graphics {
public void paintComponent(Graphics g) {

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);

Image image = new ImageIcon("Pauls_sword.gif").getImage();
g.drawImage(image,3,4,this);

}
}

I get this error message
MacBeverstock:~/Documents/program Brooks$ javac graphics.java
graphics.java:14: cannot find symbol
symbol : method drawImage(java.awt.Image,int,int,graphics)
location: class java.awt.Graphics
g.drawImage(image,3,4,this);
^
1 error
MacBeverstock:~/Documents/program Brooks$


Thank for any help you can offer.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

A bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. No cutesie-pie "handles", please; we're all adults here at the Ranch. You'll find that most people aren't too interested in helping until you follow the rules round these parts.

You can change your display name here. Thanks for your immediate attention.
 
Brooks MacBevers
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. Is this good.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brooks M. B.:
OK. Is this good.

The policy requires a last name. Initials can be used for the first, but not last. Thanks!
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler error is giving you all the info you need to fix it. Look up drawImage in the API specs.

The only drawImage method signature with 4 arguments is drawImage(Image, int, int, ImageObserver).

You are passing graphics which does not implement ImageObserver. Fix that and it should compile ok.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by (yberknight yo:
...I get this error message
MacBeverstock:~/Documents/program Brooks$ javac graphics.java
graphics.java:14: cannot find symbol
symbol : method drawImage(java.awt.Image,int,int,graphics)
location: class java.awt.Graphics...

If you check the API for Graphics, you will find several overridden drawImage methods. It looks like you're trying to call this one...

drawImage(Image img, int x, int y, ImageObserver observer)

The problem is that your fourth argument ("this") is an instance of graphics, which is not an ImageObserver (since it does not implement the ImageObserver interface).
 
Brooks MacBevers
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot! For your help.
 
Brooks MacBevers
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What imageObsever should I use.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the component you're going to draw on -- the frame or panel. All AWT and Swing components implement this interface.
 
Brooks MacBevers
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run the revised version of the program it compiles. This is it
import java.awt.*;
import javax.swing.*;
public class graphics{
public void paintComponent(Graphics g) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
Image image = new ImageIcon("Pauls_sword.gif").getImage();
g.drawImage(image,3,4,frame);
}
}
Then I get this error message
MacBeverstock:~/Documents/program Brooks$ java graphics
Exception in thread "main" java.lang.NoSuchMethodError: main
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are supposed to put these four lines in a main() method, rather than in paintComponent():-

CR
 
Brooks MacBevers
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I get a some sample java for images.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the Swing tutorial examples -LunarPhases- display images.
 
Brooks MacBevers
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Brooks MacBevers
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there one for java 5 or java 1.5?
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
look all you do is have one file for you drawing?...

and one file that uploads your drawing.

so have

import javax.swing.JFrame;
public class ImageFrame
{
public static void main (String [] args)
{
//then have all your JFrame crap in here

// and don't forget..

frame.getContentPane().add(new ImagePanel);
//where ImagePanel is below.


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

public class ImagePanel extends JPanel
{
// you could have any buttons or variables in here if you want
}

public void init()
{
// then you add them in here to the panel
// and set the preferred size of panel and background color.
}

// now you can paint!

public void paintComponent (Graphics g)
{
// create your image here, or in the public class ImagePanel

// then do the

g.drawImage(w/e,w/e,w/e,w/e);

}

}

// then you would be done


-Justin-
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic