一个简单的Java程序 - 在向面板添加形状时遇到麻烦

时间:2015-11-23 04:32:26

标签: java

我是Java语言和Stack Overflow的新手。我想在Panel中添加形状以模仿地图。问题在标题中解释 - 我不知道如何将形状添加到我的面板中。请注意,我大约完成了这个程序的一半,这意味着它还没有我想要的所有功能。我有两个课程 - Map.javaMapShapes.java

我的第一堂课。

/** Program that maps a room in the house and sees how things will look from an overhead, 2d perspective. ;
 * @author: James ;
 * 
* */

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

public class Map extends JPanel{ 
  MapShapes[] shapeArray = new MapShapes[20];
  int count = 0;
  private JLabel askLength, askWidth, askX, askY;
  private JTextField length, width, x, y; 
  private JButton addButton;
  private DrawingPanel drawPanel = new DrawingPanel();

 public static void main(String[] args){
JFrame frame = new JFrame("Map");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Map panel = new Map();
frame.getContentPane().add(panel);

frame.pack();
frame.setVisible(true);
}// end of main;

 public Map(){
  JPanel controlPanel = new JPanel();

addButton = new JButton("Add a shape");

askX = new JLabel("X coordinate: ");
askY = new JLabel("Y coordinate: ");
askLength = new JLabel("Length: ");
askWidth = new JLabel("Width: ");

x = new JTextField(3);
y = new JTextField(3);
length = new JTextField(3);
width = new JTextField(3);

length.addActionListener(new Listener());
width.addActionListener(new Listener());
x.addActionListener(new Listener());
y.addActionListener(new Listener());

controlPanel.add(askX);
controlPanel.add(x);
controlPanel.add(askY);
controlPanel.add(y);
controlPanel.add(askLength);
controlPanel.add(length);
controlPanel.add(askWidth);
controlPanel.add(width);
controlPanel.add(addButton);
controlPanel.setPreferredSize(new Dimension (100, 400));

add(controlPanel);   
add(drawPanel);

 }// end of constructor;

private class DrawingPanel extends JPanel{

/** This public DrawingPanel() constructor.* */
public DrawingPanel(){

  setPreferredSize(new Dimension(400, 400));
  setBackground(Color.pink);

} // End of DrawingPanel() method.

/** This public void paintComponent(Graphics g) method* */
public void paintComponent(Graphics g){
  super.paintComponent(g);

  for(int index = 0; index<count; index++)
    shapeArray[index].display(g);       

} // End of paintComponent(Graphics g) method.

}// End of private class;

  /** This class allows actions to be added to the buttons and text fields;*/
  private class Listener implements ActionListener{

public void actionPerformed(ActionEvent event){
  int lengthVal, widthVal, yVal, xVal;

  JButton button = (JButton) event.getSource () ;

  String textX = x.getText();
  String textY = y.getText();
  String textLength = length.getText();
  String textWidth = width.getText();

  xVal = Integer.parseInt(textX);
  yVal = Integer.parseInt(textY);
  lengthVal = Integer.parseInt(textLength);
  widthVal = Integer.parseInt(textWidth);

  if(button.getText().equals("Add a shape")){
    if(count<shapeArray.length){
      shapeArray[count] = new MapShapes(lengthVal, widthVal, xVal, yVal);
      count++;
    } // end of nested if block;

   } // End of if block;

  repaint();

}// end of method;

}// end of private class;

}// end of class;

我的第二堂课。

/**  This class makes the rectangles for the Map program. ;
 * @author: James ;
*/

import java.util.*; // So that we can use the Random class.
import java.awt.*; // So that we can use the Color class and the Graphics class.

 /** This class makes rectangles.*/
  public class MapShapes{

 private int x;
 private int y;
 private int length;
 private int width;
 private Color colour;

public MapShapes(int length, int width, int x, int y){
this.length = length;
this.width = width;
this.x = x;
this.y = y;
this.colour = new Color(randInt(0,254), randInt(0,254), randInt(0,254));               
 }

  /** This randInt(int lowest, int highest) method:
* Returns a random value within particular limits to instantiate the colour data field.
* */
 public int randInt( int lowest, int highest){
Random generator = new Random(); 
int randomNum = generator.nextInt(highest - lowest) + lowest;
return randomNum;
}

public void display(Graphics j){
  j.setColor(colour);
  j.fillRect(x, y, length, width);
}
}// end of class;

1 个答案:

答案 0 :(得分:1)

您需要向ActionListener添加addButton,在触发后,会收集其他字段所需的信息并添加shapeArray并调用repaint在屏幕上显示的DrawingPanel的实例。

有关详细信息,请参阅How to Use Buttons, Check Boxes, and Radio Buttons和{{3}}