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