使用组合和继承在Java中绘制图片

时间:2015-12-09 16:13:57

标签: java inheritance composition

这是我在这里的第一篇文章。 我是一名高中学习AP计算机科学的学生。对于一个项目,我必须画一个篮球的图片。好的,够容易的。问题是我必须使用正确的组合和继承来做到这一点。我不能只在主类中抛出一堆图形方法。我已经掌握了所有基本代码,但这是我需要帮助的地方......

  1. 我希望班级Basketball能够继承超级班级Circle中的所有内容。我是否通过在课程Circle标题中扩展课程Basketball来正确地做到了这一点?
  2. 在课程Basketball内,我如何链接到班级班级和班级AirValve以显示正确的作文?
  3. 正如你所看到的,我理解这些概念,但我不明白如何让它们很好地发生。

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

1 个答案:

答案 0 :(得分:0)

继承:

Imports <xmlns:old="http://www.deitel.com/nutrition">
Imports <xmlns:new="http://www.deitel.com/nutrition2">

Public Class NutritionLINQ

    Private Sub convertButton_Click(ByVal sender As System.Object,
        ByVal e As System.EventArgs) Handles CombineButton.Click

        Try
            ' attempt to load files
            Dim newDocument = XDocument.Load("Nutrition.xml")
            Dim oldDocument = XDocument.Load("Nutrition2.xml")

            ' convert from old to new format
            oldDocument = Transform(oldDocument)

            ' combine documents and write to output file
            SaveFinalDocument(newDocument, oldDocument)

            ' tell user we succeeded
            MessageBox.Show("Documents successfully combined.")
        Catch ' catch everything
            ' inform the user that there was a problem combining
            MessageBox.Show("Files could not be combined.")
            MessageBox.Show("This was probably caused by an I/O error.")
        End Try
    End Sub ' Main

    Private Function Transform(ByVal document As XDocument) As XDocument

        Dim newDocumentRoot =
            <nutrition>
                <%= From nutrition In document.Root.Elements()
                    Select
                    <nutrition information=
                         <%= nutrition.<old:title>.Value %>
                         <%= nutrition.<old:size>.Value %>
                         <%= nutrition.<old:calories>.Value %>
                         <%= nutrition.<old:caloriesFat>.Value %>
                         <%= nutrition.<old:fat>.Value %>
                         <%= nutrition.<old:saturedFat>.Value %>
                         <%= nutrition.<old:choloesterol>.Value %>
                         <%= nutrition.<old:Sodium>.Value %>
                         <%= nutrition.<old:carbohydrates>.Value %>
                         <%= nutrition.<old:fiber>.Value %>
                         <%= nutrition.<old:sugar>.Value %>
                         <%= nutrition.<old:protein>.Value %>
                         <%= nutrition.<old:description>.Value %>

                         <%= nutrition.<new:vitaminA>.Value %>
                         <%= nutrition.<new:vitaminC>.Value %>
                         <%= nutrition.<new:vitaminE>.Value %>
                         <%= nutrition.<new:zinc>.Value %>

                    />
                %>
            </nutrition>

        Return New XDocument(newDocumentRoot) 

    End Function 

    Private Sub SaveFinalDocument(ByVal document1 As XDocument,
        ByVal document2 As XDocument)

        ' create root element, and fill with the elements
        ' contained in the roots of both documents
        Dim root =
           <nutrition>
               <%= document1.Root.Elements() %>
               <%= document2.Root.Elements() %>
           </nutrition>

        Dim finalDocument As New XDocument(root) ' create new document
        finalDocument.Save("output.xml") ' save document to file
    End Sub ' SaveFinalDocument

End Class

和作文:

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