簡體   English   中英

為什么我不能在jpanel上創建形狀?

[英]Why can't I create a shape on jpanel?

我正在使用套接字編程在Java gui上工作。我想使用從服務器發送的參數在jframe上創建jpanel,並在jpanel中創建隨機形狀。 我使用此資源來繪制形狀:

https://github.com/AugustBrenner/Random-Draw-Shape/blob/master/DrawPanel.java

我在jframe中的代碼是;

 public void starteGame(String received) {
    gamers.setText(received);



    String[] mParsed = received.split(" ");
    boolean filled = true;
    int width = Integer.parseInt(mParsed[2]);
    int height = Integer.parseInt(mParsed[1]);
    int x1 = Integer.parseInt(mParsed[3]);
    int y1 = Integer.parseInt(mParsed[4]);
    int x2 = Integer.parseInt(mParsed[5]);
    int y2 = Integer.parseInt(mParsed[6]);
    int randomShape = Integer.parseInt(mParsed[7]);
    Color firstColor = new Color(Integer.parseInt(mParsed[8]), true);
    Color secondColor = new Color(Integer.parseInt(mParsed[9]), true);
    boolean cyclic = Boolean.parseBoolean(mParsed[10]);
    switch (randomShape) {
        case 1:
            // add the line to the list of lines to be displayed
            shape = new MyPolygon(x1, y1, x2, y2, firstColor, filled);
            break;
        case 2:
            shape = new MyRectangle(x1, y1, x2, y2, firstColor, filled);
            break;
        case 3:
            shape = new MyOval(x1, y1, x2, y2, firstColor, filled);
            break;
    }

 jPanel2=new PanelDraw(width,height,x1,y1,x2,y2,firstColor,secondColor,
randomShape,shape,cyclic);

}

And my PanelDraw is;
public class PanelDraw extends JPanel implements MouseMotionListener {

private Random randomNumbers;
private MyShape shape;
int x1;
int y1;
int x2;
int y2;
Color firstColor;
Color secondColor;
int width;
int height;
// generate random shape
int randomShape;
 boolean cyclic ;
// constructor, creates a panel with random shapes
public PanelDraw(int width, int height, int x1, int y1, int x2, int y2, 
Color first, Color second, int randomS,MyShape shape ,boolean cyclic) {
    this.width = width;
    this.height = height;
    // generate random coordinates      
    this.x1 = x1;
    this.y1 = y1;
    this.x2 = x2;
    this.y2 = y2;
    // generate a random color
    this.firstColor = first;
    this.secondColor = second;
    // generate random shape
    randomShape = randomS;
    this.shape=shape;
    this.cyclic=cyclic;
    //setBackground( Color.BLACK ); 
} // end DrawPanel constructor

   public void mouseMoved(MouseEvent event) {

}// end mouseMoved

public void mouseDragged(MouseEvent event) {
} // end method

// for each shape array, draw the individual shapes
   @Override
   public void paintComponent(Graphics g) {

    //initialize filled boolean to true;
    boolean filled = true;

    // initialize random cyclic boolean


    // draw the shape
    Graphics2D g2d = (Graphics2D) g; // cast g to Graphics2D
    g2d.setPaint(new GradientPaint(x1, y1, firstColor, x2, y2,
            secondColor, cyclic));
    shape.draw(g2d);
    g2d.dispose();
   // shapeCount++;

    try {
        Thread.sleep(700);
    } catch (Exception e) {
    }

    repaint();

} // end method paintComponent

} // end class DrawPanel

當我的函數正在運行且隨機形狀不出現時,面板不會出現。 你能幫助我嗎?

Thread.sleep(700);

永遠不要在繪畫方法中使用Thread.sleep()。

這將導致事件調度線程進入睡眠狀態,這意味着GUI無法重繪自身或響應事件。

也:

  1. 切勿在繪畫方法中調用reapint()。 如果需要動畫,請使用Swing計時器。

  2. paintCoponent()方法中的第一條語句應為super.paintComponent(g) ,以確保在執行自定義繪畫之前清除面板的背景。

問題#1 ...

try {
    Thread.sleep(700);
} catch (Exception e) {
}

paintComponent方法內部:/

Swing是單線程的(並且不是線程安全的),在paintComponent執行此操作將防止對所有內容進行繪制,直到返回sleep之后,它還將停止所有用戶交互。

有關更多詳細信息,請參見Swing中的並發。

問題#1.1 ...

Graphics2D g2d = (Graphics2D) g; // cast g to Graphics2D
//...
g2d.dispose();

傳遞給您的Graphics上下文是由系統創建的,並且在繪制過程中被繪制的所有組件之間共享。 這樣處理可能會導致其他組件被塗漆

問題#1.2 ...

repaint();

paintComponent方法中。

請勿直接或間接更改噴塗過程中任何組件的狀態。 繪畫應該畫狀態,別無其他。 這樣做將導致重新繪制管理器運行異常並消耗所有CPU周期

問題#2 ...

jPanel2=new PanelDraw(width,height,x1,y1,x2,y2,firstColor,secondColor, randomShape,shape,cyclic);

是的,但是您沒有將組件添加到任何東西中,因此應該如何繪畫?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM