本文目錄一覽:
- 1、Java中,哪個類提供用於畫圖的方法?哪個方法可以畫線,哪個顯示矩形?
- 2、Java awt包中Graphics類中drawChars怎麼用
- 3、java語言的awt 包的 graphics 類的抽象方法
- 4、用java實現一個方法 關於繪製走勢圖
- 5、關於java中畫圖形的paint方法
- 6、JAVA swing編程 畫圖問題 paint()方法
Java中,哪個類提供用於畫圖的方法?哪個方法可以畫線,哪個顯示矩形?
Graphics類是專門用來畫圖的,g.drawLine(int x,int y,int x1,int y1)用來畫線,g.drawRetrangle(int x,int y,int x1,int y1)用來畫矩形,具體的你可以參考幫助文檔
Java awt包中Graphics類中drawChars怎麼用
drawChars
public void drawChars(char[] data,
int offset,
int length,
int x,
int y)使用此圖形上下文的當前字體和顏色繪製由指定字元數組給定的文本。首字元的基線位於此圖形上下文坐標系的 (x, y) 位置處。
參數:
data – 要繪製的字元數組
offset – 數據的初始偏移量
length – 要繪製的字元數
x – 文本基線的 x 坐標
y – 文本基線的 y 坐標
你應該是問Graphics怎麼用的吧
這是個虛類,所以需要被繼承了以後重寫裡面的方法。
而在用的過程中一般會由comment傳給你。
比如一個JFrame
你寫一個新的類繼承自JFrame。
然後重寫他的paint(Graphics g)函數。
這樣在這個函數體中,你就能收到從repaint發給你的Graphics對象。
然後使用g.drawChars函數傳入相應的值就行。
java語言的awt 包的 graphics 類的抽象方法
實際傳給paint方法的參數是抽象類Graphics的子類,這個子類實現了你提到的那些抽象方法,這個子類由Java語言自身提供,不需要使用者自己實現,當然你也可以自己寫個子類去實現抽象類Graphics中的這些抽象方法,提供你自己的繪圖功能。
在paint方法中加一句System.out.println(g.getClass());你就可以看到具體實現類的名字了。
用java實現一個方法 關於繪製走勢圖
我來講下用Java編寫一個繪製圖形的小程序吧,你看下有沒有用
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
//不規則圖形的繪製
public class IrregularShapeDemo extends JFrame {
GeneralPath gPath= new GeneralPath(); //GeneralPath對象實例
Point aPoint;
//構造函數
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中畫圖形的paint方法
代碼如下:
/**分析下例:我們只是new了一個對象並沒有調用Paint()方法那為什麼會畫出圖呢?
* Graphics這個類的對象就是一隻畫筆,當某容器調用paint()時就會在該容器中畫圖。
* 當窗口產生時本身就存在一隻畫筆,我們只需要拿到畫筆重寫Paint()便可以隨心作畫。
*每次需要重畫的時候就會自動調用paint(Graphics g)(什麼時候需要重畫呢?如當窗口被覆蓋又重新置頂時,當窗口最小化又最大化時等等)
*/
總結:我們想要在容器中畫圖時只需要做的就是: 在該容器中重寫Paint() 系統會自動傳給我們畫筆,自動調用paint方法按照我們的意願進行作畫。
public class TestGraphics extends Frame. {
public static void main(String []args) {
new TestGraphics(“畫圖”,100,100,200,200,Color.white);
}
public TestGraphics(String s,int x,int y,int w,int h,Color c) {
super(s);
this.setBounds(x, y, w, h);
this.setBackground(c);
this.setVisible(true);
}
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.magenta);
g.fillOval(100, 100, 50, 50);
g.setColor(Color.green);
g.fill3DRect(60, 100, 50, 50, false);
g.setColor(c);
}
}
小例子2:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
原理是:在Frame中增加成員變數-容器ArrayList,用它來容納點,每次點擊滑鼠就觸發了事件:往容器中添加一個點,然後立即調用repaint方法強制畫出容器中所有的點來
所以我們利用容器來”裝”點,然後通過iterator來遍歷畫出所有的點。
適配器類:使用適配器類可以只重寫我們需要的方法
因為這些適配器類都已經實現了相應的介面即把所有的方法都空實現了一遍 我們只需要重寫我們需要的方法即可
repaint -調用- update() – 調用 – paint();
*/
public class MyFrame. extends Frame. {
ArrayListPointal ;//泛型指定容器中只能放入Point
public MyFrame(String s) {
super(s);
al =new ArrayListPoint();
this.setBounds(100, 100, 200, 200);
this.setBackground(Color.darkGray);
this.setVisible(true);
this.addMouseListener(new MouseAdapter(){//匿名類
@Override
public void mousePressed(MouseEvent e) {
MyFrame. f = (MyFrame)e.getSource();//e是事件,e.getSource()是獲取事件源即窗口 f
f.al.add(new Point(e.getX(),e.getY())); //而e.getX(),e.getY()則是獲取事件發生的x,y坐標
repaint();//每次點擊滑鼠觸發事件時都有了新的點,所以強制要求重畫,才能立刻顯示出該點否則只有窗口被最小化又最大化後才能看到剛才的點
}
});
//匿名類:在參數處我們傳遞的是new WindowAdapter() {匿名類體} );他沒有名字,我們把它當成WindowAdapter來用,為什麼可以這樣呢?因為語法上規定了匿名類要麼是實現了前面的介面,要麼是從前面的類繼承,就著前面父類的名字來寫類體。當此類與其他類關係不大時可以用匿名類
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public void paint(Graphics g) {
Iterator Pointit= al.iterator();//泛型指定取出元素時只能是point
while(it.hasNext()) {
Point p = it.next();//由於使用泛型這時候就不用強制轉換了
Color c = g.getColor();//保護原有顏色
g.setColor(Color.pink);
g.fillOval(p.x-6, p.y-6, 12, 12);
g.setColor(c);
}
}
public static void main(String []args) {
new MyFrame(“點擊”);
}
}
JAVA swing編程 畫圖問題 paint()方法
super.paint()的作用是把當前的區域清空,每次resize的時候就會自動調用paint()方法,paint()方法里先調用了super.paint()清空當前區域,再畫一個矩型筐,當然每次只有一個了。 另外你的演算法也有漏洞,當你想從右上角拉到左下角畫矩形的時候是沒有反應的。 下面這個程序修改了只畫一個的錯誤,改進了右上角拉到左下角的漏洞,還增加了拖動的中間過程。沒時間給你寫注釋,自己看吧。 import java.awt.*; import java.awt.event.*; import java.util.Vector; import javax.swing.*; public class Hello extends JFrame implements MouseListener,MouseMotionListener{ int x一,y一; Vector rectangles=null; public Hello(){ this.setTitle(“畫圖小程序”); this.addMouseListener(this); this.addMouseMotionListener(this); this.setSize(四00,四00); rectangles=new Vector(); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void paint(Graphics g){ super.paint(g); g.setColor(Color.BLUE); for(int i=0;irectangles.size();i++){ Rectangle rec=rectangles.get(i); int tmp_x=rec.x; int tmp_y=rec.y; int tmp_w=rec.width; int tmp_h=rec.height; g.drawRect(tmp_x,tmp_y,tmp_w,tmp_h); } } public static void main(String[] args) { new Hello(); } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { x一=e.getX();y一=e.getY(); } public void mouseReleased(MouseEvent e) { int x二=e.getX(); int y二=e.getY(); int x=0; int y=0; Graphics g=this.getGraphics(); g.setColor(Color.BLUE); int tmp_w=0; int tmp_h=0; if(x一x二){ x=x二; tmp_w=x一-x二; } if(y一y二){ y=y二; tmp_h=y一-y二; } rectangles.add(new Rectangle(x,y,tmp_w,tmp_h)); this.repaint(); } public void mouseDragged(MouseEvent e) { int x二=e.getX(); int y二=e.getY(); int x=0; int y=0; Graphics g=this.getGraphics(); g.setColor(Color.BLUE); int tmp_w=0; int tmp_h=0; if(x一x二){ x=x二; tmp_w=x一-x二; } if(y一y二){ y=y二; tmp_h=y一-y二; } paint(g); g.drawRect(x,y,tmp_w,tmp_h); } public void mouseMoved(MouseEvent e) { }
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/185551.html