aspose file tools*
The moose likes Beginning Java and the fly likes Help with graphics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with graphics" Watch "Help with graphics" New topic
Author

Help with graphics

Brooks MacBevers
Greenhorn

Joined: Apr 23, 2006
Posts: 9
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Brooks MacBevers
Greenhorn

Joined: Apr 23, 2006
Posts: 9
OK. Is this good.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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!


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
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.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Apr 23, 2006
Posts: 9
Thanks alot! For your help.
Brooks MacBevers
Greenhorn

Joined: Apr 23, 2006
Posts: 9
What imageObsever should I use.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Use the component you're going to draw on -- the frame or panel. All AWT and Swing components implement this interface.
Brooks MacBevers
Greenhorn

Joined: Apr 23, 2006
Posts: 9
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

Joined: Oct 13, 2005
Posts: 39814
    
  28
You are supposed to put these four lines in a main() method, rather than in paintComponent():-

CR
Brooks MacBevers
Greenhorn

Joined: Apr 23, 2006
Posts: 9
Can I get a some sample java for images.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42601
    
  65
One of the Swing tutorial examples -LunarPhases- display images.


Ping & DNS - my free Android networking tools app
Brooks MacBevers
Greenhorn

Joined: Apr 23, 2006
Posts: 9
Thanks
Brooks MacBevers
Greenhorn

Joined: Apr 23, 2006
Posts: 9
Is there one for java 5 or java 1.5?
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
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-


You down with OOP? Yeah you know me!
 
Consider Paul's rocket mass heater.
 
subject: Help with graphics