Last active
November 22, 2015 13:14
-
-
Save AnnaBoro/698b8c66962558cc13b4 to your computer and use it in GitHub Desktop.
abstractShape + interfaceDrawable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson6.shapes; | |
public abstract class AbstractShape implements Drawable{ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson6.shapes; | |
import java.awt.*; | |
public class Circle extends AbstractShape { | |
public Circle() { | |
} | |
@Override | |
public void draw(Graphics g) { | |
System.out.println("Draw circle"); | |
g.setColor(new Color(0, 240, 40)); | |
g.drawOval(50, 50, 100, 100); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson6.shapes; | |
import java.awt.*; | |
public interface Drawable { | |
public void draw(Graphics g); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson6.shapes; | |
public class Launcher3 { | |
public static void main(String[] args) { | |
Drawable[] d = {new Circle(), new Rectangle(), new Square(), new Triangle()}; | |
new ShapesTemplate(d); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson6.shapes; | |
import java.awt.*; | |
public class Rectangle extends AbstractShape { | |
public Rectangle() { | |
} | |
@Override | |
public void draw(Graphics g) { | |
System.out.println("Draw rectangle"); | |
g.setColor(new Color(255, 90, 90)); | |
int[] arrX = {100, 100, 150, 150}; | |
int[] arrY = {200, 300, 300, 200}; | |
g.fillPolygon(arrX, arrY, 4); | |
g.setColor(new Color(0, 255, 255)); | |
g.fillRect(250, 200, 100, 150); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson6.shapes; | |
import java.awt.Dimension; | |
import java.awt.Graphics; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.WindowConstants; | |
public class ShapesTemplate extends JPanel { | |
private Drawable[] shapes; | |
public ShapesTemplate(Drawable[] shapes) { | |
this.shapes = shapes; | |
if (shapes == null || shapes.length < 1) { | |
this.shapes = new Drawable[0]; | |
} | |
JFrame frame = new JFrame("DAY 6, 2D Graphics"); | |
frame.setLocation(750, 150); | |
frame.setMinimumSize(new Dimension(600, 400)); | |
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | |
frame.getContentPane().add(this); | |
frame.pack(); | |
frame.setVisible(true); | |
repaint(); | |
} | |
@Override | |
protected void paintComponent(Graphics g) { | |
for (Drawable d : shapes) { | |
d.draw(g); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson6.shapes; | |
import java.awt.*; | |
public class Square extends Rectangle{ | |
public Square() { | |
} | |
public void draw(Graphics g) { | |
System.out.println("Draw square"); | |
g.setColor(new Color(30, 230, 200)); | |
g.fillRect(400, 50, 50, 50); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson6.shapes; | |
import java.awt.*; | |
public class Triangle extends AbstractShape { | |
public Triangle() { | |
} | |
@Override | |
public void draw(Graphics g) { | |
System.out.println("Draw triangle"); | |
g.setColor(new Color(0, 0, 255)); | |
int[] arrayX = {300, 600, 450}; | |
int[] arrayY = {100, 100, 300}; | |
Graphics2D g2d = (Graphics2D) g; | |
g2d.setStroke(new BasicStroke(3)); | |
g.drawPolygon(arrayX, arrayY, 3); | |
g2d.setStroke(new BasicStroke()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment