当前位置:首页 » 编程语言 » java画直线

java画直线

发布时间: 2022-03-30 16:30:06

A. java画多条直线问题

还有用点画直线的算法有点问题,当x8-x8>1,y8-y8<1的情况和x8-x8<1,y8-y8>1的情况画不出来,寻求算法 import java.awt.*; import java.awt.Graphics; import java.awt.Point; import java.awt.event.MouseEvent...

B. 请问java里,怎样按一下按钮就画一条直线

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Random;

public class DrawLine2 extends Frame {
Button b = null;
Random r = new Random();
ArrayList<Line> lines = new ArrayList<Line>();
Image offScreenImage;
static final int w=800, h=600 , x=300 , y =100 ;

public static void main(String[] args){
DrawLine2 dl = new DrawLine2();
dl.lunchFrame();
}

private void lunchFrame() {
setBounds(x,y,w,h);
this.setTitle("按钮画直线");
setVisible(true);
addWindowListener(new WindowAdapter(){

@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}

});
b = new Button("直线");
/*
Panel p = new Panel();
p.setBounds(350,500,100,100);
p.setBackground(Color.BLUE);
add(p);
*/
setLayout(null);
add(b);
b.setBounds(350, 560, 100, 20);
b.addActionListener(new Moniter());
new Thread(new DrawLines()).start();
}

public void update(Graphics g) {
//1.得到缓冲图像
if(this.offScreenImage == null) {
this.offScreenImage = this.createImage(w, h);
}
//2.得到缓冲图像的画笔
Graphics gOffScreen = this.offScreenImage.getGraphics();
//3.绘制缓冲图像
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.GREEN);
gOffScreen.fillRect(0, 0, w, h);
gOffScreen.setColor(c);
//4.调用paint(),将缓冲图象的画笔传入
paint(gOffScreen);
//5.再将此缓冲图像一次性绘到代表屏幕的Graphics对象,即该方法传入的“g”上
g.drawImage(offScreenImage, 0, 0, null);
}

public void paint(Graphics g){
for(int i=0;i<lines.size();i++){
Line line = lines.get(i);
line.draw(g);
}

}

class Moniter implements ActionListener{

@Override
public void actionPerformed(ActionEvent e) {
Line line = new Line();
lines.add(line);
}

}

class Line{
int x1 = r.nextInt(w);
int y1 = r.nextInt(h);
int x2 = r.nextInt(w);
int y2 = y1;
public void draw(Graphics g) {

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

}

class DrawLines implements Runnable{

@Override
public void run() {
try {
while(true){
Thread.sleep(50);
repaint();
}

} catch (InterruptedException e) {
e.printStackTrace();
}

}

}

}

C. java 用点画直线问题

import java.awt.*;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.awt.event.*;
public class TestMain1 {
public static void main(String[] args) {
FrameDraw1 fd=new FrameDraw1();
}
}
class FrameDraw1 extends Frame{
int i;
static ArrayList <Point[]> list =new ArrayList<Point[]>();

public FrameDraw1(){
init();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);}
});
this.setBounds(20,20,400,400);
this.setVisible(true);
}
void init(){
this.addMouseListener(new Moo1(this));
}
public void paint(Graphics g){
for(int i=0;i<list.size();i++){
Point []p=(Point[])list.get(i);
int j=2;
while(p[j]!=null&&j<100){
g.fillOval(p[j].x,p[j].y,4,4);
j++;
}
}
}

}
class Moo1 extends MouseAdapter{
Point point;
Point points[]=new Point[100];
FrameDraw1 fd;
ArrayList <Point>list=new ArrayList<Point>();
public Moo1(FrameDraw1 fd){
this.fd=fd;
}
public void mousePressed(MouseEvent e){
point=(Point)e.getPoint();
points[0]=point;//points[0]放的是起点的点坐标

}
public void mouseReleased(MouseEvent e) {
point=(Point)e.getPoint();
points[1]=point;//points[1]放的是终点的点坐标
savePoint(points[0].x,points[0].y,points[1].x,points[1].y);
fd.repaint();
// FrameDraw1.list.add(list);
}
void savePoint(int x1,int y1,int x2,int y2){
int x,y,a,b,d,d1,d2;
a=y1-y2;
b=x2-x1;
x=x1;
y=y1;
point=new Point(x,y);
d=2*a+b;d1=2*a;d2=2*(a+b);
points[2]=point;
int i;
for(i=3,x=x1;x<=x2&&i<100;x++,i++){
if(d<0){y++;d+=d2;}
else{d+=d1;}
point=new Point(x,y);
points[i]=point;
}
FrameDraw1.list.add(points);

}
}
import java.awt.*;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.awt.event.*;
public class TestMain1 {
public static void main(String[] args) {
FrameDraw1 fd=new FrameDraw1();
}
}
class FrameDraw1 extends Frame{
int i;
static ArrayList <Point[]> list =new ArrayList<Point[]>();

public FrameDraw1(){
init();
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);}
});
this.setBounds(20,20,400,400);
this.setVisible(true);
}
void init(){
this.addMouseListener(new Moo1(this));
}
public void paint(Graphics g){
for(int i=0;i<list.size();i++){
Point []p=(Point[])list.get(i);
int j=2;
while(p[j]!=null&&j<100){
g.fillOval(p[j].x,p[j].y,4,4);
j++;
}
}
}

}
class Moo1 extends MouseAdapter{
Point point;
Point points[]=new Point[100];
FrameDraw1 fd;
ArrayList <Point>list=new ArrayList<Point>();
public Moo1(FrameDraw1 fd){
this.fd=fd;
}
public void mousePressed(MouseEvent e){
point=(Point)e.getPoint();
points[0]=point;//points[0]放的是起点的点坐标

}
public void mouseReleased(MouseEvent e) {
point=(Point)e.getPoint();
points[1]=point;//points[1]放的是终点的点坐标
savePoint(points[0].x,points[0].y,points[1].x,points[1].y);
fd.repaint();
// FrameDraw1.list.add(list);
}
void savePoint(int x1,int y1,int x2,int y2){
int x,y,a,b,d,d1,d2;
a=y1-y2;
b=x2-x1;
x=x1;
y=y1;
point=new Point(x,y);
d=2*a+b;d1=2*a;d2=2*(a+b);
points[2]=point;
int i;
for(i=3,x=x1;x<=x2&&i<100;x++,i++){
if(d<0){y++;d+=d2;}
else{d+=d1;}
point=new Point(x,y);
points[i]=point;
}
FrameDraw1.list.add(points);

}
}

D. 用java怎么在一张图片上画任意直线

java画直线很简单吧
Graphics的drawLine(int x1, int y1, int x2, int y2) 方法

js里画直线比较麻烦, js里的直线(斜线)是一个一个div(长,宽为一像素)连起来(通过css)的.

E. 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());
}
}

F. Java中怎样用graphic完成画直线的功能

G. java中画直线的问题!!!

import javax.swing.*;
import java.awt.*;
public class ex1_shapes extends JFrame{
/**
* @param args
*/
public ex1_shapes(){
super("实验一:直线、圆弧、多边形");
setSize(500,500);
show();
}
public void paint(Graphics g){
super.paint(g);
//直线演示
g.setColor(Color.BLUE);
g.drawLine(10, 10, 80, 150);
g.drawString("直线演示",50,50);
// 圆弧演示 显示“CS”
g.setColor(Color.green);
g.drawArc(180, 50, 50, 90, 30, 300); //c
g.drawArc(250, 50, 50, 45, 30,250); g.drawArc(250, 95, 50, 45, 90, -270); //S
g.drawString("圆弧演示",330,50);
//多边形 五角星
g.setColor(Color.RED);
int[] xpoints1={100,120,200,136,160,100,40,64,0,80};
int[] ypoints1={210,270,270,326,410,357,410,326,270,270};
g.drawPolygon(xpoints1, ypoints1, 10);
g.drawString("折线段演示",80,440);
//填充五角星
int[] xpoints2={350,370,450,386,410,350,290,314,250,330};
int[] ypoints2={210,270,270,326,410,357,410,326,270,270};
g.fillPolygon(xpoints2, ypoints2, 10);
g.drawString("填充图演示",330,440);

}

public static void main(String[] args) {
ex1_shapes demo=new ex1_shapes();
demo.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
// TODO 自动生成方法存根
}

}
刚做过的实验 上面代码可以运行
第一部分就是画直线的 顺便圆弧的也贴出来了 有注释 把BLUE改成GREEN就可以了
就是用Graphics类的draoLine()方法
g.drawLine(10, 10, 80, 150); 也可以用Graphics2D的方法~

热点内容
诈骗的脚本 发布:2025-01-16 23:51:27 浏览:314
电脑配置有点低怎么玩和平精英 发布:2025-01-16 23:46:14 浏览:818
ipfs分布式服务器是什么币种 发布:2025-01-16 23:32:29 浏览:991
android动态icon 发布:2025-01-16 23:03:12 浏览:605
优酷电脑缓存在哪 发布:2025-01-16 22:58:29 浏览:298
进口途锐哪个配置好 发布:2025-01-16 22:35:24 浏览:962
骨干路由器怎么配置 发布:2025-01-16 22:24:39 浏览:244
途安2021款买哪个配置 发布:2025-01-16 22:21:01 浏览:329
图片的压缩原理 发布:2025-01-16 22:17:15 浏览:493
云服务器本地电脑 发布:2025-01-16 22:17:04 浏览:961