使用组合和继承在 Java 中绘制图片
Drawing a picture in Java using composition and inheritance
这是我第一次 post 来这里。
我是一名在高中学习 AP 计算机科学的学生。对于一个项目,我必须画一张篮球的图片。好的,很简单。问题是我必须使用正确的组合和继承来做到这一点。我总不能在主class里扔一堆图形方法吧。我已经记下了所有基本代码,但这是我需要帮助的...
- 我希望 class
Basketball
继承 super-class Circle
的所有内容。我是否通过在 class Basketball
标题中扩展 class Circle
来正确地做到这一点?
- 在class
Basketball
里面,我如何link到class行和classAirValve
才能显示正确的构图?
如您所见,我理解这些概念,但我不明白如何让它们很好地发生。
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.awt.Color;
import java.awt.Graphics;
/* The point of this lab is to draw a basketball using one example of
* proper inheritance and two examples of proper composition.
*/
public class Basketball extends Circle
{
public static void main(String[] args)
{
Lines line = new Lines();
AirValve airvalve = new AirValve();
}
}
class Lines
{
public Lines()
{
}
public void paintLine(Graphics g)
{
g.drawArc(300, 275, 200, 275, 295, 135); //left arc
g.drawLine(650, 150, 650, 650); //middle line
g.drawLine(400, 400, 900, 400); //cross line
g.drawArc(800, 275, 200, 275, 245, -135); //right arc
}
}
class Circle
{
public Circle()
{
}
public void paintCirc(Graphics g)
{
g.setColor(Color.orange);
g.fillOval(400, 150, 500, 500);
}
}
class AirValve
{
public AirValve()
{
}
public void paintAV(Graphics g)
{
g.setColor(Color.black);
g.fillOval(645, 625, 10, 10);
}
}
继承:
public class Circle extends JComponent {
//draws a circle
public void paintComponent(Graphics g) {
//...
}
}
public class Basketball extends Circle {
//draws a basketball
public void paintComponent(Graphics g) {
super.paintComponent(g); // this will draw the circle outline of your basketball
//draw the middle
}
}
组成:
//draws a circle
public static void drawCircle(Graphics g, int x, int y) {
}
//draws a cross
public static void drawCircle(Graphics g, int x, int y) {
}
//draws a basketball
public static void drawBasketball(Graphics g, int x, int y) {
drawCircle(g, x, y);
drawCross(g, x, y);
}
这是我第一次 post 来这里。 我是一名在高中学习 AP 计算机科学的学生。对于一个项目,我必须画一张篮球的图片。好的,很简单。问题是我必须使用正确的组合和继承来做到这一点。我总不能在主class里扔一堆图形方法吧。我已经记下了所有基本代码,但这是我需要帮助的...
- 我希望 class
Basketball
继承 super-classCircle
的所有内容。我是否通过在 classBasketball
标题中扩展 classCircle
来正确地做到这一点? - 在class
Basketball
里面,我如何link到class行和classAirValve
才能显示正确的构图?
如您所见,我理解这些概念,但我不明白如何让它们很好地发生。
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.awt.Color;
import java.awt.Graphics;
/* The point of this lab is to draw a basketball using one example of
* proper inheritance and two examples of proper composition.
*/
public class Basketball extends Circle
{
public static void main(String[] args)
{
Lines line = new Lines();
AirValve airvalve = new AirValve();
}
}
class Lines
{
public Lines()
{
}
public void paintLine(Graphics g)
{
g.drawArc(300, 275, 200, 275, 295, 135); //left arc
g.drawLine(650, 150, 650, 650); //middle line
g.drawLine(400, 400, 900, 400); //cross line
g.drawArc(800, 275, 200, 275, 245, -135); //right arc
}
}
class Circle
{
public Circle()
{
}
public void paintCirc(Graphics g)
{
g.setColor(Color.orange);
g.fillOval(400, 150, 500, 500);
}
}
class AirValve
{
public AirValve()
{
}
public void paintAV(Graphics g)
{
g.setColor(Color.black);
g.fillOval(645, 625, 10, 10);
}
}
继承:
public class Circle extends JComponent {
//draws a circle
public void paintComponent(Graphics g) {
//...
}
}
public class Basketball extends Circle {
//draws a basketball
public void paintComponent(Graphics g) {
super.paintComponent(g); // this will draw the circle outline of your basketball
//draw the middle
}
}
组成:
//draws a circle
public static void drawCircle(Graphics g, int x, int y) {
}
//draws a cross
public static void drawCircle(Graphics g, int x, int y) {
}
//draws a basketball
public static void drawBasketball(Graphics g, int x, int y) {
drawCircle(g, x, y);
drawCross(g, x, y);
}