java形状
❶ java如何做任意形状的窗体
有各种 Border 可以写的。。。。。。。。。。。
javax.swing.border
Interface Border
All Known Implementing Classes:
AbstractBorder, BasicBorders.ButtonBorder, BasicBorders.FieldBorder, BasicBorders.MarginBorder, BasicBorders.MenuBarBorder, BasicBorders.RadioButtonBorder, BasicBorders.RolloverButtonBorder, BasicBorders.SplitPaneBorder, BasicBorders.ToggleButtonBorder, BevelBorder, BorderUIResource, BorderUIResource.BevelBorderUIResource, BorderUIResource.CompoundBorderUIResource, BorderUIResource.EmptyBorderUIResource, BorderUIResource.EtchedBorderUIResource, BorderUIResource.LineBorderUIResource, BorderUIResource.MatteBorderUIResource, BorderUIResource.TitledBorderUIResource, CompoundBorder, EmptyBorder, EtchedBorder, LineBorder, MatteBorder, MetalBorders.ButtonBorder, MetalBorders.Flush3DBorder, MetalBorders.InternalFrameBorder, MetalBorders.MenuBarBorder, MetalBorders.MenuItemBorder, MetalBorders.OptionDialogBorder, MetalBorders.PaletteBorder, MetalBorders.PopupMenuBorder, MetalBorders.RolloverButtonBorder, MetalBorders.ScrollPaneBorder, MetalBorders.TableHeaderBorder, MetalBorders.TextFieldBorder, MetalBorders.ToggleButtonBorder, MetalBorders.ToolBarBorder, SoftBevelBorder, StrokeBorder, TitledBorder
❷ JAVA (1)设计一个形状类Shape,包含一个getArea()方法,该方法不包含实际语句
abstrat class Shape{
protected abstract float getArea();
}
class Circle extends Shape{
public Circle(float radius){
this.radius=radius;
}
private float radius;
public float getArea(){
return 3.14*radius*radius;
}
}
class Rectangle extends Shape{
private float width;
private float height;
public Rectangle(float width,float height){
this.width=width;
this.height=height;
}
public float getArea(){
return width*height;
}
}
class Triangle extends Shape{
private float hemline;
private float height;
public Triangle(float hemline,float height){
this.hemline=hemline;
this.height=height;
}
public float getArea(){
return 1/2*hemline*height;
}
}
class Trapezoid extends Shape{
private float topLine;
private float bottomLine;
private float height;
public Trapezoid(float topLine,float bottomLine,float height){
this.topLine=topLine;
this.bottomLine=bottomLine;
this.height=height;
}
public float getArea(){
return 1/2*height*(topLine+bottomLine);
}
}
public class TestShape{
private float area=0.0f;
public static void countArea(Shape s){
area+=s.getArea();
}
public static void main(String[] args){
Shape s1=new Circle(1);
countArea(s1);
Shape s2=new Rectangle(1,1);
countArea(s2);
Shape s3=new Triangle(1,1);
countArea(s3);
Shape s4=new Trapezoid(1,2,1);
countArea(s4);
System.out.println("area="+area);
}
}
❸ Java实现一个表示形状的Shape抽象类
不好意思,临睡觉才想起来,这个是完整的程序,你建一个名字是ShapeTest的类,把这段代码复制进去就能运行了,要是sysout方法里有乱码,把字符类型改成UTF-8就行了。
abstract class Shape {
public double x;
public double y;
public double getX(){
return this.x=x;
}
public double getY(){
return this.y=y;
}
abstract double getArea(double x, double y);
}
class Circle extends Shape{
@Override
double getArea(double r, double PI) {
return PI*r*r;
}
}
class Rectangle extends Shape{
@Override
double getArea(double x, double y) {
return x*y;
}
}
class Square extends Shape{
@Override
double getArea(double x,double y) {
return x*y;
}
}
public class ShapeTest{
public static void main(String args[]){
double r=2;
double x=3;
double y=4;
final double PI=3.14;
Circle circle=new Circle();
Rectangle rectangle=new Rectangle();
Square square=new Square();
double circleArea=circle.getArea(r, PI);
double rectangleArea=rectangle.getArea(x, y);
double squareArea=square.getArea(x, y);
System.out.println("圆的面积是:"+circleArea);
System.out.println("矩形的面积是:"+rectangleArea);
System.out.println("正方形的面积是:"+squareArea);
}
}
❹ JAVA怎么画出一个任意大小的圆形和矩形
packagetest.xxl;
importjava.awt.Button;
importjava.awt.Color;
importjava.awt.Cursor;
importjava.awt.Graphics;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.MouseEvent;
importjava.awt.event.MouseListener;
importjavax.swing.JFrame;
,ActionListener{
privatestaticintx=0;
privatestaticinty=0;
privatestaticintw=0;
privatestaticinth=0;
privatestaticColorc;
//真为圆,假为方
privatebooleanflag=false;
=1L;
publicDemo0617(){
this.setSize(440,500);
this.setVisible(true);
this.setLayout(null);
this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
this.setResizable(false);//不能改变窗体大小
this.setBackground(Color.WHITE);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseListener(this);
this.getContentPane().setBackground(Color.WHITE);
Buttonb1,b2,b3,b4,b5,b6,b7,b8,b9;
b1=newButton("红色");
b1.setBounds(0,0,100,30);
b1.setBackground(Color.RED);
b1.addActionListener(this);
this.add(b1);
b2=newButton("黑色");
b2.setBounds(110,0,100,30);
b2.setBackground(Color.BLACK);
b2.addActionListener(this);
this.add(b2);
b3=newButton("黄色");
b3.setBounds(220,0,100,30);
b3.setBackground(Color.YELLOW);
b3.addActionListener(this);
this.add(b3);
b4=newButton("蓝色");
b4.setBackground(Color.BLUE);
b4.setBounds(330,0,100,30);
b4.addActionListener(this);
this.add(b4);
b5=newButton("橡皮擦");
b5.setBounds(0,40,100,30);
b5.addActionListener(this);
this.add(b5);
b6=newButton("撤销");
b6.setBounds(110,40,100,30);
b6.addActionListener(this);
this.add(b6);
b7=newButton("全部删除");
b7.setBounds(220,40,100,30);
b7.addActionListener(this);
this.add(b7);
b8=newButton("圆形");
b8.setBounds(0,80,100,30);
b8.addActionListener(this);
this.add(b8);
b9=newButton("矩形");
b9.setBounds(110,80,100,30);
b9.addActionListener(this);
this.add(b9);
}
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
newDemo0617();
}
@Override
publicvoidpaint(Graphicsg){
if(c==null)
c=g.getColor();
g.setColor(c);
if(flag){
g.fillOval(x,y,w,h);
}else{
g.fillRect(x,y,w,h);
}
}
publicvoidclear(Graphicsg){
g.setColor(Color.WHITE);
g.clearRect(0,0,440,500);
}
/**
*单击
*/
@Override
publicvoidmouseClicked(MouseEvente){
}
/**
*按下
*/
@Override
publicvoidmousePressed(MouseEvente){
x=e.getX();
y=e.getY();
}
/**
*松开
*/
@Override
publicvoidmouseReleased(MouseEvente){
intx=e.getX();
inty=e.getY();
if(x>this.x){
w=x-this.x;
}else{
w=this.x-x;
}
if(y>this.y){
h=y-this.y;
}else{
h=this.y-y;
}
paint(getGraphics());
}
/**
*鼠标进入事件
*/
@Override
publicvoidmouseEntered(MouseEvente){
}
/**
*鼠标移除事件
*/
@Override
publicvoidmouseExited(MouseEvente){
}
@Override
publicvoidactionPerformed(ActionEvente){
switch(e.getActionCommand().hashCode()){
case1038352:
//红色
c=Color.RED;
break;
case1293761:
//黑色
c=Color.BLACK;
break;
case1293358:
//黄色
c=Color.YELLOW;
break;
case1087797:
//蓝色
c=Color.BLUE;
break;
case27138585:
//橡皮擦
c=Color.WHITE;
break;
case836828:
Graphicsgraphics=getGraphics();
graphics.setColor(Color.WHITE);
if(flag){
graphics.fillOval(x,y,w,h);
}else{
graphics.fillRect(x,y,w,h);
}
break;
case657183940:
//全部删除
clear(getGraphics());
break;
case715036:
//圆形
flag=true;
break;
case976025:
//矩形
flag=false;
break;
default:
System.out.println(e.getActionCommand().hashCode());
break;
}
}
}
❺ java 图形 框架
Jgraph http://www.jgraph.com/ 是一个开源的,兼容Swing的基于MVC体系结构图形组件,具有以下特点:
1) 完全Swing兼容;
2) 简单、高效的设计;
3) 时间效率高;
4) 100 %纯Java;
jGraph简介
jGraph具有相当高的交互性和自动化,是一套为图定做的组件。其主要用途是在一些需要表示图结构的应用中,比如流程图、UML、交通线路、网络等等。
jGraph在本文撰写时版本为5.8.0.0,可以在链接出找到jGraph的主页。
jGraph主要包括以下一些产品:
JGraph - The Java Open Source Graph Drawing Component ( 有Open Source )
JGraph Layout Pro - The Java Graph Layout Solution
JGraphpad Pro Diagram Editor Framework
MxGraph Thin Client - JGraph in a browser!
咱们只是学习嘛,当然只用jGraph咯。jGraph Layout Pro是一个对图进行布局的软件,但是目前要收费的,jGraph对图的操作包括:图显示、图交互、图布局、图分析等。
JGraph 的基本SWING 组件如下:
org.jgraph Basic JGraph 类
org.jgraph.event Graph 事件模型
org.jgraph.graph Graph 结构及结点
org.jgraph.plaf Graph UI 委托组件
org.jgraph.util 常用的工具类
补充一下,与jGraph类似的可用于绘图的还有eclipse的GEF。
jGraph模型
一张图——JGraph 类 的主要结构:
JGraph extends JComponent {
org.jgraph.graph.GraphModel model; (DefaultGraphModel)
org.jgraph.plaf.GraphUI ui; (BasicGraphUI)
org.jgraph.graph.GraphLayoutCache cache;
}
JGraph 除了SWING的MVC结构,即引用了MODEL和UI外,他还保持着一个奇怪的应用GraphLayoutCache。 GraphLayoutCache 可以被看作是MODEL的一个扩展,它的作用是保证图中各结点的状态以及一些外观等。因为图的复杂性,使用一个GraphLayoutCache 可以用来处理这些复杂问题。
配置JGraph可以使用一系列的set方法,有许多很有用的功能可以开关。
图的逻辑结构——GraphModel 类:
满足MVC的要求,GraphModel保存着所有的图中的对象,它的默认实现DefaultGraphModel能够满足一般的需求。
GraphModel包含三个基本操作:insert() , edit() , remove() 。这些操作会起到与GraphLayoutCache相同的效果,但与GraphLayoutCache略微不同的是它的参数比较多,乍看下去比较麻烦。 其实GraphModel所要求的只是结点的逻辑结构,对于结点的细节它并不关心。所以可以在初始化图时使用GraphModel,不要常常用它的方法来 对细节做修改,这既不方便,也没必要。
另外,就是GraphModel提供了许多get方法,可以很方便检索相应的结点。
Cells
JGraph 的单位(Cells) 有三种:Vertex、 Edge、 Port。
Vertex 可以携带对象,由于JGraph是只负责表示的,并不真正负责数据的操作。那么在图形和数据间就需要一个使者,这就是Vertex ,Vertex 可以是文字、图形等对象。
Port 是一般比较陌生的单位,在图的算法中并不设计Port,但在图形表示中它十分有用。如同它的名字,他是Vertex上的一个端口,可以通过端口连接其他Vertex,而在JGraph中Port还可以用于改变Edge的形状等等。
Edge 与图算法中的边也有一点不同,Edge 是只能连接Port而不是Vertex的。这样,因为多了Port单元,使得Edge更加灵活、更加丰富了。
默认单元——DefaultCell:
它是DefaultEdge和DefaultPort的父类,又是DefaultMutableTreeNode的子类,其地位可以相当于Vertex。 一个DefaultCell可以携带一个UserObject。每个DefaultCell还有一个AttributeMap,负责它的属性(颜色、大小 等等)。用一套set方法可以修改AttributeMap。
边和端口单元——Edge、Port:
除了继承DefaultCell,Edge、Port还有一些独有的方法。
Edge有getSource() 和 getTarget() 方法,用以获得边的两端的对象(一般为Port)。还一个路由类,定义了一些路由方法。
Port 主要任务是承载Edge,所以有一些关于获得Edge的方法。另外,Port还定义了获得锚(比如一个Vertex中包含一个Port)的方法。
Cell的处理:
每个Cell包括Cell Object、Cell Renderer、Cell Editor、Cell Handle。其中Renderer负责Cell的表示,包括形状等等。Editor 做Cell的修改用,当双击Cell后则调用Editor来编辑Cell。以上都是类似与JTable 和 JTree的。
Handle 是SWING的组件中没有的,它的任务是处理Cell的大小与移动。可以重写paint()方法来指定经过鼠标拖动所导致的Cell大小和位置变化。
对于这些单元的属性的控制,可以仔细看看GraphConstants这个类的set方法,基本上所有的属性都是用这个类的set修改的。
二、JGraph设计
1) MVC
Swing是Java(Sun)提供的UI标准实现之一,Swing基于AWT(Abstract Windowing Toolkit)。JGraph完全兼容Swing,它的实现仍然基于MVC体系结构。
JGraph MVC
View:
JGraph不包含实际的数据,它提供了数据的视;JGraph对象画图的机制是:
将图元定义为一个一个的 cell,每个cell可以是一个顶点(vertex)、边(edge)或者节点(port)中的一种。顶点可以有邻接的顶点,他们通过边相联系,边联接 的两个端点称为目标和源,每个目标或者源是一个节点。节点是顶点的孩子。每个cell都可以有自己的孩子。
每个cell的外观由相应的属性定义,属性序列是指一系列的键-值对,他们以Map形式组织,例如:
Map cellAttrib = new Hashtable();
// Set bounds
Rectangle2D helloBounds = new Rectangle2D.Double(20, 20, 40, 20);
GraphConstants.setBounds(cellAttrib, helloBounds);
// Set black border
GraphConstants.setBorderColor(cellAttrib, Color.black);
一个cell有类似这样一个cellAttrib的Map,来定义其外观。
外观可以指定诸如一条边的箭头样式等属性。
Model:
数据对象可以看成是JGraph中两个独立结构 的链接点:grahp结构和group结构。Graph结构基于图论中的顶点、边定义。Group结构是cell的composition结构。 Graph结构中getSource()和getTarget()方法,获得源和目标节点。而在group中通过getChild(), getParent()来获得cell的组成结构。
2) 低层基于图论逻辑
即:一个图G包含一个非空的元 素集V(G)和一个E(G),其中,E(G)是V(G)中两个无序元素组成的二元组。V(G)称为图G顶点的集合,如果任意集合V(G)中的顶点x/y, (x,y)在E(G)中,边(x,y)可能以连接顶点x和y的边(弧)所代表,X与y就被称为邻接的,否则x与y不邻接。
❻ java 怎么改变button的形状
1,首先明确button是安卓的一个控件,是用java语言写的。
2,设置大小的方法:btn.setbounds(x,y,width,height);//设置大小并定位
或者btn.setsize(width,height);//设置大小btn.setlocation(x,y);//定位
3,也可以在布局文件上直接给定大小
比如:
这个button控件高度和宽带都是100px
❼ 如何使用java绘制几何形状到图片
java 输出菱形代码:
System.out.print(" ");
for (k = 1; k <= 2 * i - 1; k++)
System.out.print("*");
System.out.println("");
}
for (i = 1; i <= 4; i++) {
for (j = 1; j <= i; j++)
System.out.print(" ");
for (k = 1; k <= 9 - 2 * i; k++)
System.out.print("*");
System.out.println("");
}
}
}
绘制算法:
1、分为两部分,上半部分和下半部分
2、输出空格部分换个输出*部分
3、最后一个标签需要换行
❽ java如何制作图片一样形状的按钮
importjava.net.URL;
importjavax.swing.ImageIcon;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
publicclassanniuextendsJFrame{
publicstaticvoidmain(Stringargs[]){
newanniu().setVisible(true);
}
publicanniu(){
super();
this.getContentPane().setLayout(null);
setBounds(100,100,257,160);
finalJPanelpanel=newJPanel();
panel.setLayout(null);
panel.setBounds(0,0,249,126);
getContentPane().add(panel);
finalJButtonbutton=newJButton();
button.setContentAreaFilled(false);
button.setBorder(null);
URLurl=getClass().getResource("3.png");
ImageIconicon=newImageIcon(url);
button.setIcon(icon);
button.setBounds(45,48,40,40);
panel.add(button);
}
}
这个是我写的透明图片按钮的代码,你看下有没用吧。。
❾ 用Java定义一个形状类Shape
publicabstractclass Shape {
publicabstractvoid area();
}
class Circle extends Shape {
privatedoubleradius;
privatedoubleS;
Circle(double radius) {
this.radius = radius;
}
publicvoid area() {
S = 3.14 * radius * radius;
System.out.println(S);
}
}
class Rect extends Shape {
privatedoublelength;
privatedoublewidth;
privatedoubleS;
Rect(double length,double width) {
this.length = length;
this.width = width;
}
publicvoid area() {
S = length * width;
System.out.println(S);
}
}
class Test {
publicstaticvoid main(String[] args) {
Circle a = new Circle(3);
a.area();
Rect b = new Rect(3,4);
b.area();
}
}
❿ Java如何让一个形状动起来,就比如贪吃蛇
你知道动画是如何动起来的么?没错,就是一张一张的画,快速地闪过,当速度足够快的时候,就好像这个图形动起来了..
同理,在用java做可移动图形的时候,比如我们用awt绘图,当我们一遍一遍擦除重绘,速度到一定程度的时候, 这个图形就好像动了起来..
2018年8月28日15:55:01