java中画线的代码,java绘画图的代码
java中在面板上怎么画直线?
方法1:
成都创新互联专注为客户提供全方位的互联网综合服务,包含不限于网站设计制作、成都网站建设、华亭网络推广、成都微信小程序、华亭网络营销、华亭企业策划、华亭品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联为所有大学生创业者提供华亭建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com
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 绘图板 如何实现画直线的功能
这个简单
可以调用方法drawline(int x1, int y1, int x2, int y2)
其中(x1, y1), (x2, y2)分别为直线起点和终点的坐标
特意给你写了个小例子,希望能帮到你
***************************************
import javax.swing.*;
import java.awt.*;
public class DrawLine extends JPanel {
public static void main(String[] args) {
JFrame frame = new JFrame("DrawLine");
frame.getContentPane().add(new DrawLine());
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);
}
}
****************************
将程序复制到记事本中并改名为DrawLine.java(注意大小写)
然后在命令行中用 CD+文件夹 使命令范围到DrawLine.java的文件夹中
然后执行命令javac DrawLine.java
再执行命令java DrawLine
你会看到结果
java 如何在jpanel上画垂直线、水平线、平行线?
定义一个类继承JPanel类,重写 JPanel 的 paint 方法,使用 Graphics 绘制线段,如下代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
class MyPanel extends JPanel {
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.RED);
for (int i = 0; i 10; i++) {
g.drawLine(10, 10 + i * 20, this.getWidth() - 10, 10 + i * 20);
}
for (int i = 0; i 10; i++) {
g.drawLine(10 + i * 20, 10, 10 + i * 20, this.getHeight() - 10);
}
}
}
public class App extends JFrame {
public App() {
this.setSize(400, 400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyPanel panel = new MyPanel();
this.add(panel, BorderLayout.CENTER);
}
public static void main(String[] args) {
new App().setVisible(true);
}
}
运行结果:
文章题目:java中画线的代码,java绘画图的代码
网站路径:http://cdiso.cn/article/hspedd.html