我正在尝试为上课编写程序,但遇到问题。我需要创建一个形状(我的初始名称),然后用JColorChooser填充它。我已经能够使用绘画和图形来创建自己的名字了。然后,我可以使用g.setColor(new Color(11,139,198));来填充形状。我不知道如何为此添加一个JColorChooser。这是我的第一篇文章,所以让我知道我能做些什么来使其更易于阅读。
这是我的代码:
/* 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
二等-这是我的对象
/* 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
第三-这是我的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
现在,我的形状为2个JFrames,JColorChooser为1个。我也想将它们全部放在1帧中。
最佳答案
在您的LetterDraw
类中,而不是
g.setColor(new Color(11, 139, 198));
最好有一个类成员
Color color
,并为其设置一个设置器。然后做这个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);
}
}
注意
paintComponent
和super.paintComponent
。请参阅@trashgod中的评论链接。至于颜色选择,
JColorChooser
有一个静态方法,您可以从中调用以显示颜色选择器对话框并从中获取返回的颜色。然后,您可以使用该颜色在setColor
上调用LetterDraw
。无需创建自己的框架。例如。Color color = JColorChooser.showDialog(null, "title", Color.BLUE);
letterDraw.setColor(color);
如果您想保持颜色选择器框架保持打开状态,并使用当前代码使用,则需要使用某种MVC / Observer设计,或者将
LetterDraw
的实例传递给ColorChooser
构造函数,因此更改颜色后,可以调用LetterDraw's
设置颜色方法。“我也想将它们全部放入一帧中”
我只是注意到了那部分问题。
您可能会忘记了
ColorChooser
类,并在JColorChooser
类中创建了Letter
。因此,您可以访问LetterDraw
对象。这是重构的外观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);
}
}
关于java - paintComponent()和JColorChooser,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26301007/