[英]paintComponent() and JColorChooser
I am trying to write a program for class and I am stuck. 我正在尝试为课程写一个程序而且我被卡住了。 I need to create a shape (my initial) and then fill it with it JColorChooser. 我需要创建一个形状(我的初始),然后用它填充它JColorChooser。 I have been able to create my initial using paint, and graphics. 我已经能够使用绘画和图形创建我的初始。 Then I have been able to fill my shape by using g.setColor(new Color(11, 139, 198)); 然后我可以通过使用g.setColor(new Color(11,139,198))填充我的形状; I don't know how to add a JColorChooser to this. 我不知道如何添加JColorChooser。 This is my first post so let me know whatever I can do to make it easier to read. 这是我的第一篇文章,所以让我知道我能做些什么来让它更容易阅读。 Here is my code: 这是我的代码:
/* Program - Letter
* Program Desc -
* Programmer - Bradon Fredrickson
* Class -
* Created - Oct 1, 2014
*/
package letter;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Letter
{
public static void PlainLetter()
{
/*
* Creating GUI for letter
*/
LetterDraw plainLetter = new LetterDraw();
JFrame logo = new JFrame("My Logos");
JLabel plain = new JLabel("Plain Letter");
plain.setLocation(10, 0);
plain.add(plainLetter);
Container pane = logo.getContentPane();
pane.setLayout(new GridLayout(1, 1));
logo.setLocationRelativeTo(null); // Center the frame
logo.add(plainLetter);
logo.setVisible(true);
logo.setSize(400, 200);
logo.setLocation(400, 200);
logo.setVisible(true);
logo.setDefaultCloseOperation(logo.EXIT_ON_CLOSE);
}//end plain letter
public static void main(String[] args)
{
PlainLetter();
new ColorChooser();
}//end main method
}//end Letter
Second class - this is drawing my object 第二课 - 这是绘制我的对象
/* Program - ThirdLetter
* Program Desc -
* Programmer - Bradon Fredrickson
* Class -
* Created - Oct 6, 2014
*/
package letter;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Path2D;
import javax.swing.JPanel;
public class LetterDraw extends JPanel
{
/*
* Creating my shape/letter
*/
public void paint(Graphics graphics)
{
Path2D.Double path = new Path2D.Double();
Graphics2D g = (Graphics2D) graphics;
path.moveTo(17, 63);
/*
* Top Horizontal Line
*/
path.curveTo(21, 60, 21, 43, 17, 37); //left vert
path.curveTo(30, 43, 100, 43, 120, 37); //top horiz
path.curveTo(115, 45, 115, 57, 117, 62); //right vert
path.curveTo(105, 53, 60, 57, 17, 63); //bottom horiz right
/*
* Bottom Horizontal Line
*/
path.moveTo(32, 97);
path.curveTo(37, 85, 35, 79, 35, 77); //left vert
path.curveTo(45, 79, 45, 79, 100, 79); //top left horiz
path.curveTo(96, 85, 98, 92, 98, 98);//right vert
path.curveTo(89, 93, 79, 94, 32, 97);//bottom right horiz
/*
* Vertical Line
*/
path.moveTo(40, 130);
path.curveTo(48, 110, 46, 70, 37, 55); //left vert
path.curveTo(62, 55, 62, 55, 60, 55); //top horiz
path.curveTo(68, 80, 68, 100, 63, 130); //right vert
path.curveTo(60, 127, 50, 127, 40, 130);//bottom horiz
g.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setStroke(new BasicStroke(3));
//g.setStroke(new BasicStroke(4, BasicStroke.JOIN_BEVEL, 0));
g.draw(path);
g.setColor(new Color(11, 139, 198));
g.fill(path);
}//end paint
}//end LetterDraw
third - this is my colorChooser 第三 - 这是我的colorChooser
/* Program - ColorChooser
* Program Desc -
* Programmer - Bradon Fredrickson
* Class -
* Created - Oct 10, 2014
*/
package letter;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.colorchooser.ColorSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class ColorChooser
{
/**
* Creating the color chooser
*/
public ColorChooser()
{
JFrame frame = new JFrame("JColorChooser Popup");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JLabel label = new JLabel("www.java2s.com", JLabel.CENTER);
label.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48));
frame.add(label, BorderLayout.NORTH);
final JColorChooser colorChooser = new JColorChooser(label.
getBackground());
ColorSelectionModel model = colorChooser.getSelectionModel();
ChangeListener changeListener = new ChangeListener()
{
public void stateChanged(ChangeEvent changeEvent)
{
Color newForegroundColor = colorChooser.getColor();
label.setForeground(newForegroundColor);
}
};
model.addChangeListener(changeListener);
frame.add(colorChooser, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}//end colorChooser
}//end colorChooser class
Right now i get to 2 JFrames 1 for my shape and 1 for the JColorChooser. 现在我得到2个JFrames 1用于我的形状,1个用于JColorChooser。 I would like to put them all in 1 frame also. 我想把它们全部放在1帧中。
In your LetterDraw
class, instead of doing 在你的LetterDraw
课程中,而不是做
g.setColor(new Color(11, 139, 198));
better to have a class member Color color
, and have a setter for it. 最好有一个类成员Color color
,并有一个setter。 Then do just do this 那就去做吧
public class LetterDraw extends JPanel {
private Color color = Color.BLUE; // default;
public void setColor(Color color) {
this.color = color;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
...
g.setColor(color);
}
}
Notice the paintComponent
and super.paintComponent
. 注意paintComponent
和super.paintComponent
。 See link from @trashgod in comments. 请参阅评论中@trashgod的链接。
As for the color selection, JColorChooser
has a static method, from which you can call to show a color chooser dialog and obtain a returned color from. 至于颜色选择, JColorChooser
有一个静态方法,您可以从中调用以显示颜色选择器对话框并从中获取返回的颜色。 You can then use that color to call setColor
on the LetterDraw
. 然后,您可以使用该颜色在LetterDraw
上调用setColor
。 No need to create your own frame. 无需创建自己的框架。 For instance. 例如。
Color color = JColorChooser.showDialog(null, "title", Color.BLUE);
letterDraw.setColor(color);
If you want to keep the color chooser frame open though and have the , using your current code, You would need to either use some kind of MVC/Observer design, or pass the instance of LetterDraw
to the ColorChooser
constructor, so that it can call LetterDraw's
set color method, after it changes color. 如果你想保持颜色选择框打开,但并拥有,使用当前的代码,你会需要或者使用某种MVC / Observer设计的,或者的实例传递LetterDraw
到ColorChooser
构造函数,以便它可以调用LetterDraw's
设置颜色方法,在它改变颜色后。
"I would like to put them all in 1 frame also" “我想把它们全部放在1帧中”
I just noticed that part of the question. 我只是注意到了问题的一部分。
You could forget that ColorChooser
class all together and create the JColorChooser
in the Letter
class. 你可以忘记ColorChooser
类,并在Letter
类中创建JColorChooser
。 So you have access to the LetterDraw
object. 因此您可以访问LetterDraw
对象。 Here's how a refactor might look 以下是重构的外观
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import java.awt.geom.Path2D;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Letter {
LetterDraw letterDraw = new LetterDraw();
public Letter() {
JFrame frame = new JFrame();
JPanel letterDrawWrapper = new JPanel(new GridBagLayout());
letterDrawWrapper.add(letterDraw);
frame.add(letterDrawWrapper);
frame.add(createColorChooser(), BorderLayout.PAGE_END);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JColorChooser createColorChooser() {
JColorChooser colorChooser = new JColorChooser();
colorChooser.getSelectionModel().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
letterDraw.setColor(colorChooser.getColor());
}
});
return colorChooser;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Letter();
}
});
}
}
class LetterDraw extends JPanel {
private Color color;
public void setColor(Color color) {
this.color = color;
repaint();
}
@Override
protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Path2D.Double path = new Path2D.Double();
Graphics2D g = (Graphics2D) graphics;
path.moveTo(17, 63);
/*
* Top Horizontal Line
*/
path.curveTo(21, 60, 21, 43, 17, 37); //left vert
path.curveTo(30, 43, 100, 43, 120, 37); //top horiz
path.curveTo(115, 45, 115, 57, 117, 62); //right vert
path.curveTo(105, 53, 60, 57, 17, 63); //bottom horiz right
/*
* Bottom Horizontal Line
*/
path.moveTo(32, 97);
path.curveTo(37, 85, 35, 79, 35, 77); //left vert
path.curveTo(45, 79, 45, 79, 100, 79); //top left horiz
path.curveTo(96, 85, 98, 92, 98, 98);//right vert
path.curveTo(89, 93, 79, 94, 32, 97);//bottom right horiz
/*
* Vertical Line
*/
path.moveTo(40, 130);
path.curveTo(48, 110, 46, 70, 37, 55); //left vert
path.curveTo(62, 55, 62, 55, 60, 55); //top horiz
path.curveTo(68, 80, 68, 100, 63, 130); //right vert
path.curveTo(60, 127, 50, 127, 40, 130);//bottom horiz
g.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setStroke(new BasicStroke(3));
//g.setStroke(new BasicStroke(4, BasicStroke.JOIN_BEVEL, 0));
g.draw(path);
g.setColor(color);
g.fill(path);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(150, 150);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.