关于一个关于JAVA绘制线段的问题.
问题是这样:我现在需要从一个TXT文件中导入线段起点、终点坐标还有线段颜色一共3组数据,并且要求在屏幕显示出这几条线段.图中是一个数据文本的例子:分别表示了起点(X,Y)...
问题是这样:我现在需要从一个TXT文件中导入线段起点、终点坐标还有线段颜色一共3组数据,并且要求在屏幕显示出这几条线段.图中是一个数据文本的例子:分别表示了起点(X,Y)坐标,终点(X,Y)坐标和随机颜色值.分数不低,希望有人回答.
展开
展开全部
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JFrame;
public class LineTest extends JFrame {
private static final long serialVersionUID = 1L;
private ArrayList<Line> lines = null;
private Scanner san = null;
public LineTest(String path) {
this.lines = this.bulidLines(path);
}
@Override
public void paint(Graphics g) {
super.paint(g);
for (Line l : lines) {
g.setColor(l.color);
g.drawLine(l.start.x, l.start.y, l.end.x, l.end.y);
}
}
public static void main(String[] args) {
LineTest lt = new LineTest("data.txt");
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width / 2;
int screenHeight = screenSize.height / 2;
int height = 600;
int width = 600;
lt.setTitle("乱画线段。。。");
lt.setLocation(screenWidth - width / 2, screenHeight - height / 2);
lt.setSize(width, height);
lt.setResizable(false);
lt.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
lt.setVisible(true);
}
protected ArrayList<Line> bulidLines(String path) {
ArrayList<Line> lines = new ArrayList<Line>();
try {
san = new Scanner(new File(path));
while (san.hasNextLine()) {
String l = san.nextLine();
int[] vals = this.pareInts(l.split("(\\s)+"));
lines.add(new Line(vals));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return lines;
}
private int[] pareInts(String[] sval) {
int[] vals = new int[sval.length];
for (int i = 0; i < vals.length; i++) {
vals[i] = Integer.parseInt(sval[i]);
}
return vals;
}
private class Line {
Point start;
Point end;
Color color;
Line(int[] vals) {
this.start = new Point(vals[0], vals[1]);
this.end = new Point(vals[2], vals[3]);
this.color = new Color(vals[4], vals[5], vals[6]);
}
}
}
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JFrame;
public class LineTest extends JFrame {
private static final long serialVersionUID = 1L;
private ArrayList<Line> lines = null;
private Scanner san = null;
public LineTest(String path) {
this.lines = this.bulidLines(path);
}
@Override
public void paint(Graphics g) {
super.paint(g);
for (Line l : lines) {
g.setColor(l.color);
g.drawLine(l.start.x, l.start.y, l.end.x, l.end.y);
}
}
public static void main(String[] args) {
LineTest lt = new LineTest("data.txt");
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width / 2;
int screenHeight = screenSize.height / 2;
int height = 600;
int width = 600;
lt.setTitle("乱画线段。。。");
lt.setLocation(screenWidth - width / 2, screenHeight - height / 2);
lt.setSize(width, height);
lt.setResizable(false);
lt.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
lt.setVisible(true);
}
protected ArrayList<Line> bulidLines(String path) {
ArrayList<Line> lines = new ArrayList<Line>();
try {
san = new Scanner(new File(path));
while (san.hasNextLine()) {
String l = san.nextLine();
int[] vals = this.pareInts(l.split("(\\s)+"));
lines.add(new Line(vals));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return lines;
}
private int[] pareInts(String[] sval) {
int[] vals = new int[sval.length];
for (int i = 0; i < vals.length; i++) {
vals[i] = Integer.parseInt(sval[i]);
}
return vals;
}
private class Line {
Point start;
Point end;
Color color;
Line(int[] vals) {
this.start = new Point(vals[0], vals[1]);
this.end = new Point(vals[2], vals[3]);
this.color = new Color(vals[4], vals[5], vals[6]);
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个你之前不是问过了,都回答了。
http://zhidao.baidu.com/question/495435656.html?oldq=1
http://zhidao.baidu.com/question/495435656.html?oldq=1
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个昨天就算了,被百度吞了,好在楼主又发了一个。
给你代码,开始没注意,画出来的是个正方形。
------------------------------------------------------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class LineAppFrame extends JFrame {
public LineAppFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(550, 550);
setLocationRelativeTo(null);
JPanel panel = new LinePanel();
getContentPane().add(panel, BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args) {
new LineAppFrame();
}
}
class LinePanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
FileReader fr = null;
BufferedReader br = null;
try {
File file = new File("data.txt");
fr = new FileReader(file);
br = new BufferedReader(fr);
Color defaultColor = g.getColor();
while (br.ready()) {
String line = br.readLine();
int[] data = conver(line);
g.setColor(new Color(data[4], data[5], data[6]));
g.drawLine(data[0], data[1], data[2], data[3]);
}
g.setColor(defaultColor);
} catch (Exception e) {
} finally {
try {
br.close();
fr.close();
} catch (Exception e) {
}
}
}
private int[] conver(String line) {
int[] result = new int[7];
int index = 0;
Scanner sc = new Scanner(line);
while (sc.hasNextInt()) {
result[index++] = sc.nextInt();
}
return result;
}
}
给你代码,开始没注意,画出来的是个正方形。
------------------------------------------------------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class LineAppFrame extends JFrame {
public LineAppFrame() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(550, 550);
setLocationRelativeTo(null);
JPanel panel = new LinePanel();
getContentPane().add(panel, BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args) {
new LineAppFrame();
}
}
class LinePanel extends JPanel {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
FileReader fr = null;
BufferedReader br = null;
try {
File file = new File("data.txt");
fr = new FileReader(file);
br = new BufferedReader(fr);
Color defaultColor = g.getColor();
while (br.ready()) {
String line = br.readLine();
int[] data = conver(line);
g.setColor(new Color(data[4], data[5], data[6]));
g.drawLine(data[0], data[1], data[2], data[3]);
}
g.setColor(defaultColor);
} catch (Exception e) {
} finally {
try {
br.close();
fr.close();
} catch (Exception e) {
}
}
}
private int[] conver(String line) {
int[] result = new int[7];
int index = 0;
Scanner sc = new Scanner(line);
while (sc.hasNextInt()) {
result[index++] = sc.nextInt();
}
return result;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询