【发布时间】:2016-06-28 19:57:34
【问题描述】:
我正在尝试用另一种颜色(绿色)画出最短的线,用另一种颜色(白色)画出最长的线。我怎样才能做到这一点?我应该使用 if else 语句吗?
即使我这样做,我也可以使它最接近 0 以打印我猜的不同颜色,但是当该行实际上为零时,这没有任何意义。
import java.util.ArrayList;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
public class DotsPanel extends JPanel
{
private final int SIZE = 6; // radius of each dot
private int selectedCircle = -1;
private int length = 0;
private int i = 0;
private int totalDistance = 0;
private ArrayList<PointCount> pointList;
public DotsPanel()
{
pointList = new ArrayList<PointCount>();
addMouseListener (new DotsListener());
addMouseMotionListener (new DotsListener());
setBackground(Color.black);
setPreferredSize(new Dimension(300, 300));
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int min = Integer.MAX_VALUE;
int minIndex = -1;
int max = -Integer.MAX_VALUE;
int maxIndex = -1;
for(i=0; i < pointList.size()-1;i++){
length = (int)Math.sqrt(Math.pow(pointList.get(i).x - pointList.get(i+1).x, 2)+
Math.pow(pointList.get(i).y - pointList.get(i+1).y, 2));
g.setColor(Color.cyan);
g.drawString("" + length,(pointList.get(i).x+pointList.get(i+1).x)/2, (pointList.get(i).y+pointList.get(i+1).y)/2);
}
totalDistance += length;
for(i=0; i < pointList.size()-1;i++){
g.setColor(Color.white);
g.drawLine(pointList.get(i).x,
pointList.get(i).y,
pointList.get(i+1).x,
pointList.get(i+1).y);
g.setColor(Color.blue);
g.fillOval(pointList.get(i+1).x-SIZE,
pointList.get(i+1).y-SIZE,
SIZE*2,
SIZE*2);
// --- Displays counts
g.setColor(Color.yellow);
g.drawString("" + pointList.get(i+1).getCount(),
pointList.get(i+1).x,
pointList.get(i+1).y);
}
for(i=0; i < pointList.size()-2; i++) {
int distance = (int) Math.sqrt(Math.pow(pointList.get(i).x - pointList.get(i+1).x, 2)+
Math.pow(pointList.get(i).y - pointList.get(i+1).y, 2));
if(distance > max) {
max = distance;
maxIndex = i;
g.setColor(Color.red);
g.drawLine(pointList.get(i).x,
pointList.get(i).y,
pointList.get(i+1).x,
pointList.get(i+1).y);
}
if(distance < min) {
min = distance;
minIndex = i;
g.setColor(Color.green);
g.drawLine(pointList.get(i).x,
pointList.get(i).y,
pointList.get(i+1).x,
pointList.get(i+1).y);
}
}
g.setColor(Color.white);
g.drawString("Count: " + pointList.size(), 5, 15);
g.drawString("Total Distance: " + totalDistance, 5, 30);
}
private class DotsListener implements MouseListener,MouseMotionListener
{
public void mouseClicked(MouseEvent event)
{
boolean spotSelected = false;
int count = 0;
while(count < pointList.size() && !spotSelected) {
Point current = pointList.get(count).getPoint();
if(getDistance(event.getPoint(), current) <= SIZE) {
// --- Spot/circle selected or clicked on again
pointList.get(count).addCount();
spotSelected = false;
}
count++;
}
// --- Checks whether a new object needs to be created
if(!spotSelected) { // spotSelected == false
pointList.add(new PointCount(event.getPoint()));
}
repaint();
}
//--------------------------------------------------------------
// Provide empty definitions for unused event methods.
//--------------------------------------------------------------
public void mousePressed(MouseEvent e){}
public void mouseMoved(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
private int getDistance(Point p1, Point p2) {
return (int)Math.sqrt(Math.pow(p1.x - p2.x, 2)+
Math.pow(p1.y - p2.y, 2));
}
}
【问题讨论】:
-
我想您需要了解更多信息,例如,您需要维护
List或数组中的每个点或生成维护该信息的Line类,然后可以计算线的长度。然后,您可以对列表进行排序,这样可以更轻松地确定哪个是最短和最长的,但您也可以计算何时调用mouseReleased -
是的,那么在我得到长度后我应该做什么
-
这将取决于您维护信息的“方式”以及是否要显示您绘制的所有其他线条
-
Ok g.drawline(my coordinates) 并让它们一直出现。之后
-
用点坐标pt1 pt2制作int线
标签: java swing arraylist jpanel