简体   繁体   中英

Why can't I create a shape on jpanel?

I'm working on java gui with socket programming.I want to create jpanel on the jframe with the parameters I send from the server and create random shapes in jpanel. I used this resource to draw shapes:

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

my code in jframe is;

 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

the panel does not appear when my function is running and random shapes do not appear. Can you help me?

Thread.sleep(700);

Don't ever use a Thread.sleep() in a painting method.

This will cause the Event Dispatch Thread to sleep which means the GUI can't repaint itself or respond to events.

Also:

  1. Never invoke reapint() in a painting method. If you need animation use a Swing Timer.

  2. The first statement in the paintCoponent() method should be super.paintComponent(g) to make sure the background of the panel is cleared before you do your custom painting.

Problem #1...

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

inside your paintComponent method :/

Swing is single threaded (and not thread safe), doing this in your paintComponent will prevent EVERYTHING from been painted until AFTER sleep returns, it will also stop all user interaction from occurring.

See Concurrency in Swing for more details

Problem #1.1...

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

The Graphics context passed to you is created by the system and is shared between all components been painted during the paint pass. Disposing of it like this can cause other components from been painted

Problem #1.2...

repaint();

inside your paintComponent method.

Don't change the state, directly or indirectly, of any component from within a paint pass. Painting should paint state and nothing else. Doing this will cause the repaint manager to run wild and consume all the CPU cycles

Problem #2....

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

Yes, but you're not adding the component to anything, so how is it suppose to be painted?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM