2014-05-15 36 views
-8

全部!我看起来在整合三角形出现问题时出现了!这是主要的。Java不显示三角形

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.event.*; 


// Class definition 

public class Assignment extends JPanel implements ActionListener { 

private JButton moveLeft, moveRight, makeBigger, makeSmaller; 
private JComboBox shapeChoice; 
private Point firstPoint; 
private Circle firstCircle; 
private Square firstSquare; 
private Doughnut firstDoughnut; 
private Point aShape; 
private Triangle firstTriangle; 
private JLabel cat; 


// Constructor method declaration 

public Assignment() { 
    cat = new JLabel("Area:"); 
    add(cat); 
    moveLeft = new JButton("Move Left"); 
    add(moveLeft); 
    moveLeft.addActionListener(this); 

    moveRight = new JButton("Move Right"); 
    add(moveRight); 
    moveRight.addActionListener(this); 

    makeBigger = new JButton("Make Bigger"); 
    add(makeBigger); 
    makeBigger.addActionListener(this); 

    makeSmaller = new JButton("Make Smaller"); 
    add(makeSmaller); 
    makeSmaller.addActionListener(this); 

    shapeChoice = new JComboBox(); 
    shapeChoice.addItem("Point"); 
    shapeChoice.addItem("Square"); 
    shapeChoice.addItem("Circle"); 
    shapeChoice.addItem("Doughnut"); 
    shapeChoice.addItem("Triangle"); 
    add(shapeChoice); 
    shapeChoice.addActionListener(this); 


    // Instantiate shape classes 
    firstPoint = new Point(); 
    firstCircle = new Circle(); 
    firstSquare = new Square(); 
    firstDoughnut = new Doughnut(); 
    firstTriangle = new Triangle(); 
    // Initially assign aShape a Point object 
    aShape = firstPoint; 

    // Create a new window using the Swing class JFrame and add this panel 
    makeFrame(); 
} 

public void actionPerformed(ActionEvent e) { 
    // Manipulate respective object when buttons pressed 
    if (e.getSource() == moveLeft) { 
      aShape.moveXY(-20, 0); 
    } 

    else if (e.getSource() == moveRight) { 
      aShape.moveXY(20, 0); 
    } 

    else if (e.getSource() == makeBigger) { 
      aShape.reSize(20); 
    } 

    else if (e.getSource() == makeSmaller) { 
      aShape.reSize(-20); 
    } 

    // checkbox assigns object to aShape 
    else if (e.getSource() == shapeChoice) { 
     if (shapeChoice.getSelectedItem().equals("Circle")) 
      aShape = firstCircle; 

     else if (shapeChoice.getSelectedItem().equals("Square")) 
      aShape = firstSquare; 
     else if (shapeChoice.getSelectedItem().equals("Point")) 
      aShape = firstPoint; 
     else if (shapeChoice.getSelectedItem().equals("Doughnut")) 
      aShape = firstDoughnut; 
     else if (shapeChoice.getSelectedItem().equals("Triangle")) 
      aShape = firstTriangle; 
    } 


    // Ask Java to repaint the window to reflect the updates made 
    repaint(); 
} 

// Provide this method to instruct Java what to do when repainting the window 
public void paintComponent(Graphics g) { 

    super.paintComponent(g); 

    // Call the respective methods from respective class to print the new co-ordinate values 
    aShape.outputXYCoords(g); 

    // Call the respective methods from respective class to redraw the shape 
    aShape.display(g); 
} 


// Create a window frame 

public void makeFrame() { 

    // Instantiate a window frame using Swing class JFrame 
    JFrame frame = new JFrame("PolyMorphDemo"); 

    // When the window is closed terminate the application 
    frame.addWindowListener(new WindowAdapter() { 
     public void windowClosing(WindowEvent e) { 
      System.exit(0); 
     } 
    }); 

    // set initial size of window 
    frame.setSize(800, 600); 

    // add the current object to the centre of the frame and make visible 
    frame.getContentPane().add(this, BorderLayout.CENTER); 
    frame.setVisible(true); 
} 

}

这是三角形类 进口java.awt中的*;

// Class definition 
public class Triangle extends Point { 
protected int size; 

// Constructor method declaration 
public Triangle() { 
    super(); 
    // set size and offset y co-ordinate to display below point 
    size = 50; 
    yPos = 100; 
    xPos = 100; 
} 

// Method to update new size of the circle 
public void reSize(int newSize) { 
    size = size + newSize; 
} 

// An overiding method to display circle on screen as inherited method from class Point not suitable 
public void display(Graphics g) { 
    // Call method fillRect from class Graphics to draw the square of variable length of size 
    g.fillPolygon(new int[]{xPos, xPos+size, xPos+(2*size)}, new int[]{yPos, yPos+size, yPos+(2*size)},3); 
} 
} 

如果有人能告诉我我究竟做了什么错,或者可能是什么问题,我会非常感谢!

这里是Point类: import java.awt。*;

// Class definition 
public class Point extends Shape 
{ 

protected int xPos, yPos; 
protected int startX, startY, endX, endY; 
// Constructor method declaration 

public Point() { 
    xPos = 100; 
    yPos = 100; 
} 

// Method to update new position of point on screen 
public void moveXY(int newX, int newY) { 
    xPos = xPos +newX; 
    yPos = yPos +newY; 
} 

// Cannot resize a point but method required to support polymorphism 
public void reSize(int newSize) { 
} 

// Method to print X,Y co-ordinates of point on screen 
public void outputXYCoords(Graphics g) { 
    g.drawString("X Coord = " + xPos + 
      " Y Coord = " + yPos, 5, yPos-10); 
} 

// Method to draw point on screen at new X.Y position 
public void display(Graphics g) { 
    g.fillOval(xPos, yPos, 5, 5); 
} 
} 

对不起,缺乏解释,有点累。基本上程序启动。它完美地显示了Circle/Square/Donut选项,并且它们四处移动。当我从下拉菜单中选择三角形时,什么也没有显示出来。它只是空着。

+1

尝试调试它。 –

+0

你的'Point'类在哪里?发布不完整的代码并不是真的有帮助 – MadProgrammer

+1

你能描述一下究竟出了什么问题吗?只是告诉我们你有一个问题不是很有帮助。 –

回答

4

的基本问题,是你的Triangle类产生挺直线...

g.fillPolygon(
     new int[]{xPos, xPos + size, xPos + (2 * size)}, 
     new int[]{yPos, yPos + size, yPos + (2 * size)}, 3); 

因此,根据您提供的值,这将产生...

  1. 100x100
  2. 150x150
  3. 200x200

相反,你可以使用类似...

g.fillPolygon(
     new int[]{xPos, xPos + size, xPos - size}, 
     new int[]{yPos, yPos + size, yPos + size}, 3); 

将产生...

  1. 100x100
  2. 150x150
  3. 50x150

WHI ch会产生类似...

Triangle

+0

非常感谢!我不敢相信我是多么的愚蠢.....谢谢! :) – user3186187