一切!我似乎遇到了整合三角形的问题!这是主要的:
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);
}
}
这是三角类 import 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选项并且它们四处移动。当我从下拉菜单中选择Triangle时,什么都没有显示出来。它只是保持空白。
答案 0 :(得分:4)
基本问题是,你的Triangle
课正在产生一条直线......
g.fillPolygon(
new int[]{xPos, xPos + size, xPos + (2 * size)},
new int[]{yPos, yPos + size, yPos + (2 * size)}, 3);
因此,根据您提供的值,这将产生......
100x100
150x150
200x200
相反,您可以使用类似......
的内容g.fillPolygon(
new int[]{xPos, xPos + size, xPos - size},
new int[]{yPos, yPos + size, yPos + size}, 3);
哪会产生......
100x100
150x150
50x150
会产生类似......
的东西