gpt4 book ai didi

java - 使用java绘制最短线和最长线用不同的颜色

转载 作者:行者123 更新时间:2023-12-01 10:14:33 26 4
gpt4 key购买 nike

我正在尝试绘制用不同颜色(绿色)绘制的最短线和用另一种颜色(白色)绘制的最长线。我怎样才能做到这一点?我应该使用 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类,它是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);
}

}

}

更新

您需要在绘制之前确定最短/最长的线条,一旦开始就为时已晚。

一种方法是在添加新点时计算最长和最短长度,例如......

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);
}
//...

关于java - 使用java绘制最短线和最长线用不同的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35976362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com