Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ SoftwareStudio Lab4 2016

# 組員一

姓名:
姓名:楊翔閔

學號:
學號:103062111

# 組員二

姓名:
姓名:潘麗文

學號:
學號:103062111

#Screenshot

Expand Down
18 changes: 18 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# SoftwareStudioLab4
SoftwareStudio Lab4 2016

# 組員一

姓名:潘麗文

學號:103062111

# 組員二

姓名:楊翔閔

學號:103062124

#Screenshot

![alt tag](/csc.png)
Binary file modified bin/MyJPanel.class
Binary file not shown.
62 changes: 35 additions & 27 deletions src/MyJPanel.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
}
}
117 changes: 67 additions & 50 deletions src/Square.java
Original file line number Diff line number Diff line change
@@ -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;
}


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