JAVA学习---绘制图形

import java.awt.GridLayout;

import javax.swing.JFrame;


public class TestFigurePanel extends JFrame {

	public TestFigurePanel()
	{
		setLayout(new GridLayout(2,3,5,5));
		add(new FigurePanel(FigurePanel.LINE));
		add(new FigurePanel(FigurePanel.RECTANGLE));
		add(new FigurePanel(FigurePanel.ROUND_RECTANGLE));
		add(new FigurePanel(FigurePanel.OVAL));
		add(new FigurePanel(FigurePanel.RECTANGLE, true));
		add(new FigurePanel(FigurePanel.RECTANGLE, false));
		add(new FigurePanel(FigurePanel.ROUND_RECTANGLE, true));
		add(new FigurePanel(FigurePanel.OVAL, true));
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		TestFigurePanel frame = new TestFigurePanel();
		frame.setSize(400,300);
		frame.setTitle("TestFigurePanel");
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JPanel;


public class FigurePanel extends JPanel {
	
	// define constants
	public static final int LINE = 1;
	public static final int RECTANGLE = 2;
	public static final int ROUND_RECTANGLE = 3;
	public static final int OVAL = 4;
	
	private int type = 1;
	private boolean filled = false;
	
	public FigurePanel()
	{
		
	}
	
	public FigurePanel(int type)
	{
		this.type = type;
	}
	
	public FigurePanel(int type, boolean filled)
	{
		this.type = type;
		this.filled = filled;
	}
	
	protected void paintComponent(Graphics g)
	{
		super.paintComponents(g);
		
		int width = getWidth();
		int height = getHeight();
		
		System.out.println("width = " + width + " heigth = "+ height);
		System.out.println("width = " + ((int)width*0.1) + " heigth = "+ height);
		switch (type)
		{
		case LINE:
		{
			g.setColor(Color.BLACK);
			g.drawLine(10, 10, width - 10, height - 10);
			g.drawLine(width - 10, 10, 10, height - 10);
		}
		case RECTANGLE:
		{
			g.setColor(Color.BLUE);
			
			
			if (filled)
			{
				g.fillRect((int)(0.1*width), (int)(0.1 * height), (int)(0.8 *width), (int)(0.8*height));
			}
			else
			{
				g.drawRect((int)(0.1*width), (int)(0.1 * height), (int)(0.8 *width), (int)(0.8*height));
			}
		}
		case ROUND_RECTANGLE:
		{
			g.setColor(Color.RED);
			if (filled)
			{
				g.fillRoundRect((int)(0.1*width), (int)(0.1 * height), (int)(0.8 *width), (int)(0.8*height), 40, 40);
			}
			else
			{
				g.drawRoundRect((int)(0.1*width), (int)(0.1 * height), (int)(0.8 *width), (int)(0.8*height), 40, 40);
			}
		}
		case OVAL:
		{
			g.setColor(Color.BLACK);
			if (filled)
			{
				g.fillOval((int)(0.1*width), (int)(0.1 * height), (int)(0.8 *width), (int)(0.8*height));
			}
			else
			{
				g.drawOval((int)(0.1*width), (int)(0.1 * height), (int)(0.8 *width), (int)(0.8*height));
			}
		}
		}
	}
	
	public void setType(int type)
	{
		this.type = type;
	}
	public int getType()
	{
		return type;
	}
	
	public void setFilled(boolean filled)
	{
		this.filled = filled;
		repaint();
	}
	
	public boolean isFilled()
	{
		return filled;
	}
	
	public Dimension getPreferredSize()
	{
		return new Dimension(80, 80);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java绘制不规则几何图形,比如划曲线,写字,线条随意画,如截图所示,甚至可以写出文字:   不规则图形绘制代码:   public class IrregularShapeDemo extends JFrame {    GeneralPath gPath= new GeneralPath(); //GeneralPath对象实例   //构造函数   public IrregularShapeDemo() {    super("不规则图形绘制"); //调用父类构造函数    enableEvents(AWTEvent.MOUSE_EVENT_MASK|AWTEvent.MOUSE_MOTION_EVENT_MASK); //允许事件    setSize(300, 200); //设置窗口尺寸    setVisible(true); //设置窗口可视    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序    }    public void paint(Graphics g) { //重载窗口组件的paint()方法    Graphics2D g2D = (Graphics2D)g; //获取图形环境    g2D.draw(gPath); //绘制路径    }    public static void main(String[] args) {    new IrregularShapeDemo();    }    protected void processMouseEvent(MouseEvent e) { //鼠标事件处理    if(e.getID() == MouseEvent.MOUSE_PRESSED) {    aPoint = e.getPoint(); //得到当前鼠标点    gPath = new GeneralPath(); //重新实例化GeneralPath对象    gPath.moveTo(aPoint.x,aPoint.y); //设置路径点    }    }    protected void processMouseMotionEvent(MouseEvent e) { //鼠标运动事件处理    if(e.getID() == MouseEvent.MOUSE_DRAGGED) {    aPoint = e.getPoint(); //得到当前鼠标点    gPath.lineTo(aPoint.x, aPoint.y); //设置路径    gPath.moveTo(aPoint.x, aPoint.y);    repaint(); //重绘组件    }    }   }
Java学习思维导图是一种辅助工具,可以帮助学习者整理和梳理Java编程语言的相关知识点和概念。通过思维导图的方式,可以将Java语言中的不同概念、语法和特性等以图形化的形式呈现出来,方便学习者进行记忆和理解。 在进行Java学习时,思维导图可以作为一种学习辅助工具,帮助学习者将复杂的Java知识进行分类和整理。通过绘制思维导图,可以将Java的基础知识、面向对象编程、异常处理、集合框架、多线程等内容进行归类和梳理,形成一个完整的知识结构。学习者可以根据自己的学习进度和重点,逐步完善和扩充思维导图,从而更好地理解Java编程语言。 通过下载Java学习思维导图,学习者可以在电脑或移动设备上随时随地进行学习和查看。可以将思维导图保存到本地,方便离线使用。另外,一些教育、培训机构或个人开发者也会提供一些开源的或付费的Java学习思维导图,供学习者下载和使用。这些导图通常是经过精心设计和整理的,涵盖了Java学习的各个方面,并提供了详细的解释和示例。 总之,Java学习思维导图是学习Java编程语言时的有力辅助工具,通过图形化的方式整理和展示Java知识,帮助学习者更好地理解和掌握Java的相关概念和语法。下载和使用Java学习思维导图可以提高学习的效率和质量,推动自己在Java编程领域的进步。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值