单击jButton时如何更改单个椭圆的颜色? [英] How to change the color of an individual ellipse when clicking a jButton?
问题描述
我希望能够单击JButton并将下一个单独的椭圆更改为其各自的颜色,即默认的蓝色为红色或黑色.到目前为止,我已经能够更改我制作的所有椭圆的颜色.
I want to be able to click the JButton and change the next individual ellipse I make to its respective color, Red or Black, from its default Blue. So far I'm able to change the color of all the ellipses I have made.
public class MainClient {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainClient window = new MainClient();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainClient() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final SpriteField panel = new SpriteField();
panel.setBounds(0, 110, 782, 331);
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int tX = e.getX();
int tY = e.getY();
panel.CreateObjectAt(tX,tY);
}
});
frame.getContentPane().setLayout(null);
panel.setBackground(Color.LIGHT_GRAY);
frame.getContentPane().add(panel);
final JButton btnRed = new JButton("black");
btnRed.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
panel.setColor(Color.BLACK);
}
});
btnRed.setBounds(229, 500, 97, 25);
frame.getContentPane().add(btnRed);
JButton btnRed_1 = new JButton("red");
btnRed_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
panel.setColor(Color.RED);
}
});
btnRed_1.setBounds(342, 500, 97, 25);
frame.getContentPane().add(btnRed_1);
}
}
我有一个SriteField类
I have a SriteField Class
public class SpriteField extends JPanel
{
ArrayList<RoundSprite>mSprites = new ArrayList<RoundSprite>();
private Color color = Color.BLUE;
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
AffineTransform tOldTransform = g2.getTransform();
for(RoundSprite tOne:mSprites)
{
tOne.DrawSprite(g2, color);
}
g2.setTransform(tOldTransform);
}
public void CreateObjectAt(int tX, int tY)
{
//make sprite
RoundSprite mLonely = new RoundSprite();
//set position
mLonely.SetPosition(tX, tY);
//add to array
mSprites.add(mLonely);
repaint();
}
public void setColor(Color color)
{
this.color = color;
repaint();
}
}
我有一个RoundSprite类
I have a RoundSprite Class
public class RoundSprite
{
int mX;
int mY;
private Color color;
void DrawSprite(Graphics2D g2, Color color)
{
AffineTransform tOldTransform = g2.getTransform();
g2.setColor(color);
g2.translate(mX, mY); //got it out from the corner
g2.draw(new Ellipse2D.Double(-15, -22, 30, 50));
g2.setTransform(tOldTransform);
}
public void SetPosition(int tX, int tY) //g2.translate
{
mX = tX;
mY = tY;
}
推荐答案
存在三个核心问题...
There are three core issues...
为了使panel_1
MouseListener
更改panel
中子画面的颜色,MouseListener
需要引用panel
.
In order for the panel_1
MouseListener
to change the color of the sprites within panel
, the MouseListener
requires a reference to the panel
.
您可以在initialize
方法中将panel
声明为final
,但是我更喜欢将panel
设置为MainClient
的private
实例字段.
You could declare the panel
as final
within the initialize
method, but I prefer to make the panel
a private
instance field of the MainClient
.
为了更改颜色,SpriteField
需要提供一种使感兴趣的方可以请求更改的方法.
In order to change the color, the SpriteField
needs to provide a means by which interested parties can request a change.
这将要求SpriteField
提供某种相关方可以在需要时调用的方法,例如setColor
方法...
This is going to require SpriteField
to provide some kind of method that interested parties can call when they need to, for example, a setColor
method...
public class SpriteField extends JPanel {
private Color color = Color.BLACK;
//...
public void setColor(Color color) {
this.color = color;
repaint();
}
然后panel_1
的MouseListener
现在可以引用SpriteField
并调用可以产生更改的方法...
Then panel_1
's MouseListener
can now reference the SpriteField
and call a method which can produce a change...
panel_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Click");
panel.setColor(Color.BLUE);
}
});
三个
您需要某种方式来告诉子画面它应该是什么颜色...
Three
You need some way to tell the sprite what color it should be...
这取决于您要实现的目标...
This one depends on what it is you want to achieve...
如果要为所有子图形绘制相同的颜色,则需要某种方法来告诉子图形它们应使用的颜色,例如,可以更改DrawSprite
方法以接受Color
参数.
If you want to paint ALL the sprites the same color, then you need some way to tell the sprites which color they should use, for example, you could change the DrawSprite
method to accept a Color
parameter...
public class RoundSprite {
//...
void DrawSprite(Graphics2D g2, Color color) {
在SpriteField
paintComponent
内,将颜色传递给精灵
And within the SpriteField
paintComponent
, pass the color to the sprite
public class SpriteField extends JPanel {
//...
private Color color = Color.BLACK;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
AffineTransform tOldTransform = g2.getTransform();
for (RoundSprite tOne : mSprites) {
tOne.DrawSprite(g2, color);
}
g2.setTransform(tOldTransform);
}
现在,这将以相同的颜色绘制场中的所有精灵,如果要更改单个精灵的颜色,这将变得更加复杂...
Now, this will paint ALL the sprites within the field the same color, if you want to change the color of individual sprites, this comes infinitely more complicated...
暂时不要选择选择精灵的问题...
Lets leave aside the issues of selecting a sprite for the moment...
基本上,您需要将 Two 点的相同原理应用于RoundSprite
类.
Basically, you would need to apply the same principle from point Two to the RoundSprite
class.
您需要提供一种可以set/get
所需颜色的方法.您将需要将此值存储在RoundSprite
类的实例字段中,并在调用DrawSprite
时,将该Color
应用于Graphics
上下文.
You would need to supply a method that could set/get
the desired color. You would need to store this value in an instance field of the RoundSprite
class and when DrawSprite
is called, apply that Color
to the Graphics
context.
这意味着SpriteField
不需要执行任何颜色管理,除了将对更改的请求从调用者传递到选定的sprite之外,因此它仍然需要setColor
方法...
This would mean that SpriteField
won't need to perform any color management, beyond passing the request for change from a caller to the selected sprite, so it would still need a setColor
method...
您可能希望通读 Java TM编程语言的代码约定,这将使人们更容易阅读您的代码,并让您阅读其他人
You might like to have a read through Code Conventions for the Java TM Programming Language, it will make it easier for people to read your code and for you to read others
避免使用null
布局,像素完美布局是现代ui设计中的一种幻觉.有太多因素会影响组件的单个大小,您无法控制. Swing旨在与布局管理者为核心一起工作,舍弃这些问题不会导致任何问题,而您将花费越来越多的时间进行纠正
Avoid using null
layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify
哦,您意识到已经有可能应该使用JButton
类,而不是带有MouseListener
.....?
Oh, and you realise that there already is a JButton
class that probably should be used instead of a JPanel
with a MouseListener
....?
这篇关于单击jButton时如何更改单个椭圆的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!