「java画线slope」java画线段
今天给各位分享java画线slope的知识,其中也会对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如何画直线?
这个简单
可以调用方法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中画直线的问题!!!
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的方法~
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画线slope的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java画线段、java画线slope的信息别忘了在本站进行查找喔。
发布于:2022-11-28,除非注明,否则均为
原创文章,转载请注明出处。