java基础03 JPanel绘图

绘图

绘图主要使用的是JPanel对象,需要重写对应的paintComponen方法,这里需要注意的是,在该方法中我们获得是Graphics对象,如果我们需要获得Graphics2d对象,需要运行以下代码进行转换。

Graphics2D graphics2D= (Graphics2D) g;

可以通过Math的随机数设置画布的颜色,RGB定义,重写的方法如下。

package GuiTest;

import javax.swing.*;
import java.awt.*;

public class JPanelTest extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D graphics2D= (Graphics2D) g;
        //依据图片文件获取图片对象
        Image image=new ImageIcon("").getImage();
        //将图片对象在画布中画出来
        g.drawImage(image,3,4,this);
        //生成随机的rgb颜色
        int red=(int) (Math.random()*255);
        int green=(int) (Math.random()*255);
        int blue=(int) (Math.random()*255);
        //生成Color对象
        Color randomColor=new Color(red,green,blue);
        //设置颜色
        g.setColor(randomColor);
        //画出对应的画布 Ract是方形 Oval是椭圆
        g.fillRect(20,50,100,100);
    }

}

从而在方法中可以实现 点击按钮后改变画布的颜色,每当点击按钮后 调用JFrame的repaint方法实现刷新画布。

package GuiTest;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JframeMain implements ActionListener {
    JButton button;
    JFrame frame;
    public static void main(String[] args) {
        JframeMain jframeMain=new JframeMain();
        jframeMain.go();
    }
    void go(){
        //声明一个Frame对象
        frame=new JFrame();
        //声明一个按钮组件 并且设置组件内容
        button=new JButton("click me");
        button.addActionListener(this);
        JPanelTest jPanelTest=new JPanelTest();
        //设置程序随着窗口关闭结束
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //将按钮组件添加到Frame中
        frame.getContentPane().add(BorderLayout.SOUTH,button);
        //将画布添加到中央
        frame.getContentPane().add(BorderLayout.CENTER,jPanelTest);
        //设置Frame组件的大小
        frame.setSize(300,300);
        //将frame显示出来
        frame.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //继承接口类之后 重写响应事件 事件发生后运行本方法
        frame.repaint();
    }
}

渐变色的实现

不仅仅可以实现纯色,还可以实现渐变色,代码如下。

package GuiTest;

import javax.swing.*;
import java.awt.*;

public class JpanelGenralColor extends JPanel {
    @Override
    protected void paintComponent(Graphics g) {
        //获取graphics2D对象
        Graphics2D graphics2D= (Graphics2D) g;
        //生成随机的rgb颜色
        int red=(int) (Math.random()*255);
        int green=(int) (Math.random()*255);
        int blue=(int) (Math.random()*255);
        //生成初始对象
        Color startcolor=new Color(red,green,blue);

        red=(int) (Math.random()*255);
        green=(int) (Math.random()*255);
        blue=(int) (Math.random()*255);
        //生成终止颜色
        Color endcolor=new Color(red,green,blue);
        //设置GradientPaint生成渐变色
        GradientPaint gradientPaint=new GradientPaint(70,70,startcolor,
                150,150,endcolor);
        //设置画布
        graphics2D.setPaint(gradientPaint);
        //设置大小
        graphics2D.fillOval(70,70,100,100);

    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值