为什么我不能在jpanel上创建一个形状?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么我不能在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
当我的功能运行并且没有出现随机形状时,面板不会出现。你能帮助我吗?
答案
Problem #1...
try {
Thread.sleep(700);
} catch (Exception e) {
}
在你的paintComponent
方法内:/
Swing是单线程的(而不是线程安全的),在你的paintComponent
中执行此操作将阻止绘制所有内容,直到sleep
返回之后,它还将阻止所有用户交互发生。
有关详细信息,请参阅Concurrency in Swing
问题#1.1 ......
Graphics2D g2d = (Graphics2D) g; // cast g to Graphics2D
//...
g2d.dispose();
传递给您的Graphics
上下文由系统创建,并在绘制过程中绘制的所有组件之间共享。像这样处理它会导致其他部件被涂漆
问题#1.2 ......
repaint();
在你的paintComponent
方法里面。
不要直接或间接地改变油漆通道中任何组件的状态。绘画应该描绘状态而不是其他。执行此操作将导致重绘管理器运行并消耗所有CPU周期
Problem #2....
jPanel2=new PanelDraw(width,height,x1,y1,x2,y2,firstColor,secondColor, randomShape,shape,cyclic);
是的,但你没有将组件添加到任何东西,那么它是如何被绘制的?
另一答案
Thread.sleep(700);
不要在绘画方法中使用Thread.sleep()。
这将导致事件调度线程进入休眠状态,这意味着GUI无法重新绘制自身或响应事件。
也:
- 永远不要在绘画方法中调用reapint()。如果你需要动画,可以使用Swing Timer。
- paintComponent()方法中的第一个语句应该是
super.paintComponent(g)
,以确保在执行自定义绘制之前清除面板的背景。
以上是关于为什么我不能在jpanel上创建一个形状?的主要内容,如果未能解决你的问题,请参考以下文章