Drawing image on the Panel

Draw Image icon on the Panel and make it move

Page 1 of 1

1 Replies - 6308 Views - Last Post: 06 March 2010 - 03:18 PM Rate Topic: -----

#1 rhaktkdl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-February 10

Drawing image on the Panel

Posted 05 March 2010 - 10:27 PM

Hello, I am trying to draw image icon on the Panel and to make it move on it.

I created two separated panels one for drawing the image and another for controlling the image drawed on another panel.

But, I am stuck with creating ActionListener method and drawing image on the panel..

Please help me out,

Turtle class
import javax.swing.*;
import java.awt.geom.AffineTransform;
import java.awt.*;


public class Turtle {
	double direction;
	int x;
	int y;
	ImageIcon icon;
	
	public Turtle() {
		x = 0;
		y = 0;
		direction = 0;
		icon = new ImageIcon("c:/myjava/work/hw07/turtle.gif");
	}
	public Turtle(int x, int y, double direction){
		this.x=x;
		this.y=y;
		this.direction=direction;
		ImageIcon icon = new ImageIcon("c:/myjava/work/hw07/turtle.gif");
		
	}
	public double turn(double deg){
		double rotation = deg*(Math.PI/180);
		return rotation;
	}
	
	public void move(int distance){
		x = (int) (distance*Math.cos(direction));
		y = (int) (distance*Math.sin(direction));
	}
	
	public void setX(int x) {
		this.x = x;
	}
	
	public void setY(int y) {
		this.y = y;
	}
	
	public int getX() {
		return this.x;
	}
	
	public int getY() {
		return this.y;
	}

	public static void drawImage(Graphics g, ImageIcon icon, int x, int y, double rotation){
		Graphics2D g2 = (Graphics2D)g;
		AffineTransform af = new AffineTransform();
		af.translate(x - icon.getIconWidth() / 2, y - icon.getIconHeight() / 2);
		af.rotate(rotation, icon.getIconWidth() / 2, icon.getIconHeight() / 2);
		g2.drawImage(icon.getImage(), af, null);
	}
}



TurtlePanel class (where I want to draw the image)
import javax.swing.*;

import java.awt.*;
import java.awt.geom.AffineTransform;

public class TurtlePanel extends JPanel {
	
	Turtle tt = new Turtle();
	JPanel TPanel = new JPanel();
	JLabel X = new JLabel("X: ");
	JLabel Y = new JLabel("Y: ");
	JLabel Dir = new JLabel("Dir: ");
	JLabel Xval = new JLabel("0");
	JLabel Yval = new JLabel("0");
	JLabel Dval = new JLabel("0");
	Graphics g;
	double direction;
	private ImageIcon icon;
	
	public 	TurtlePanel(){
		icon = tt.icon;
	}
	
	public TurtlePanel(ImageIcon icon){
		this.icon = icon;
	}
	
	
	public void setTPanel(){
		TPanel.setSize(300,300);
		TPanel.setLayout(null);
		TPanel.add(X);
		TPanel.add(Y);
		TPanel.add(Dir);
		TPanel.add(Xval);
		TPanel.add(Yval);
		TPanel.add(Dval);
		
		Dir.setBounds(0,3,25,10);
		X.setBounds(0,14,15,10);
		Y.setBounds(0,25,15,10);
		Dval.setBounds(26,3,10,10);
		Xval.setBounds(16,14,10,10);
		Yval.setBounds(16,25,10,10);
	}
	
	public void paintComponent(Graphics g){
		super.paint(g);
		tt.drawImage(g, icon, getX(), getY(), direction);
	}
	
}






TurtleRemotePanel (where I want to control the image)

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class TurtleRemotePanel{
	JPanel TRPanel = new JPanel();
	JLabel distance = new JLabel("Distance: ");
	JLabel degree = new JLabel("Degree: ");
	TextField distxt = new TextField();
	TextField degtxt = new TextField();
	Button forward = new Button("Forward");
	Button backward = new Button("Backward");
	Button turnLeft = new Button("Turn Left");
	Button turnRight = new Button("Turn Right");
	ImageIcon icon;
	TurtlePanel tp = new TurtlePanel();
	Turtle t = new Turtle();
	Graphics g;
	double deg;

	public TurtleRemotePanel(){
		icon = icon;
	}
	
	public TurtleRemotePanel(ImageIcon icon){
		this.icon = icon;
	}
	public void setTRPanel(){
		TRPanel.setLayout(null);
		TRPanel.add(distance);
		TRPanel.add(degree);
		TRPanel.add(distxt);
		TRPanel.add(degtxt);
		TRPanel.add(forward);
		TRPanel.add(backward);
		TRPanel.add(turnLeft);
		TRPanel.add(turnRight);
		TRPanel.setSize(300, 100);
		
		distance.setBounds(10,10,70,30);
		degree.setBounds(10,45,70,30);
		distxt.setBounds(85,10,60,30);
		degtxt.setBounds(85,45,60,30);
		forward.setBounds(150,10,60,30);
		backward.setBounds(215,10,60,30);
		turnLeft.setBounds(150,45,60,30);
		turnRight.setBounds(215,45,60,30);
		
		distxt.setEditable(true);
		degtxt.setEditable(true);
		
		forward.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				int dstc = Integer.parseInt(distxt.getText());
				t.move(dstc);
				tp.Xval.setText(String.valueOf(t.getX()));
				tp.Yval.setText(String.valueOf(t.getY()));
				tp.paintComponent(g);
				
			}
		});
			
	}
}
	





TurtleMain class (where I want to gather all classes and make a frame of the package)
import javax.swing.*;

import java.awt.*;


public class TurtleMain {
	
	public static void main(String[] args){
		JFrame frame = new JFrame("TurtleFrame");
		TurtlePanel tp = new TurtlePanel();
		tp.setTPanel();
		TurtleRemotePanel trp = new TurtleRemotePanel();
		trp.setTRPanel();
		frame.add(tp.TPanel);
		frame.add(trp.TRPanel);
		frame.setSize(300,400);
		frame.setLayout(null);
		tp.TPanel.setBounds(0,100,300,300);
		trp.TRPanel.setBounds(0,0,300,100);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Drawing image on the Panel

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Drawing image on the Panel

Posted 06 March 2010 - 03:18 PM

why are you declaring the drawImage method of the turtle class as static?
if it is static you should call it as Turtle.drawImage(..) instead of creating an Object of Turtle and then call the method (as you do in the TurtlePanel class.
However, it is not recommended to declare it as static.
How do you plan to move your turtle?
i'd recommend on having two more variables in the turtle class. dx and dy which will indicate the delta x and delta y the turtle moves.
to move just update the turtle x\y accordingly.
move right: x + dx
move left: x - dx
move up: y - dy
move down y + dy

when you draw the turtle, for each iteration of the "game loop", draw the updated turtle's location. it will create the feel of movement.
i suggest that you take a good look at Dogstopper's excellent blog. it has a lot of information about java game programming. (it is very cloase to what you try to do).this is an input manager:
http://www.dreaminco...&showentry=2210
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1