• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with graphics

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. Is this good.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot! For your help.
 
Brooks MacBevers
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What imageObsever should I use.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I get a some sample java for images.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
Brooks MacBevers
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there one for java 5 or java 1.5?
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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-
 
Good heavens! What have you done! Here, try to fix it with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic