画线功能java代码 java画线段

java简单的画线程序

import java.awt.*;

网站建设哪家好,找创新互联建站!专注于网页设计、网站建设、微信开发、微信小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了莆田免费建站欢迎大家使用!

import java.awt.event.*;

import java.util.ArrayList;public class DrawLine extends Frame

implements WindowListener

{

static ArrayList lbArray; DrawLine(){

setBounds(150,150,300,300);

setVisible(true);

Label lb0=new Label("Hello");

addWindowListener(this);

add(lb0);

lb0.setBounds(50,30,40,20);

Label lb1=new Label("World!");

add(lb1);

lb1.setBounds(50,90,40,20); Label lb2=new Label("Java");

add(lb2);

lb2.setBounds(50,150,40,20);

lbArray.add(lb0);

lbArray.add(lb1);

lbArray.add(lb2);

repaint(); }

public void windowClosed(WindowEvent w)

{

System.exit(0);

}

public void windowClosing(WindowEvent w)

{

dispose();

}

public void windowOpened(WindowEvent e) {}

public void windowIconified(WindowEvent e) {}

public void windowDeiconified(WindowEvent e) {}

public void windowActivated(WindowEvent e) {}

public void windowDeactivated(WindowEvent e) {}

public void paint(Graphics g)

{

super.paint(g); if(lbArray.size()=1)

{

g.setColor(Color.red);

int x1,y1,x2,y2;

Label lb=(Label)lbArray.get(0);

Rectangle rc=lb.getBounds();

x1=rc.x+rc.width/2;

y1=rc.y+rc.height/2;

for(int i=1;ilbArray.size();i++)

{

lb=(Label)lbArray.get(i);

rc=lb.getBounds();

x2=rc.x+rc.width/2;

y2=rc.y+rc.height/2;

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

x1=x2;

y1=y2;

}

}

} public static void main(String[] args){

lbArray=new ArrayList(10);

new DrawLine();

}

} 终于做了个满足你要求的东西。要知道我昨天和你说过我并不常用Java。赶快采纳吧。

java 最简单的画线演示程序

对于你拖动的时候会描出很多线的问题,可以在public void paint(Graphics g)函数中,用super.paint(g);语句解决,就象下面我的程序一样。

最简单的画线演示程序如下:

import java.awt.Graphics;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;

import javax.swing.JPanel;

public class DrawLine extends JFrame implements MouseListener,MouseMotionListener{

private int x0=0,y0=0,x=0,y=0;

JPanel jp=new JPanel();

DrawLine(){

jp.addMouseListener(this);

jp.addMouseMotionListener(this);

add(jp);

setSize(300, 300);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

}

public void paint(Graphics g){

super.paint(g);

g.drawLine(x0, y0, x, y);

}

public static void main(String[] args) {

new DrawLine();

}

public void mouseDragged(MouseEvent arg0) {

x=arg0.getXOnScreen();

y=arg0.getYOnScreen();

repaint();

}

public void mouseMoved(MouseEvent arg0) {}

public void mouseClicked(MouseEvent arg0) {}

public void mouseEntered(MouseEvent arg0) {}

public void mouseExited(MouseEvent arg0) {}

public void mousePressed(MouseEvent arg0) {

x0=arg0.getXOnScreen();

y0=arg0.getYOnScreen();

}

public void mouseReleased(MouseEvent arg0) {

x=arg0.getXOnScreen();

y=arg0.getYOnScreen();

repaint();

}

}

java简单画线程序

这个覆盖 paint 方法就可以了。

我帮你写好了,我采用的方法是:把内容画在一个 JPanel 里,然后用 setContentPane 添加到了 JFrame 上,这样做是因为定位问题:JFrame 有边框,绘制组件的时候坐标是算边框占位的,但是 drawLine 的时候是不算的,为了统一算边框,就用了这个办法。

下面是两个文件,第一个 MyFrame 是一个完整的工具,MyFrame 因为继承了 JFrame,用起来和 JFrame 一样用,另外提供了一个接口,addComponent(JComponent) 方法可以向里添加组件,会自动连线的。你只要 import ui 就可以用了。

然后第二个是测试类,可以看到我在 setVisible 之后添加的组件,它也能正确显示,证明 addComponent 方法可以随时调用,不用另外刷新。

---------------------------- MyFrame.java -----------------------------

package ui;

import java.awt.*;

import javax.swing.*;

import java.util.*;

class MyPanel extends JPanel {

private VectorJComponent comps;

public MyPanel () {

comps = new VectorJComponent();

setLayout(null);

}

public void paint (Graphics g) {

g.setColor(Color.BLACK);

for (int i=1; icomps.size(); i++) {

g.drawLine(comps.get(0).getLocation().x+comps.get(0).getSize().width/2,

comps.get(0).getLocation().y+comps.get(0).getSize().height/2,

comps.get(i).getLocation().x+comps.get(i).getSize().width/2,

comps.get(i).getLocation().y+comps.get(i).getSize().height/2);

}

super.paintComponents(g);

}

public void addComponent (JComponent comp) {

comp.setOpaque(true);

comps.add(comp);

add(comp);

repaint();

}

}

public class MyFrame extends JFrame {

private MyPanel mp;

public MyFrame () {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

mp = new MyPanel();

setContentPane(mp);

}

public void addComponent (JComponent comp) {

mp.addComponent(comp);

}

}

---------------------------- TestMyFrame.java -----------------------------

import ui.*;

import javax.swing.*;

import javax.swing.border.*;

public class TestMyFrame {

public static void main (String args[]) {

MyFrame mf = new MyFrame();

mf.setSize(500, 500);

mf.setLocationRelativeTo(null);

mf.setVisible(true);

testAddComponent(mf);

}

public static void testAddComponent (MyFrame mf) {

JLabel l1 = new JLabel("l1");

l1.setBorder(new EtchedBorder());

l1.setBounds(200, 100, 20, 20);

mf.addComponent(l1);

JLabel l2 = new JLabel("l2");

l2.setBounds(100, 150, 20, 20);

l2.setBorder(new EtchedBorder());

mf.addComponent(l2);

JLabel l3 = new JLabel("l3");

l3.setBounds(150, 150, 20, 20);

l3.setBorder(new EtchedBorder());

mf.addComponent(l3);

JLabel l4 = new JLabel("l4");

l4.setBounds(200, 150, 20, 20);

l4.setBorder(new EtchedBorder());

mf.addComponent(l4);

}

}


网页标题:画线功能java代码 java画线段
本文地址:http://scyanting.com/article/hjggec.html