「画线的java」画线的英语怎么说

博主:adminadmin 2022-12-02 02:12:07 75

今天给各位分享画线的java的知识,其中也会对画线的英语怎么说进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java怎么画出一条直线呢?高手进吧。

我晕,组件要重绘,就使用paintComponent方法吧,没有什么灵活不灵活的

import java.awt.Color;

import java.awt.EventQueue;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.GridLayout;

import javax.swing.BorderFactory;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

public class FrameDemo extends JFrame {

private JPanel contentPane;

TestComponent tc1, tc2;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

FrameDemo frame = new FrameDemo();

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

public FrameDemo() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 450, 300);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(new GridLayout(2, 1, 10, 10));

tc1 = new TestComponent();

tc1.setBorder(BorderFactory.createLineBorder(Color.BLUE));

tc2 = new TestComponent();

tc2.setBorder(BorderFactory.createLineBorder(Color.PINK));

contentPane.add(tc1);

contentPane.add(tc2);

}

}

class TestComponent extends JComponent {

public void paintComponent(Graphics g1) {

Graphics2D g = (Graphics2D) g1;

g.setColor(Color.RED);

g.drawLine(0, 0, this.getWidth(), this.getHeight());

}

}

java 画线函数

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

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

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

java中在面板上怎么画直线?

方法1:

public static void main(String[] args) {

    //JPanel  p = new JPanel(); 注释掉这句

    JFrame frame = new JFrame("DrawLine");

    frame.add(new DrawLine());//将p对象换成本类

    //因为本类继承了JPanel重写paintComponent进行绘制,是绘制到本类的panel上的,

    //而不是绘制到new Panel()对象

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setSize(300, 200);

    frame.setVisible(true);

}

protected void paintComponent(Graphics g) {

    super.paintComponent(g);

    g.drawLine(50, 50, 200, 250);

}

方法2:

//这种方式可能 让你更理解

public static void main(String[] args) {

DrawLine dl = new DrawLine();//新建对象

dl.init();//执行初始化

}

private void init(){

//JPanel  p = new JPanel(); 注释掉这句

JFrame frame = new JFrame("DrawLine");

frame.add(this);//将p对象换成本类

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(300, 200);

frame.setVisible(true);

}

protected void paintComponent(Graphics g) {

    super.paintComponent(g);

    g.drawLine(50, 50, 200, 250);

}

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和画线的英语怎么说的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-02,除非注明,否则均为首码项目网原创文章,转载请注明出处。