public static void main(String[] args){ JFrame app = new JFrame(); app.add(new R5A_1()); app.setSize(800, 800); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); app.setVisible(true); } interface Figure{ public void draw(Graphics g); public void repaint(); public boolean hit(int x, int y); public void moveTo(int x, int y); } static class Picture{ BufferedImage img;
int xpos, ypos, width, height; public Picture(String fname, int x, int y){
xpos = x; ypos = y; width = img.getWidth();height = img.getHeight(); } public void moveTo(int x, int y) { xpos = x; ypos = y; } public void draw(Graphics g) { int x = xpos-width/2, y = ypos-height/2; Graphics2D g2d = (Graphics2D)g; g.drawImage(img, x, y, null); try{ img = ImageIO.read(new File("1.jpg")); }catch(Exception ex){ System.err.println( ex ); }
} } static class Circle implements Figure{ Color col1; Color col2;
int size; int xpos, ypos, rad; public Circle(Color c1, Color c2, int x, int y, int r){ col1= c1; col2 = c2; xpos = x; ypos = y; rad = r; }
public boolean hit(int x, int y){ return(xpos-x)*(xpos-x) + (ypos-y)*(ypos-y) <= rad*rad; } public void moveTo(int x, int y){ xpos = x; ypos = y; } public void draw(Graphics g){ g.setColor(col1); g.fillOval(xpos-rad, ypos-rad, rad*2, rad*2); } public void repaint(){ col1 = col2; } public void resize(){ rad = rad*2; } } static class Rect implements Figure{ Color col1; Color col2; int xpos, ypos, width, height; public Rect(Color c1, Color c2, int x, int y, int w, int h){ col1= c1; col2 = c2; xpos = x; ypos = y; width = w; height = h; } public boolean hit(int x, int y){ return xpos-width/2 <= x && x <= xpos+width/2 && ypos-height/2 <= y && y <= ypos+height/2; } public void moveTo(int x, int y){ xpos = x; ypos = y; }
public void draw(Graphics g){ g.setColor(col1); g.fillRect(xpos-width/2, ypos-height/2, width, height); } public void repaint(){ col1 = col2; } }