EDIT: to clarify, one of my requirements is to use a single array.
编辑:澄清一下,我的一个要求是使用单个阵列。
I am having trouble storing multiple variables to a single element in an array. We are creating a really simple program to mimic Microsoft Paint. One of the requirements is to store each element I draw in to an array so that the 'paint' method repaints the drawings each time the windows is minimized and then redisplayed. Here are the requirements
我在将多个变量存储到数组中的单个元素时遇到问题。我们正在创建一个非常简单的程序来模仿Microsoft Paint。其中一个要求是将我绘制的每个元素存储到一个数组中,以便每次窗口最小化然后重新显示时,“paint”方法都会重新绘制图形。这是要求
We are to assume a max size for the array is 20. Each element should include 5 variables:
我们假设数组的最大大小为20.每个元素应包含5个变量:
- char shape (l for line, r for rectangle, c for circle)
- Start x value
- Start y value
- width (rectangle), or ending x (line), or radius (circle)
- height (rectangle), or ending y (line), or radius (circle)
char形状(l代表直线,r代表矩形,c代表圆形)
开始x值
开始y值
宽度(矩形),或结束x(直线)或半径(圆)
高度(矩形),或结束y(线)或半径(圆)
Here is my code for the array class:
这是我的数组类的代码:
class storeDraws {
final int MAXSIZE = 20;
static int S[];
static int n; //number of draws user makes
static char shape;
static double px, py, w, h;
storeDraws () {
S = new int [MAXSIZE];
n = 0;
shape = 'l';
px = 0;
py = 0;
w = 0;
h = 0;
}
}
I have read a few places that I can input the array as (using the mouseReleased(MouseEvent e) method:
我已经阅读了一些我可以输入数组的地方(使用mouseReleased(MouseEvent e)方法:
storeDraws[] input = new storeDraws{value, value, value, value, value};
But I don't think that would work for what I am trying to do with the 'paint' method to redraw the shapes. I thought I could somehow pass it using the standard format of S[n] = (char, double, double, double, double), but I get warning that this is illegal.
但我认为这不会对我试图用'paint'方法重绘形状的方法起作用。我以为我可以使用S [n] =(char,double,double,double,double)的标准格式以某种方式传递它,但我得到警告,这是非法的。
Edit 8:30 am I got this part working. In my class here is my code now.
编辑上午8:30我得到了这部分工作。在我的课堂上,这里是我的代码。
class storeDraws {
static char shape;
static int px, py, w, h;
storeDraws () {
shape = 'l';
px = 0;
py = 0;
w = 0;
h = 0;
}
}
I then declared this in the DrawPanel class:
然后我在DrawPanel类中声明了这个:
private storeDraws[] store = new storeDraws[20];
private int n = 0;
And mouseReleased method of DrawPanel:
和MouseRenel的DrawPanel方法:
public void mouseReleased(MouseEvent e) {
if (drawShape == "line") {
store[n].shape = 'l';
store[n].px = p1.x;
store[n].py = p1.y;
store[n].w = p3.x;
store[n].h = p3.y;
n++;
}
And paint:
public void paint(Graphics g) {
for (int i = 0; i < n; i++) {
if (store[i].shape == 'l')
g.drawLine(store[n].px, store[n].py, store[n].w, store[n].h);
But if I draw 6 lines it only repaints the last line.
但如果我绘制6行,它只会重新绘制最后一行。
5 个解决方案
#1
I think you need to separate some of the functionality you want. You can have a class for each of the elements and then store instances of the class in an array of DrawingElement objects.
我认为您需要分离一些您想要的功能。您可以为每个元素创建一个类,然后将该类的实例存储在DrawingElement对象的数组中。
So you'd do something like this:
所以你会做这样的事情:
DrawingElement[] drawing = new DrawingElement[20];
DrawingElement circle = new DrawingElement('c', 10, 10, 10, 10);
DrawingElement rect = new DrawingElement('r', 20, 10, 10, 10);
drawing[0] = circle;
drawing[1] = rect;
Note: If you need to be able to get the number of objects in the array (variable n
in your code) you may want to use some implementation of a Linked List (which has a size()
method) and do some check when adding elements to make sure you don't add past the max of 20.
注意:如果您需要能够获取数组中的对象数(代码中的变量n),您可能需要使用链接列表的某些实现(具有size()方法)并在添加时进行一些检查元素,以确保您不添加超过20的最大值。
Example with LinkedList:
LinkedList的示例:
LinkedList<DrawingElement> drawing = new LinkedList<DrawingElement>();
DrawingElement circle = new DrawingElement('c', 10, 10, 10, 10);
DrawingElement rect = new DrawingElement('r', 20, 10, 10, 10);
drawing.add(circle);
drawing.add(rect);
int n = drawing.size(); //will be 2
The Drawing Element Class:
绘图元素类:
public class DrawingElement
{
char shape;
double px, py, w, h;
public DrawingElement(char shape, double px, double py, double w, double h)
{
this.shape = shape;
this.px = px;
this.py = py;
this.w = w;
this.h = h;
}
//Add getters and setters (accessors and mutators) for class variables
}
#2
I think you should use collection for this purpose.Create array, store values then add each array object in collection(list).
我认为你应该为此目的使用集合。创建数组,存储值然后在集合(列表)中添加每个数组对象。
#3
you can use arraylist instead of array. this will help you to store different variable. If there is need of using only array you can use object array Sample Code object a[]= {'a',10,10.2,10.3,10.4,10.5}
你可以使用arraylist而不是array。这将帮助您存储不同的变量。如果只需要使用数组,可以使用对象数组示例代码对象a [] = {'a',10,10.2,10.3,10.4,10.5}
#4
Please refer ArrayList
请参考ArrayList
In your case it would be better to use Java Collection(ArrayList) rather than Array.
在您的情况下,最好使用Java Collection(ArrayList)而不是Array。
#5
First point, We cannot store different types of variables values into an Array.
第一点,我们不能将不同类型的变量值存储到Array中。
An array is a container object that holds a fixed number of values of a single type. Here you are trying to store multiple type of variables into integer array.
数组是一个容器对象,它包含固定数量的单个类型的值。在这里,您尝试将多种类型的变量存储到整数数组中。
see this link:
看到这个链接:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
To overcome this drawback, We have Collection API introduced with JDK1.5
为了克服这个缺点,我们在JDK1.5中引入了Collection API
Refer this link:
请参考此链接:
http://docs.oracle.com/javase/tutorial/collections/index.html
#1
I think you need to separate some of the functionality you want. You can have a class for each of the elements and then store instances of the class in an array of DrawingElement objects.
我认为您需要分离一些您想要的功能。您可以为每个元素创建一个类,然后将该类的实例存储在DrawingElement对象的数组中。
So you'd do something like this:
所以你会做这样的事情:
DrawingElement[] drawing = new DrawingElement[20];
DrawingElement circle = new DrawingElement('c', 10, 10, 10, 10);
DrawingElement rect = new DrawingElement('r', 20, 10, 10, 10);
drawing[0] = circle;
drawing[1] = rect;
Note: If you need to be able to get the number of objects in the array (variable n
in your code) you may want to use some implementation of a Linked List (which has a size()
method) and do some check when adding elements to make sure you don't add past the max of 20.
注意:如果您需要能够获取数组中的对象数(代码中的变量n),您可能需要使用链接列表的某些实现(具有size()方法)并在添加时进行一些检查元素,以确保您不添加超过20的最大值。
Example with LinkedList:
LinkedList的示例:
LinkedList<DrawingElement> drawing = new LinkedList<DrawingElement>();
DrawingElement circle = new DrawingElement('c', 10, 10, 10, 10);
DrawingElement rect = new DrawingElement('r', 20, 10, 10, 10);
drawing.add(circle);
drawing.add(rect);
int n = drawing.size(); //will be 2
The Drawing Element Class:
绘图元素类:
public class DrawingElement
{
char shape;
double px, py, w, h;
public DrawingElement(char shape, double px, double py, double w, double h)
{
this.shape = shape;
this.px = px;
this.py = py;
this.w = w;
this.h = h;
}
//Add getters and setters (accessors and mutators) for class variables
}
#2
I think you should use collection for this purpose.Create array, store values then add each array object in collection(list).
我认为你应该为此目的使用集合。创建数组,存储值然后在集合(列表)中添加每个数组对象。
#3
you can use arraylist instead of array. this will help you to store different variable. If there is need of using only array you can use object array Sample Code object a[]= {'a',10,10.2,10.3,10.4,10.5}
你可以使用arraylist而不是array。这将帮助您存储不同的变量。如果只需要使用数组,可以使用对象数组示例代码对象a [] = {'a',10,10.2,10.3,10.4,10.5}
#4
Please refer ArrayList
请参考ArrayList
In your case it would be better to use Java Collection(ArrayList) rather than Array.
在您的情况下,最好使用Java Collection(ArrayList)而不是Array。
#5
First point, We cannot store different types of variables values into an Array.
第一点,我们不能将不同类型的变量值存储到Array中。
An array is a container object that holds a fixed number of values of a single type. Here you are trying to store multiple type of variables into integer array.
数组是一个容器对象,它包含固定数量的单个类型的值。在这里,您尝试将多种类型的变量存储到整数数组中。
see this link:
看到这个链接:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
To overcome this drawback, We have Collection API introduced with JDK1.5
为了克服这个缺点,我们在JDK1.5中引入了Collection API
Refer this link:
请参考此链接:
http://docs.oracle.com/javase/tutorial/collections/index.html