用Java小程序实现对已知起点和终点的直线的绘制,过程中需要注意哪些问题

用Java小程序实现对已知起点和终点的直线的绘制,过程中需要注意哪些问题,第1张

java程序绘制直线 也数学里一样, 也是需要两个点即可实现

直角坐标系和屏幕坐标系不一样: 为了能够正确的显示出来,那么就要确保直线在JPanel等容器的可见范围内,如果点的坐标有负数, 或者 超过了容器的最大尺寸, 那么就无法正确的全部显示出来

2.awt/swing里面直线的坐标,是int类型的整数, 不支持小数

数学里P(2.8,6.7)可以,在awt/swing里就不支持

3.  javaFX支持坐标用小数表示

这是我以前写的一个可以画多颜色多图形的东西,给你看看..

import java.awt.*

import java.awt.event.*

import java.util.ArrayList

import java.util.Map

import javax.swing.*

public class DrawBorder extends JFrame implements MouseListener,

MouseMotionListener {

// private JComboBox

private JLabellabel

private JLabellabelColor

private JLabellabelShape

private String[] colors = { "Black", "Blue", "Green", "Cyan", "Gray",

"Orange", "Red" }

private String[] shapes = { "Line", "Rectangle", "Oval", "TDRectangle" }

private Map mpColor

private JComboBox comboboxColor

private JComboBox comboboxShape

ArrayList lstShape

protected int x1

protected int y1

protected int x2

protected int y2

protected String color

private Stringshape

public void display() {

shape = "Line"

comboboxShape = new JComboBox(shapes)

comboboxShape.setMaximumRowCount(shapes.length)

comboboxShape.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent event) {

// determine whether check box selected

if (event.getStateChange() == ItemEvent.SELECTED) {

shape = shapes[comboboxShape.getSelectedIndex()]

}

}

})

color = "BLACK"

comboboxColor = new JComboBox(colors)

comboboxColor.setMaximumRowCount(colors.length)

comboboxColor.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent event) {

if (event.getStateChange() == ItemEvent.SELECTED) {

color = colors[comboboxColor.getSelectedIndex()]

}

}

})

label = new JLabel()

labelShape = new JLabel("图形:")

labelColor = new JLabel("颜色:")

label.setText(shape)

// getContentPane().add(label)

lstShape = new ArrayList()

Container container = getContentPane()

container.setLayout(new FlowLayout())

container.add(labelShape)

container.add(comboboxShape)

container.add(labelColor)

container.add(comboboxColor)

container.add(label)

getContentPane().setBackground(Color.WHITE)

addMouseListener(this)

addMouseMotionListener(this)

setSize(600, 600)

setVisible(true)

}

public void mouseClicked(MouseEvent e) {

label.setText("单击鼠标在: [" + e.getX() + ", " + e.getY() + "]")

}

public void mouseEntered(MouseEvent e) {

label.setText("鼠标在: [" + e.getX() + ", " + e.getY() + "]")

}

public void mouseExited(MouseEvent e) {

label.setText("鼠标在: [窗口外]")

}

public void mousePressed(MouseEvent e) {

label.setText("按住鼠标在: [" + e.getX() + ", " + e.getY() + "]")

x1 = e.getX()

y1 = e.getY()

}

public void mouseReleased(MouseEvent e) {

label.setText("松开鼠标在: [" + e.getX() + ", " + e.getY() + "]")

// 判断是否按住鼠标并拖动过,如果不则不产生图形

if (((x1 == 0 &&y1 == 0) || (x2 == 0 &&y2 == 0)) == false) {

if (this.shape.equals("Line")) {

lstShape.add(new Line(x1, y1, x2, y2, color))

}

else if (this.shape.equals("Rectangle")) {

if (x2 >x1 &&y2 >y1) {

lstShape.add(new Rectangle(x1, y1, x2 - x1, y2 - y1, color))

}

else if (x2 <x1 &&y2 >y1) {

lstShape.add(new Rectangle(x2, y1, x1 - x2, y2 - y1, color))

}

else if (x2 >x1 &&y2 <y1) {

lstShape.add(new Rectangle(x1, y2, x2 - x1, y1 - y2, color))

}

else if (x2 <x1 &&y2 <y1) {

lstShape.add(new Rectangle(x2, y2, x1 - x2, y1 - y2, color))

}

}

else if (this.shape.equals("Oval")) {

if (x2 >x1 &&y2 >y1) {

lstShape.add(new Oval(x1, y1, x2 - x1, y2 - y1, color))

}

else if (x2 <x1 &&y2 >y1) {

lstShape.add(new Oval(x2, y1, x1 - x2, y2 - y1, color))

}

else if (x2 <x1 &&y2 <y1) {

lstShape.add(new Oval(x2, y2, x1 - x2, y1 - y2, color))

}

else if (x2 >x1 &&y2 <y1) {

lstShape.add(new Oval(x1, y2, x2 - x1, y1 - y2, color))

}

}

else if (this.shape.equals("TDRectangle")) {

if (x2 >x1 &&y2 >y1) {

lstShape.add(new TDRectangle(x1, y1, x2 - x1, y2 - y1, color))

}

else if (x2 <x1 &&y2 >y1) {

lstShape.add(new TDRectangle(x2, y1, x1 - x2, y2 - y1, color))

}

else if (x2 >x1 &&y2 <y1) {

lstShape.add(new TDRectangle(x1, y2, x2 - x1, y1 - y2, color))

}

else if (x2 <x1 &&y2 <y1) {

lstShape.add(new TDRectangle(x2, y2, x1 - x2, y1 - y2, color))

}

}

}

// 初始化图形坐标

x1 = y1 = x2 = y2 = 0

repaint()

}

public void mouseDragged(MouseEvent e) {

label.setText("拖动鼠标在: [" + e.getX() + ", " + e.getY() + "]")

x2 = e.getX()

y2 = e.getY()

label.setText("图形:" + shape)

repaint()

}

public void mouseMoved(MouseEvent e) {

label.setText("移动动鼠标在: [" + e.getX() + ", " + e.getY() + "]")

}

public void paint(Graphics g) {

super.paint(g)

g.setColor(this.toColor())

if (this.shape.equals("Line")) {

g.drawLine(x1, y1, x2, y2)

}

else if (this.shape.equals("Rectangle")) {

if (x2 >x1 &&y2 >y1) {

g.drawRect(x1, y1, x2 - x1, y2 - y1)

}

else if (x2 <x1 &&y2 >y1) {

g.drawRect(x2, y1, x1 - x2, y2 - y1)

}

else if (x2 <x1 &&y2 <y1) {

g.drawRect(x2, y2, x1 - x2, y1 - y2)

}

else if (x2 >x1 &&y2 <y1) {

g.drawRect(x1, y2, x2 - x1, y1 - y2)

}

}

else if (this.shape.equals("Oval")) {

if (x2 >x1 &&y2 >y1) {

g.drawOval(x1, y1, x2 - x1, y2 - y1)

}

else if (x2 <x1 &&y2 >y1) {

g.drawOval(x2, y1, x1 - x2, y2 - y1)

}

else if (x2 <x1 &&y2 <y1) {

g.drawOval(x2, y2, x1 - x2, y1 - y2)

}

else if (x2 >x1 &&y2 <y1) {

g.drawOval(x1, y2, x2 - x1, y1 - y2)

}

}

else if (this.shape.equals("TDRectangle")) {

if (x2 >x1 &&y2 >y1) {

g.draw3DRect(x1, y1, x2 - x1, y2 - y1, true)

}

else if (x2 <x1 &&y2 >y1) {

g.draw3DRect(x2, y1, x1 - x2, y2 - y1, true)

}

else if (x2 <x1 &&y2 <y1) {

g.draw3DRect(x2, y2, x1 - x2, y1 - y2, true)

}

else if (x2 >x1 &&y2 <y1) {

g.draw3DRect(x1, y2, x2 - x1, y1 - y2, true)

}

}

// 画出存放在list中的图形

for (Object object : lstShape) {

if (object.getClass().toString().equals("class DrawBorder$Line")) {

Line line = (Line) object

g.setColor(line.toColor())

g.drawLine(line.x1, line.y1, line.x2, line.y2)

}

else if (object.getClass().toString()

.equals("class DrawBorder$Rectangle")) {

Rectangle rectangle = (Rectangle) object

g.setColor(rectangle.toColor())

g.drawRect(rectangle.x1, rectangle.y1, rectangle.x2, rectangle.y2)

}

else if (object.getClass().toString().equals("class DrawBorder$Oval")) {

Oval oval = (Oval) object

g.setColor(oval.toColor())

g.drawOval(oval.x1, oval.y1, oval.x2, oval.y2)

}

else if (object.getClass().toString().equals(

"class DrawBorder$TDRectangle")) {

TDRectangle tDRectangle = (TDRectangle) object

g.setColor(tDRectangle.toColor())

g.draw3DRect(tDRectangle.x1, tDRectangle.y1, tDRectangle.x2,

tDRectangle.y2, false)

}

}

}

public static void main(String[] args) {

DrawBorder db = new DrawBorder()

db.display()

db.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

}

public Color toColor() {

if (this.color.equals("Blue")) {

return Color.BLUE

}

else if (this.color.equals("Green")) {

return Color.GREEN

}

else if (this.color.equals("Cyan")) {

return Color.CYAN

}

else if (this.color.equals("Gray")) {

return Color.GRAY

}

else if (this.color.equals("Orange")) {

return Color.ORANGE

}

else if (this.color.equals("Red")) {

return Color.RED

}

else

return Color.BLACK

}

class Line extends DrawBorder {

Line(int x1, int y1, int x2, int y2, String color) {

this.x1 = x1

this.y1 = y1

this.x2 = x2

this.y2 = y2

this.color = color

}

}

class Rectangle extends DrawBorder {

Rectangle(int x1, int y1, int x2, int y2, String color) {

this.x1 = x1

this.y1 = y1

this.x2 = x2

this.y2 = y2

this.color = color

}

}

class Oval extends DrawBorder {

Oval(int x1, int y1, int x2, int y2, String color) {

this.x1 = x1

this.y1 = y1

this.x2 = x2

this.y2 = y2

this.color = color

}

}

class TDRectangle extends DrawBorder {

TDRectangle(int x1, int y1, int x2, int y2, String color) {

this.x1 = x1

this.y1 = y1

this.x2 = x2

this.y2 = y2

this.color = color

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12201129.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存