展开全部
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();
}
}
}
}
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();
}
}
}
}
追问
谢谢你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 CopyOfDrawLine1 extends Frame {
Button b = null;
Random r = new Random();
public static void main(String[] args){
CopyOfDrawLine1 dl = new CopyOfDrawLine1();
dl.lunchFrame();
}
private void lunchFrame() {
setBounds(300,100,800,600);
setVisible(true);
addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
b = new Button("直线");
this.add(b);
setLayout(null);
b.setBounds(350, 560, 100, 20);
b.addActionListener(new Moniter());
}
int i = 0;
public void paint(Graphics g){
if(i > 0)
g.drawLine(200, 300, 500, 300);
}
class Moniter implements ActionListener{
public void actionPerformed(ActionEvent e) {
i++;
repaint();
}
}
}
展开全部
注册个按钮事件 ,然后写个类集成jpanel ,复写 draw 方法 ,在里面画直线,然后 按钮 里面写 repaint
追问
能把详细的代码告诉我吗,急盼之中
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
???????????、一个按钮来直线
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询