Drawing images?

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 416 Views - Last Post: 12 September 2012 - 08:30 PM Rate Topic: -----

#1 picyoko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-September 12

Drawing images?

Posted 10 September 2012 - 07:44 PM

I am trying to draw an image to the screen:

class 1:

import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;

public class Main extends JFrame{
	
	String GameNameLocal = new String();
	public static final String Hi = "Unnamed";
	
	public static void main(String[]args){
		MainFrame();
	}
	
	public static void MainFrame(){
		JFrame Frame = new JFrame();
		Frame.setTitle(Hi);
		Frame.setVisible(true);
		Frame.setSize(640, 640);
		Frame.setResizable(false);
		Frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
		Frame.getContentPane().setBackground(Color.BLACK);
		Menu menu = new Menu();//Call Instance of class
		Frame.add(menu);
	}
	
}


class 2:

public class Menu extends JPanel{
	
	Image image;
	
	int x;
	int y;
	
	public void run(){
		SetImage();
	}

	public void SetImage(){
		image = new ImageIcon("/res/Image.png").getImage();
	}

	public void paint(Graphics g){
		super.paintComponent(g);
		setBackground(Color.BLACK);
		repaint();
	        g.drawImage(image,25,25,null);
	}
}


All I get is a black screen, and I don't know why.
Could anyone help me?

Is This A Good Question/Topic? 0
  • +

Replies To: Drawing images?

#2 Sheph  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 427
  • View blog
  • Posts: 998
  • Joined: 12-October 11

Re: Drawing images?

Posted 10 September 2012 - 08:53 PM

The image needs to be in the right directory:

Put it in the folder given by
System.getProperty("user.dir");
Was This Post Helpful? 0
  • +
  • -

#3 picyoko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-September 12

Re: Drawing images?

Posted 10 September 2012 - 08:55 PM

I checked, and my image is in the directory I am writing.
Was This Post Helpful? 0
  • +
  • -

#4 Sheph  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 427
  • View blog
  • Posts: 998
  • Joined: 12-October 11

Re: Drawing images?

Posted 10 September 2012 - 09:30 PM

Oh call menu.run() in your main method. You never call the code that loads the Image. There's really no need for a delegate method in the same class. SetImage does the same thing as run.

A couple more things:
  • Don't call repaint() inside of paint(). It will cause an infinite loop.
  • Call super.paint() instead of super.paintComponent() or change the method you override from paint() to paintComponent(). One or the other
  • Java method names start with a lowercase letter. (SetImage -> setImage)

Was This Post Helpful? 0
  • +
  • -

#5 picyoko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-September 12

Re: Drawing images?

Posted 10 September 2012 - 09:35 PM

I fixed everything, but I still get a black screen.
Was This Post Helpful? 0
  • +
  • -

#6 Sheph  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 427
  • View blog
  • Posts: 998
  • Joined: 12-October 11

Re: Drawing images?

Posted 10 September 2012 - 11:10 PM

Post the updated code so we can see the exact changes.
Was This Post Helpful? 0
  • +
  • -

#7 picyoko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-September 12

Re: Drawing images?

Posted 11 September 2012 - 05:30 PM

(Out of order)
Second:

package game.Source;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.MouseInputListener;
import java.util.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Menu extends JPanel{
	
	Image here;
	
	int playx;
	int playy;

	public void setImage(){
	here = new ImageIcon("/Unnamed Game/res/Image.png").getImage();
	}

	public void paint(Graphics g){
		super.paint(g);
		setBackground(Color.BLACK);
		g.drawImage(here , 25, 25, null);
	}
}


First:

import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;

public class Main extends JFrame{
	
	String GameNameLocal = new String();
	public static final String hi = "Unnamed";
	
	public static void main(String[]args){
		MainFrame();
	}
	
	public static void MainFrame(){
		JFrame Frame = new JFrame();
		Frame.setTitle(Hi);
		Frame.setVisible(true);
		Frame.setSize(640, 640);
		Frame.setResizable(false);
		Frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
		Frame.getContentPane().setBackground(Color.BLACK);
		Menu menu = new Menu();//Call Instance of class
		menu.setImage();
		Frame.add(menu);
	}
	
}

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8015
  • View blog
  • Posts: 31,118
  • Joined: 06-March 08

Re: Drawing images?

Posted 11 September 2012 - 05:39 PM

add ypur JPanel to your JFrame before you set the JFrame visible(true)
or call validate() after you added it

and setBackground() needs to be called only once
Was This Post Helpful? 0
  • +
  • -

#9 picyoko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-September 12

Re: Drawing images?

Posted 11 September 2012 - 05:41 PM

Ok, I called it before the setVisible, but the problem is still there.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8015
  • View blog
  • Posts: 31,118
  • Joined: 06-March 08

Re: Drawing images?

Posted 11 September 2012 - 05:49 PM

Works for me
You probably don't read your Image correctly. Add:
	here = new ImageIcon("/Unnamed Game/res/Image.png").getImage();
	System.out.println(here);


we'll see what you read
Was This Post Helpful? 0
  • +
  • -

#11 picyoko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-September 12

Re: Drawing images?

Posted 11 September 2012 - 05:51 PM

It outputs "Here", but still no image.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8015
  • View blog
  • Posts: 31,118
  • Joined: 06-March 08

Re: Drawing images?

Posted 11 September 2012 - 06:05 PM

If reading of the Image had worked it should print something like:

sun.awt.image.ToolkitImage@4a79717e
Was This Post Helpful? 0
  • +
  • -

#13 picyoko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-September 12

Re: Drawing images?

Posted 11 September 2012 - 06:06 PM

No, For me, It only prints Here.
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8015
  • View blog
  • Posts: 31,118
  • Joined: 06-March 08

Re: Drawing images?

Posted 11 September 2012 - 07:11 PM

so back to post #10
your Image is not there
Was This Post Helpful? 0
  • +
  • -

#15 picyoko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-September 12

Re: Drawing images?

Posted 12 September 2012 - 07:28 PM

Sorry, misread it, here is it pasted:

sun.awt.image.ToolkitImage@148bd3
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2