Hi guys, why wont this work. I'm still learning and have no experience with graphics of any kind. Just trying to put the image onto the background. The link directory is right i think.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.*;	//inc javax.swing.ImageIcon

public class TheArea extends JFrame	{
	
	private Image aImage;
	private ImageIcon ii;
	
public static void main(String[] args)	
{
	new TheArea();	
	
}
	public TheArea()	
	{
		//JFrame frame = new JFrame();
		setSize(1000, 650);		//horizontal, vertical
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		setTitle("Area");
		setBackground(Color.GREEN);
		//frame.add(this);
	    //frame.setVisible(true);

		 try 
		 {
			 ii = new ImageIcon(this.getClass().getResource("D:\\Users\\Jonny\\Desktop\\Eclipse Work\\Radar.jpg"));	//ii = new ImageIcon(this.getClass().getResource("D:\\Users\\Jonny\\Desktop\\Radar.jpg"));
			 areaImage = ii.getImage(); 
		 }
		 catch(Exception e)
		 {
			 System.out.println(e.toString());
		 }

	}
	 public void paint(Graphics g) {

	        Graphics2D g2d = (Graphics2D) g;
	        g2d.drawImage(areaImage, 25, 10, null); 
	    }

}

Also do you guys know of or have a link to where i can find information or just how i can have a dot at the centre of a circle or background and have a line that it turns/spins around it?

Dani AI

Generated

Two quick, concrete fixes before anything else: correctly called out the missing class keyword, but the remaining problems are not syntax-only. In 's snippet the image load call uses getResource with a Windows absolute path (that will return null), and the code assigns areaImage while the field is named aImage (a compile-time/name bug). Also avoid overriding JFrame.paint for Swing painting — use a JPanel and override paintComponent, call super.paintComponent(g), and create the UI on the Event Dispatch Thread (SwingUtilities.invokeLater). Call setVisible(true) after you add components.

A simple, reliable pattern:

  • Load from disk with ImageIO.read(new File("D:\\...\\Radar.jpg")) or use new ImageIcon(path) for files; use getResource("/resourceName") only for classpath resources (leading slash = root).
  • Put drawing code into a JPanel subclass paintComponent(Graphics g) and call super.paintComponent(g) first.
  • Start animation with a javax.swing.Timer that updates an angle and calls repaint().

Example snippets (adapt to your names):

BufferedImage img = ImageIO.read(new File("D:\\Users\\Jonny\\Desktop\\Radar.jpg"));

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(img, 25, 10, null);
}

For a rotating “radar” line: keep an angle double, a Timer that increments it, then compute the end point with x = cx + (int)(Math.cos(angle)*r) and y = cy + (int)(Math.sin(angle)*r) and draw a line from center to that point.

Quick checklist: fix the variable name mismatch, stop using getResource with an absolute path, move painting into paintComponent, run GUI creation on the EDT, and start setVisible after assembling the frame. For Swing painting details and image loading, see the official Java tutorials: Painting in AWT and Swing and ImageIO.

Recommended Answers

All 2 Replies

Didi you intend this to be a class? If yes (which is probable) you forgot keyword class

#
public TheArea()
#
{
#
//JFrame frame = new JFrame();
#
setSize(1000, 650); //h

Fix and try this

#
class TheArea()
#
{
#
//JFrame frame = new JFrame();
#
setSize(1000, 650); //h

Sorry that was my fault when i copied it i must have missed the class keyword, so that wasn't the problem. Any other ideas? should this work?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.