用java画线段,矩形等等图形(持续更新)

1.源代码分为三个类别,第一个是main,第二个是frame 第三个是panel

2,main中创建线程,线程中创建frame,并且设置可见;

3,frame的构造函数中,创建panel,并且添加panel进入自己;

4,panel继承JPanel,重载函数paintComponent(Graphics g),在这个函数里面调用画图函数,并且使用这个g(需要创建2d副本)



------------------------------------

1.

package graphTest;


import java.awt.EventQueue;


import javax.swing.*;


public class Main {


public static void main(String[] args) {
// TODO Auto-generated method stub
/*
* 在awt的事件队列线程之中,创建窗口和组件,确保线程安全
* 就是说,组建的创建,绘制,和事件响应,需要处于同一线程

*/
EventQueue.invokeLater(new Runnable(){
public void run()
{
MyFrame frame=new MyFrame();
frame.setVisible(true);
}
});
}


}

-----------------

2.

package graphTest;
import javax.swing.JFrame;
import javax.swing.WindowConstants;


class MyFrame extends JFrame
{
public static final String TITLE="java 图形绘制";
public static final int WIDTH=500;
public static final int HEIGHT=500;

public MyFrame()
{
super();
this.initFrame();
}
public void initFrame()
{
this.setTitle(TITLE);
this.setSize(WIDTH,HEIGHT);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//设置到屏幕中心
this.setLocationRelativeTo(null);

//设置窗口的内容面板
//个人感觉这一段的逻辑不是很好理解
MyPanel panel=new MyPanel(this);
this.setContentPane(panel);
}

}


3-------------------

package graphTest;
import graphTest.MyFrame;
import java.awt.*;






import javax.swing.*;


public class MyPanel extends JPanel{
private MyFrame frame;
public MyPanel(MyFrame frame)
{
super();
this.frame=frame;
}
@Override
protected void paintComponent(Graphics g)
//是由系统斯调用的,不是我们自己调用的。
{
super.paintComponent(g);
drawLine(g);
}
private void drawLine(Graphics g) {
//System.out.println("为什么我会自动运行?");
frame.setTitle("1. 线段&折线段");
//Graphcis 的副本,因为我们希望改变的graphics的参数,
Graphics2D g2d=(Graphics2D)g.create();//将父类强制转换成子类
//抗锯齿?我不是很懂啊?
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.red);
 
 
//  1  draw line with two points
g2d.drawLine(50, 50, 100,50);
// g2d.dispose();
 
//  2  draw line with many points 
 
int x2[]=new int[]{50,100,150,200};//a row of xpoints 
int y2[]=new int[]{100,120,80,100};//a row of ypoints duplicated with each other 
int num2=4;
g2d.drawPolyline(x2, y2, num2);
 
// 3 draw line with two points and set stroke which means width of line
BasicStroke bs1=new BasicStroke(5);//width is 5px
g2d.setStroke(bs1);//set stroke in g2d with this stroke
g2d.drawLine(110,200,150, 300);
 
 
// 4 draw a dotted line 
float dash[]=new float[]{5,10};
BasicStroke bs2=new BasicStroke(1,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_MITER,10.0f,dash,0.0f);
g2d.setStroke(bs2);
g2d.drawLine(300,300,300,450);
g2d.dispose();
     }

}



4------------------

更新函数,画矩形,从panel类的重载函数drawcomponent中调用

private void drawRect(Graphics g)
{
frame.setTitle("2 绘制矩形");
Graphics2D g2d=(Graphics2D)g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.blue);
g2d.drawRect(30, 20,50,80 );
g2d.drawRect(34, 26,50,80 );
g2d.dispose();

}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值