// class1
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.Dimension;
/**
*
* @author User
*/
public class Buildings {
private Image img,img2;
private String s;
private int b_index;
private BufferedImage image, offScreenImage;
private int width,height;
private String s_shopping="School";
private String building="Building";
public Buildings(){
try{
img= ImageIO.read(new File("building.jpg"));
img2=ImageIO.read(new File("s1.jpg"));
}
catch(IOException e){
System.out.println("Image not found");
}
}
public void PaintBuilding(Graphics2D offScreenG2D, int x, int y){
offScreenG2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
offScreenG2D.drawImage(img, x,y,null);
offScreenG2D.drawString(building, x+1, y+1);
}
public void PaintSchool(Graphics2D g2d, int x, int y){
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.drawImage(img2, x,y,null);
g2d.drawString(s_shopping, x+1, y+1);
}
public String getString(){
return s;
}
public void setString(String s){
this.s=s;
}
public void setBIndex(int index){
this.b_index=index;
}
public int getBIndex(){
return b_index;
}
}
//class2
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
class BuildingOptions extends JFrame{
JRadioButton rb1;
JRadioButton rb2;
ButtonGroup bgroup;
private int build_index;
JPanel panelRbutton;
// modify the line below to the second line
// Buildings build= new Buildings();
static Buildings build;
// modify the constructor as shown
public BuildingOptions( Buildings build ){
super("Choose a building");
// add this line:
this.build = build;
build.setBIndex(build_index);
rb1= new JRadioButton("Add a School");
rb2=new JRadioButton("Add a Shopping Mall");
//rb3=new JRadioButton()
bgroup= new ButtonGroup();
bgroup.add(rb1);
bgroup.add(rb2);
rb1.addItemListener(new ButtonListener());
rb2.addItemListener(new ButtonListener());
panelRbutton= new JPanel();
panelRbutton.add(rb1);
panelRbutton.add(rb2);
this.add(panelRbutton);
this.setSize(250, 300);
this.setBackground(Color.LIGHT_GRAY);
this.setVisible(true);
repaint();
}
public class ButtonListener implements ItemListener{
public void itemStateChanged(ItemEvent e){
if(e.getSource()==rb1){
System.out.println("School");
build_index=1;
build.setBIndex(build_index);
repaint();
System.out.println("Index here is:"+build.getBIndex());
}
if(e.getSource()==rb2){
System.out.println("Building");
build_index=2;
build.setBIndex(build_index);
repaint();
System.out.println("Index here is:"+build.getBIndex());
}
}
// commented out main() method to avoid confusion:
/*
public static void Main(String [] args){
BuildingOptions frame1= new BuildingOptions(build);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setVisible(true);
}*/
}
}
//class3
import javax.swing.*;
//import Buildings.MouseDrag;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MainDrawing extends JPanel
{
int id=2;
int index;
int height, width, x,y;
// this creates Object1-A, 'b'
Buildings b= new Buildings();
boolean buildingS=false;
JButton b1;
BuildingOptions bOpt;
MouseDrag mouseDrag;
public MainDrawing()
{
index=index;
this.setBackground(Color.white);
this.setSize(300,150);
//mouseDrag = new MouseDrag();
// addMouseListener(mouseDrag);
//addMouseMotionListener(mouseDrag);
// b.setBIndex(index);
b1=new JButton("Add Buildings");
b1.setSize(25, 50);
b1.addActionListener(new ButtonListener());
add(b1);
mouseDrag = new MouseDrag();
addMouseListener(mouseDrag);
addMouseMotionListener(mouseDrag);
}
private final class MouseDrag extends MouseAdapter {
private boolean dragging = false;
private Point last;
@Override
public void mousePressed(MouseEvent m) {
last = m.getPoint();
// dragging = isInsideEllipse(last);
if (!dragging) {
x = last.x;
y = last.y;
width = 0;
height = 0;
}
// repaint();
}
@Override
public void mouseReleased(MouseEvent m) {
//last = null;
dragging = true;
repaint();
}
@Override
public void mouseDragged(MouseEvent m) {
int dx = m.getX() - last.x;
int dy = m.getY() - last.y;
if (dragging) {
x += dx;
y += dy;
} else {
width += dx;
height += dy;
}
last = m.getPoint();
repaint();
}
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d=(Graphics2D)g;
System.out.println("Index is:"+b.getBIndex());
if(b.getBIndex()==1)
b.PaintSchool(g2d, x, y);
if(b.getBIndex()==2)
b.PaintBuilding(g2d, x, y);
}
public class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1)
// this creates Object2, passing a reference to Object1-A
bOpt= new BuildingOptions( b );
bOpt.setVisible(true);
bOpt.setSize(250, 250);
System.out.println("Index in button listener in class buttonListener:"+b.getBIndex());
repaint();
/*index=1;
// buildingS=true;
b.setBIndex(index);
repaint();
System.out.println("Index:"+b.getBIndex());*/
}
}
public static void main(String args[]){
MainDrawing md= new MainDrawing();
JFrame f=new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//f.setBackground(Color.yellow);
f.setContentPane(md);
f.setVisible(true);
f.setSize(350,350);
f.setVisible(true);
}
}