java画线函数代码 java画直线简单程序
java 如何在jpanel上画垂直线、水平线、平行线?
定义一个类继承JPanel类,重写 JPanel 的 paint 方法,使用 Graphics 绘制线段,如下代码:
创新互联公司-专业网站定制、快速模板网站建设、高性价比海兴网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式海兴网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖海兴地区。费用合理售后完善,十年实体公司更值得信赖。
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 drawLine()方法怎么用?
这个方法是画一条直线,都知道两点确定一条直线,而在坐标中由横(X)、纵坐标(y)确定一个点,这四参数实际就是确定两个点,要画的直线的起始点横纵坐标和终点的横纵坐标。
X1,Y1是确定直线的起始点,即横坐标为x1,纵坐标为y1的点。同理x2,y2确定直线的终点。
例:
A(x1,y1) B(x2,y2) 就可以画出直线AB了。
参数:
x1 - 第一个点的 x 坐标。
y1 - 第一个点的 y 坐标。
x2 - 第二个点的 x 坐标。
y2 - 第二个点的 y 坐标。
扩展资料:
注意事项
该方法只能绘制单条直线;如果需要同时绘制多条直线,则可以使用drawLines方法。
基本语法:public void drawLine (float startX, float startY, float stopX, float stopY, Paint paint)
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
paint.setColor(Color.BLACK); //设置画笔颜色
canvas.drawColor(Color.WHITE); //设置背景颜色
paint.setStrokeWidth((float) 1.0); //设置线宽
canvas.drawLine(50, 50, 450, 50, paint); //绘制直线
paint.setStrokeWidth((float) 5.0); //设置线宽
canvas.drawLine(50, 150, 450, 150, paint); //绘制直线
paint.setStrokeWidth((float) 10.0); //设置线宽
canvas.drawLine(50, 250, 450, 250, paint); //绘制直线
paint.setStrokeWidth((float) 15.0); //设置线宽
canvas.drawLine(50, 350, 450, 350, paint); //绘制直线
paint.setStrokeWidth((float) 20.0); //设置线宽
canvas.drawLine(50, 450, 450, 450, paint); //绘制直线
}
java有根据点画直线的函数吗?
//Point类
public class Point {
private int x;
private int y;
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
//DrawLine类
import java.awt.Graphics;
import javax.swing.JPanel;
public class DrawLine extends JPanel {
private Point[] points;
public DrawLine(Point[] points) {
this.points = points;
}
public void paint(Graphics g) {
for (int i = 0; i points.length; i++) {
if(i==points.length-1){
g.drawLine(points[i].getX(), points[i].getY(), points[0].getX(), points[0].getY());
}else{
g.drawLine(points[i].getX(), points[i].getY(), points[i+1].getX(), points[i+1].getY());
}
}
}
}
//main方法程序入口
import javax.swing.JFrame;
public class MainFrame extends JFrame{
private DrawLine dl;
public MainFrame(Point[] points) {
dl = new DrawLine(points);
this.add(dl);
this.setVisible(true);
this.setBounds(112, 84, 800, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
//矩形左上角为坐标原点
int x = 100;//横坐标
int y = 100;//纵坐标
int width = 500;//矩形宽
int height = 400;//矩形高
//4个点按一定的规则可以形成矩形,如果是任意点那就只能把这4个点连起来。
//可以参照g.drawRect(x, y, width, height);方法!
//更多重写的方法靠你自己了,我只是帮你开了个头。参照DrawLine类。
Point[] points = {new Point(x,y),new Point(x+width,y),new Point(x+width,y+height),new Point(x,y+height)};
new MainFrame(points);
}
}
java画函数曲线
我做了一个简单的例子,还没有完善好,只是起到一个抛砖引玉的作用,希望你能自己把它补完整。
package test;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
public class DrawFn extends JFrame{
private JTextField txt_interval;
private JTextField txt_c;
private JTextField txt_b;
private JTextField txt_a;
private drawFnPanel panel = new drawFnPanel();
/**
* @param args
*/
public static void main(String[] args) {
DrawFn frame=new DrawFn();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public DrawFn() {
super();
getContentPane().setLayout(null);
final JLabel aLabel = new JLabel();
aLabel.setText("a=");
aLabel.setBounds(10, 10, 21, 18);
getContentPane().add(aLabel);
txt_a = new JTextField();
txt_a.setBounds(31, 8, 87, 22);
getContentPane().add(txt_a);
final JLabel bLabel = new JLabel();
bLabel.setText("b=");
bLabel.setBounds(136, 10, 21, 18);
getContentPane().add(bLabel);
txt_b = new JTextField();
txt_b.setBounds(159, 8, 87, 22);
getContentPane().add(txt_b);
final JLabel cLabel = new JLabel();
cLabel.setText("c=");
cLabel.setBounds(267, 10, 32, 18);
getContentPane().add(cLabel);
txt_c = new JTextField();
txt_c.setBounds(290, 8, 87, 22);
getContentPane().add(txt_c);
final JLabel label = new JLabel();
label.setText("步长=");
label.setBounds(398, 10, 47, 18);
getContentPane().add(label);
txt_interval = new JTextField();
txt_interval.setBounds(440, 8, 87, 22);
getContentPane().add(txt_interval);
final JButton button = new JButton();
button.addActionListener(new draw_actionAdapter(this));
button.setText("绘制");
button.setBounds(533, 5, 61, 28);
getContentPane().add(button);
panel.setBorder(new LineBorder(Color.black, 1, false));
panel.setBounds(10, 36, 599, 389);
getContentPane().add(panel);
}
public void paintFn(ActionEvent e){
panel.paintFn(Integer.parseInt(txt_a.getText()), Integer.parseInt(txt_b.getText()), Integer.parseInt(txt_c.getText()), Integer.parseInt(txt_interval.getText()));
}
}
class drawFnPanel extends JPanel{
private int _a=0;
private int _b=0;
private int _c=0;
private float _interval=0.01f;
public void paintFn(int a,int b,int c,int interval){
_a=a;
_b=b;
_c=c;
_interval=interval;
repaint();
}
private double getY(float x){
return _a*x*x+_b*x+_c;
}
public void paintComponent(Graphics g){
Graphics2D g2d=(Graphics2D)g;
if(_a==0 _b==0 _c==0){
return;
}
for(float i=-100f;i100f;i=i+_interval){
g2d.drawRect((int)i, (int)getY(i), 1, 1);
}
}
}
class draw_actionAdapter implements ActionListener{
private DrawFn adapter;
public draw_actionAdapter(DrawFn adapter){
this.adapter=adapter;
}
public void actionPerformed(ActionEvent e) {
adapter.paintFn(e);
}
}
网页名称:java画线函数代码 java画直线简单程序
链接地址:http://myzitong.com/article/doeisch.html