使用java绘制不同颜色的最短线和最长线
using java to draw the shortest line and the longest line with different colors
我正在尝试绘制我用不同颜色(绿色)绘制的最短线和我用另一种颜色(白色)绘制的最长线。我怎样才能做到这一点?我应该使用 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));
}
}
有几种方法可以做到这一点,例如,创建一个简单的 Line
class 即 Comparable
并且可以计算自己的长度。这些存储在已排序的 List
中,因此最短的在前,最长的在最后,这使得从 List
中挑选它们并绘制它们变得简单
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private List<Line> lines;
public TestPane() {
lines = new ArrayList<Line>(25);
MouseAdapter ma = new MouseAdapter() {
private Line currentLine;
@Override
public void mousePressed(MouseEvent e) {
currentLine = new Line(e.getPoint());
Collections.sort(lines);
lines.add(currentLine);
}
@Override
public void mouseReleased(MouseEvent e) {
currentLine = null;
repaint();
}
@Override
public void mouseDragged(MouseEvent e) {
currentLine.to(e.getPoint());
Collections.sort(lines);
repaint();
}
};
addMouseListener(ma);
addMouseMotionListener(ma);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
if (!lines.isEmpty()) {
Line shortest = lines.get(0);
Line longest = lines.get(lines.size() - 1);
if (lines.size() > 2) {
lines.subList(1, lines.size() - 2);
for (Line line : lines) {
g2d.draw(line.getShape());
}
}
g2d.setColor(Color.RED);
g2d.draw(shortest.getShape());
if (!longest.equals(shortest)) {
g2d.setColor(Color.GREEN);
g2d.draw(longest.getShape());
}
}
g2d.dispose();
}
}
public class Line implements Comparable<Line> {
private Point p1;
private Point p2;
public Line(Point p1) {
this.p1 = p1;
this.p2 = p1;
}
public Line to(Point p) {
p2 = p;
return this;
}
public double getLength() {
return Math.sqrt(((p2.x - p1.x) * (p2.x - p1.x)) + ((p2.y - p1.y) * (p2.y - p1.y)));
}
@Override
public int compareTo(Line o) {
return (int) (getLength() - o.getLength());
}
public Line2D getShape() {
return new Line2D.Double(p1, p2);
}
}
}
更新
您需要在绘制之前确定 shortest/longest 行,一旦开始就为时已晚。
一种方法是在添加新点时计算最长和最短长度,例如...
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()));
if (pointList.size() > 1) {
// Here, you now need to determine if the distance between the this and
// the previous point is the shortest or longest
longest = Math.max(longest, getDistance(pointList.get(pointList.size() - 1), pointList.get(pointList.size() - 2)));
shortest = Math.min(shortest, getDistance(pointList.get(pointList.size() - 1), pointList.get(pointList.size() - 2)));
}
}
repaint();
}
然后当你绘制线条时,你只需计算每个点之间的距离并确定它们是否与最长或最短的线条长度相同...
@Override
protected 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++) {
Point from = pointList.get(i);
Point to = pointList.get(i + 1);
int distance = getDistance(from, to);
if (distance == longest) {
g.setColor(Color.WHITE);
} else if (distance == shortest) {
g.setColor(Color.GREEN);
} else {
g.setColor(Color.BLACK);
}
//...
我正在尝试绘制我用不同颜色(绿色)绘制的最短线和我用另一种颜色(白色)绘制的最长线。我怎样才能做到这一点?我应该使用 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));
}
}
有几种方法可以做到这一点,例如,创建一个简单的 Line
class 即 Comparable
并且可以计算自己的长度。这些存储在已排序的 List
中,因此最短的在前,最长的在最后,这使得从 List
中挑选它们并绘制它们变得简单
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Line2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private List<Line> lines;
public TestPane() {
lines = new ArrayList<Line>(25);
MouseAdapter ma = new MouseAdapter() {
private Line currentLine;
@Override
public void mousePressed(MouseEvent e) {
currentLine = new Line(e.getPoint());
Collections.sort(lines);
lines.add(currentLine);
}
@Override
public void mouseReleased(MouseEvent e) {
currentLine = null;
repaint();
}
@Override
public void mouseDragged(MouseEvent e) {
currentLine.to(e.getPoint());
Collections.sort(lines);
repaint();
}
};
addMouseListener(ma);
addMouseMotionListener(ma);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
if (!lines.isEmpty()) {
Line shortest = lines.get(0);
Line longest = lines.get(lines.size() - 1);
if (lines.size() > 2) {
lines.subList(1, lines.size() - 2);
for (Line line : lines) {
g2d.draw(line.getShape());
}
}
g2d.setColor(Color.RED);
g2d.draw(shortest.getShape());
if (!longest.equals(shortest)) {
g2d.setColor(Color.GREEN);
g2d.draw(longest.getShape());
}
}
g2d.dispose();
}
}
public class Line implements Comparable<Line> {
private Point p1;
private Point p2;
public Line(Point p1) {
this.p1 = p1;
this.p2 = p1;
}
public Line to(Point p) {
p2 = p;
return this;
}
public double getLength() {
return Math.sqrt(((p2.x - p1.x) * (p2.x - p1.x)) + ((p2.y - p1.y) * (p2.y - p1.y)));
}
@Override
public int compareTo(Line o) {
return (int) (getLength() - o.getLength());
}
public Line2D getShape() {
return new Line2D.Double(p1, p2);
}
}
}
更新
您需要在绘制之前确定 shortest/longest 行,一旦开始就为时已晚。
一种方法是在添加新点时计算最长和最短长度,例如...
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()));
if (pointList.size() > 1) {
// Here, you now need to determine if the distance between the this and
// the previous point is the shortest or longest
longest = Math.max(longest, getDistance(pointList.get(pointList.size() - 1), pointList.get(pointList.size() - 2)));
shortest = Math.min(shortest, getDistance(pointList.get(pointList.size() - 1), pointList.get(pointList.size() - 2)));
}
}
repaint();
}
然后当你绘制线条时,你只需计算每个点之间的距离并确定它们是否与最长或最短的线条长度相同...
@Override
protected 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++) {
Point from = pointList.get(i);
Point to = pointList.get(i + 1);
int distance = getDistance(from, to);
if (distance == longest) {
g.setColor(Color.WHITE);
} else if (distance == shortest) {
g.setColor(Color.GREEN);
} else {
g.setColor(Color.BLACK);
}
//...