java有画图的库吗_Java画图
//利用awt.Graphics畫圖
//Graph用于顯示所要畫的圖型
//Shape的子類定義可以圖形
//要改變圖形,可以利用Graphics類提供的方法實現
//要在同一窗口顯示更多圖形,通過重載paint()實現。
java" style="padding: 0.3em; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-top: 0px; margin-bottom: 1.5em; line-height: 1.5em; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); overflow-y: auto; background-color: rgb(246, 246, 246);">
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Graph extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private Shape shape;
public Graph(Shape shp) {
super();
this.shape = shp;
}
@Override
public void paint(Graphics g) {
super.paint(g);
shape.draw(g);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame();
frame.setTitle("Shape");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Rect rect = new Rect(10,10,100,100);
Graph grp = new Graph(rect);
frame.add(grp);
frame.setLocationRelativeTo(null);
frame.setSize(600,400);
frame.setVisible(true);
}
}
abstract class Shape{
public abstract void draw(Graphics g);
}
class Rect extends Shape{
private int x,y,sizeX,sizeY;
public Rect(int x,int y,int sizeX,int sizeY){
this.x = x;
this.y = y;
this.sizeX = sizeX;
this.sizeY = sizeY;
}
public void draw(Graphics g){
g.drawRect(x,y,sizeX,sizeY);
}
}
class Line extends Shape{
private int x,y,sizeX,sizeY;
public Line(int x,int y,int sizeX,int sizeY){
this.x = x;
this.y = y;
this.sizeX = sizeX;
this.sizeY = sizeY;
}
public void draw(Graphics g){
g.drawLine(x,y,sizeX,sizeY);
}
}
總結
以上是生活随笔為你收集整理的java有画图的库吗_Java画图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java闭包lambda,闭包在groo
- 下一篇: java美元兑换,(Java实现) 美元