java畫直線
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的方法~