diff --git a/README.md b/README.md index d61737d..8578632 100644 --- a/README.md +++ b/README.md @@ -3,15 +3,15 @@ SoftwareStudio Lab4 2016 # 組員一 -姓名: +姓名:楊翔閔 -學號: +學號:103062111 # 組員二 -姓名: +姓名:潘麗文 -學號: +學號:103062111 #Screenshot diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..8ff6259 --- /dev/null +++ b/README.txt @@ -0,0 +1,18 @@ +# SoftwareStudioLab4 +SoftwareStudio Lab4 2016 + +# 組員一 + +姓名:潘麗文 + +學號:103062111 + +# 組員二 + +姓名:楊翔閔 + +學號:103062124 + +#Screenshot + +![alt tag](/csc.png) \ No newline at end of file diff --git a/bin/MyJPanel.class b/bin/MyJPanel.class index 12bb529..d4016f4 100644 Binary files a/bin/MyJPanel.class and b/bin/MyJPanel.class differ diff --git a/src/MyJPanel.java b/src/MyJPanel.java index e252a2b..a9acb4e 100644 --- a/src/MyJPanel.java +++ b/src/MyJPanel.java @@ -1,4 +1,5 @@ - +import java.awt.Graphics; +import javax.swing.JPanel; import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; @@ -7,29 +8,36 @@ public class MyJPanel extends JPanel implements MouseMotionListener{ - - private Square square = new Square(100); - - public MyJPanel(){ - addMouseMotionListener(this); - } - protected void paintComponent(Graphics g){ - super.paintComponent(g); - // TODO Draw square and fill it with random color decided by method getRandomColor() - // You should use fillRect() - - } - - @Override - public void mouseDragged(MouseEvent e) { - // TODO Get mouse dragged position and change suqare's position - centerX = e.getX(); - centerY = e.getY(); - repaint(); - } - - @Override - public void mouseMoved(MouseEvent e) { - // No need to implement - } -} + + private Square square = new Square(100); + int centerX; + int centerY; + + public MyJPanel(){ + addMouseMotionListener(this); + } + protected void paintComponent(Graphics g){ + super.paintComponent(g); + // TODO Draw square and fill it with random color decided by method getRandomColor() + // You should use fillRect() + g.drawRect(centerX - square.getShapeHeight()/2, centerY - square.getShapeHeight()/2, square.getShapeWidth(), square.getShapeHeight()); + g.setColor(square.getRandomColor()); + g.getColor(); + g.fillRect(centerX - square.getShapeHeight()/2, centerY - square.getShapeHeight()/2, square.getShapeWidth(), square.getShapeHeight()); + } + + @Override + public void mouseDragged(MouseEvent e) { + // TODO Get mouse dragged position and change suqare's position + centerX = e.getX(); + centerY = e.getY(); + repaint(); + //centerX = square.getCenterX(e.getX()); + //centerY = square.getCenterY(e.getY()); + } + + @Override + public void mouseMoved(MouseEvent e) { + // No need to implement + } +} \ No newline at end of file diff --git a/src/Square.java b/src/Square.java index 96e07a8..bcf7bbf 100644 --- a/src/Square.java +++ b/src/Square.java @@ -1,50 +1,67 @@ -import java.awt.Color; -import java.util.Random; - -public class Square extends Shape { - - private int shapeWidth; - - public Square(int shapeWidth){ - // TODO Constructor - this.shapeWidth = shapeWidth; - } - - - @Override - public Color getRandomColor() { - // TODO Return random color produce from java.util.Random; - // Java 'Color' class takes 3 floats, from 0 to 1. - - // TODO Return color produced by three rgb floats. - return new Color(r, g, b); - } - - @Override - public int getShapeWidth(){ - // TODO Return Square width - return 0; - } - - @Override - public int getShapeHeight(){ - // TODO Return Square height - return 0; - } - - - @Override - public int getCenterX(int mouseX) { - // TODO Input mouse X position and return center X of square - return mouseX - shapeWidth/2; - } - - - @Override - public int getCenterY(int mouseY) { - // TODO Input mouse Y position and return center Y of square - return mouseY - shapeWidth/2; - } - - -} \ No newline at end of file +import java.awt.Color; +//import java.util.Random; + +public class Square extends Shape +{ + + private int shapeWidth; + //private int shapeHeight; + //private int mouseX; + //private int mouseY; + + public Square(int shapeWidth) + { + // TODO Constructor + this.shapeWidth = shapeWidth; + //this.shapeHeight = shapeHeight; + } + + + @Override + public Color getRandomColor() + { + float r, g, b;// TODO Return random color produce from java.util.Random; + //Random ran = new Random(); + + //r = ran.nextFloat(); + //g = ran.nextFloat(); + //b = ran.nextFloat(); + r = (float)Math.random(); + g = (float)Math.random(); + b = (float)Math.random(); + // Java 'Color' class takes 3 floats, from 0 to 1. + + // TODO Return color produced by three rgb floats. + return new Color(r, g, b); + } + + @Override + public int getShapeWidth() + { + // TODO Return Square width + return shapeWidth; + } + + @Override + public int getShapeHeight(){ + // TODO Return Square height + return shapeWidth; + } + + + @Override + public int getCenterX(int mouseX) + { + //this.mouseX = mouseX; + return mouseX - shapeWidth/2; + } + + + @Override + public int getCenterY(int mouseY) + { + //this.mouseY = mouseY; + // TODO Input mouse Y position and return center Y of square + return mouseY - shapeWidth/2; + } +}