9
A
回答
7
我創建了一個小例子程序:
public class Test extends JFrame {
public Test() {
this.setPreferredSize(new Dimension(400, 400));
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void paint(Graphics g) {
super.paint(g);
// define the position
int locX = 200;
int locY = 200;
// draw a line (there is no drawPoint..)
g.drawLine(locX, locY, locX, locY);
}
public static void main(String[] args) {
Test test = new Test();
}
}
你也可以使用update或paintComponents方法,這會更好。但是,你必須確保它被調用。如果你有問題,它不會被調用,你可以採用如下方案:Why is paint()/paintComponent() never called?
1
簡單性和實用性之間的最佳平衡點很可能將是擴展JPanel,並重寫paintComponent(圖形)。然後把該面板在您的JFrame(用適當的佈局這裏有一些使用說明:。http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JComponent.html#paintComponent%28java.awt.Graphics%29
1
看到
void update(Graphics g)
JFrame類的方法 圖形API(像畫點,畫線,畫弧等)都在圖形類
編輯:http://www.javadb.com/drawing-a-line-using-java-2d-graphics-api
0
問問自己,如果你真的想延長JFrame
或JPanel
。如果您決定不這樣做,那麼您可以創建一個基本的JComponent
。取決於您使用的佈局管理器,您可能會取得不同的成功。
public class PixelComponent extends JComponent
{
private Color color;
public PixelComponent(Color color)
{
super();
this.color = color;
}
public PixelComponent()
{
this(Color.BLACK);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(color);
g.fillRect(0, 0, 1, 1);
}
}
0
發送圖形和參考軸x和y使一個像素:
private void doPixel(Graphics g, int x, int y){ g.fillRect(x, y, 1, 1); }
相關問題
- 1. 確定在Java中旋轉矩形內的鼠標點擊(AWT,擺動)
- 2. JAVA製作時間擺動計時器
- 3. 在Java AWT中繪製單個像素的最快方法圖形
- 4. Java ProgressBar在擺動
- 5. Java,Swing,Awt - 在圖像上繪製矩形並使其可見
- 6. 在框架中添加多個擺動/ awt形狀
- 7. 如何在Java擺動
- 8. Java在搖擺中採樣像素顏色
- 9. 擺動計時器不工作(Java)
- 10. 搖擺/「選擇列表」的實施AWT
- 11. 如何在android中擺動像鐘擺一樣的圖像?
- 12. Java Table - AWT
- 13. 如何在Java繪製中使用擺動的選定區域執行鼠標點擊動作
- 14. 製作Button類(沒有用擺動)
- 15. Java swing/awt組件繪製棋盤
- 16. 使用AWT繪製Java圖形
- 17. java - AWT fillOval不繪製新對象
- 18. 擺動組件和awt事件的問題
- 19. 在awt中顯示圖像
- 20. 在awt中添加圖像
- 21. Java - 二進制圖像像素索引
- 22. 鉻像可摺疊的搜索欄(在Java中 - 擺動)
- 23. 擺動定製組件dummylistener
- 24. 如何在Matlab中繪製彈簧擺的彈簧擺的擺動運動
- 25. 擺動焦點問題
- 26. 擺動瓷磚圖像
- 27. 使用擺動在圖像上繪製形狀
- 28. Java - 顯示擺動GUI
- 29. 的Java /擺動:JSlider.setValue阻止
- 30. 在擺動中顯示圖像
整齊的小例子感謝。我不得不添加進口來運行:import javax.swing。*; import java.awt.Graphics; import java.awt.Dimension; – strainer 2010-11-18 17:57:36
謝謝,你知道如何設置線/點/點的顏色嗎? :) – 2010-11-18 22:31:40
如何做雙打而不是整數? – 2018-01-14 10:28:10