Adding classes to JPanel
Hi all,
Im a total Java 2D graphics newbie and im developing an application that will simulate the interaction between two robotic arms and a conveyor belt. The graphics will be completely simple and I will be using multple rectangles to represent an aerial view of the robots.
I have created a frame within which I have a JPanel to the right which will contain some controls and a large JPanel to the left that will show the robot animations.
My question is this, I have created a robot class with the following code:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
publicclass robotInextends JPanel{
privateint theta;
publicvoid paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g;
Shape oldClip = g2.getClip();
Rectangle2D e =new Rectangle2D.Float();
e.setRect(10,10,100,100);
g2.clip(e);
g2.setPaint(Color.red);
g2.setClip(oldClip);
g2.draw(e);
g2.fill(e);
}
}
In my main class I then create the gui but how do I add a robotIn() to the JPanel that I want it in. Also, is this the best way to go about it? Id appreciate any advice.
I tried the following code but it didnt work:
themainPanel =new JPanel();
getContentPane().add(themainPanel, BorderLayout.CENTER);
themainPanel.add(new robotIn());
[1981 byte] By [
JOzzieTa] at [2007-10-3 3:06:01]

import java.awt.*;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
public class RobotDesign {
public static void main(String[] args) {
RobotPanel robotPanel = new RobotPanel();
Controls controls = new Controls(robotPanel);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(robotPanel);
f.getContentPane().add(controls.getPanel(), "West");
f.setSize(400,300);
f.setLocation(200,200);
f.setVisible(true);
}
}
class RobotPanel extends JPanel {
Robot[] robots;
public RobotPanel() {
robots = new Robot[0];
setBackground(Color.yellow);
// With this (component) approach you'll have to figure out
// how you want to position the components and how they
// will interact in this container. Once you decide you
// will know how to finish this next line
//setLayout(); // FlowLayout default
}
public void addRobot(Robot robot) {
Robot[] temp = new Robot[robots.length+1];
System.arraycopy(robots, 0, temp, 0, robots.length);
temp[robots.length] = robot;
robots = temp;
add(robot); // layout manager takes care of the details
revalidate();
// and, if you elect absolute positioning (null layout)
// you will need to specify the location and size
// robot.setBounds(x, y, w, h);
}
}
class Controls {
RobotPanel robotPanel;
public Controls(RobotPanel rp) {
robotPanel = rp;
}
public JPanel getPanel() {
JPanel panel = new JPanel();
panel.setBackground(Color.blue);
panel.setPreferredSize(new Dimension(100, 200));
// add components to panel
// add listeners as needed
// one of these listeners might do
robotPanel.add(new Robot());
return panel;
}
}
class Robot extends JPanel {
private int theta;
public Robot() {
setPreferredSize(new Dimension(100, 150));
}
/** see JComponent api for proper signature */
protected void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g;
Shape oldClip = g2.getClip();
Rectangle2D e = new Rectangle2D.Float();
e.setRect(10,10,100,100);
g2.clip(e);
g2.setPaint(Color.red);
g2.setClip(oldClip);
g2.draw(e);
g2.fill(e);
}
}
The other approach is graphics in which you draw everything in the RobotPanel.
Just depends on what you want to do with your app.