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);
}
}

New Topic/Question
Reply




MultiQuote



|