gpt4 book ai didi

java - 如何画颜色

转载 作者:行者123 更新时间:2023-12-01 06:52:24 24 4
gpt4 key购买 nike

好的,我的主类有一些按钮,一个用于三角形,另一个用于一个

椭圆形和盒子。

我有一个 ColorChooser 按钮,我想单击它,ColorChooser 就会出现。我有

椭圆形、三角形和 ColorChooser 的类,我将它们每个都设置为一种模式

在我的主程序中。

这是我的主程序,只有框和 ColorChooser:

如您所见,我为每个按钮使用模式,并且为 ColorChooser 设置模式 4

如果你想让我添加盒子类或 ColorChooser 类,如果它没有,我会添加

意义。我只是不想再这样了。

这是我的主程序:

import java.awt.*;

import java.util.ArrayList;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.ChangeEvent;


public class Kaleidescope extends JFrame implements MouseListener, ActionListener,

MouseMotionListener

{

Box b;

ArrayList<Box> boxes; // list of boxes

ColorChooser oo;

ColorChooser[] colors;

int colorCount;

// Buttons

JButton boxButton;

JButton ColorButton;

int x1, y1; // mousePressed

int w1, z1; // mouseEntered

int mode =1; // 1 = line, 2= boxes, 3 = oval, 4= text, 5 = SG, twoLines = 7.

public static void main( String[] args )

{
System.out.println("hi there.");

new Kaleidescope();
}

public Kaleidescope()

{
setDefaultCloseOperation( EXIT_ON_CLOSE );

addMouseListener(this);

addMouseMotionListener(this);

boxes = new ArrayList<Box>();

colors = new ColorChooser[20];

colorCount = 0;

setLayout( new FlowLayout() );

boxButton = new JButton("Boxes");

add(boxButton);

boxButton.addActionListener( this );

ColorButton = new JButton("Color Chooser");

add(ColorButton);

ColorButton.addActionListener( this);

setSize( new Dimension(500,500) );

setVisible(true);

}

// returns a random color

public Color randomColor()

{

int red = (int)(Math.random()*255);

int green = (int)(Math.random()*255);

int blue = (int)(Math.random()*255);

return new Color(red,green,blue);

}

public void mouseClicked( MouseEvent e )

{

// box

if ( mode == 2)

{

boxes.add(new Box(e.getX(), e.getY(), randomColor()));
}


repaint();
}

//action performed

public void actionPerformed( ActionEvent e )

{

if ( e.getSource()==TriangleButton ) { mode = 1;}

else if ( e.getSource()==boxButton ) { mode = 2;}

else if ( e.getSource()==ovalButton) { mode = 3;}

else if ( e.getSource()==ColorButton) { mode = 4;}

//clear all

else if (e.getSource() == clearButton)

{
boxes.clear();
triangles.clear();
ovals.clear();

}

repaint();

}
public void mouseEntered( MouseEvent e ) { }

public void mousePressed( MouseEvent e ) { }

public void mouseExited( MouseEvent e ) { }

public void mouseReleased( MouseEvent e ) {}

public void mouseMoved( MouseEvent e ) {}

public void mouseDragged( MouseEvent e ){ }
}

public void paint( Graphics g )

{
//draw/paint box triangle and oval

super.paint(g);

for (Box box : boxes)
{
box.drawMe(g);
}
}
}

这是我的 colorChooser 类:

    import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.colorchooser.*;

public class ColorChooser extends JPanel implements ChangeListener

{

public static final long serialVersionUID = 1L;

public JColorChooser tcc;

public JLabel banner;

public ColorChooser()

{
super(new BorderLayout());

banner = new JLabel("",JLabel.CENTER);

banner.setOpaque(true);

banner.setPreferredSize(new Dimension(100, 65));

JPanel bannerPanel = new JPanel(new BorderLayout());

bannerPanel.add(banner, BorderLayout.CENTER);

bannerPanel.setBorder(BorderFactory.createTitledBorder("Banner"));

//Set up color chooser for setting text color

tcc = new JColorChooser(banner.getForeground());

tcc.getSelectionModel().addChangeListener(this);

tcc.setBorder(BorderFactory.createTitledBorder("Choose Text Color"));

add(bannerPanel, BorderLayout.CENTER);

add(tcc, BorderLayout.PAGE_END);
}

public void stateChanged(ChangeEvent e)

{
Color newColor = tcc.getColor();

banner.setForeground(newColor);
}


private static void createAndShowGUI()

{
//Create and set up the window.

JFrame frame = new JFrame("ColorChooserDemo");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.

JComponent newContentPane = new ColorChooser();

newContentPane.setOpaque(true); //content panes must be opaque

frame.setContentPane(newContentPane);

//Display the window.

frame.pack();

frame.setVisible(true);
}

public static void main(String[] args)

{
javax.swing.SwingUtilities.invokeLater(new Runnable()

{
public void run()

{
createAndShowGUI();
}
});
}

}

最佳答案

首先查看 How to Write an Action ListenerHow to Use Color Choosers

基本上,将 ActionListener 附加到您想要激活 JColorChooserJButton,并且当 actionPerformed 方法被调用,使用内置功能显示默认选择器窗口,例如(来自链接的教程)...

Color newColor = JColorChooser.showDialog(
ColorChooserDemo2.this,
"Choose Background Color",
banner.getBackground());

已更新

首先向 Kaleidescope 添加一个 Color 实例变量,这将允许您维护对最后选择的颜色的引用

private Color currentPaintColor = Color.BLACK;

接下来,当按下 ColorButton 时,您将需要创建某种对话框来显示选择器,这将允许您等到用户选择颜色并获取结果颜色...

} else if (e.getSource() == ColorButton) {
ColorChooser chooser = new ColorChooser();
int result = JOptionPane.showConfirmDialog(this, chooser, "Color Chooser", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
currentPaintColor = chooser.getChoosenColor();
}
} //clear all

您还需要更改 ColorChooser 中的 stateChanged 方法,使其更有意义...

public void stateChanged(ChangeEvent e) {
Color newColor = tcc.getColor();

banner.setBackground(newColor);
}

现在的问题是,您想用这种新颜色做什么?您想将其应用到您当前正在绘制的所有形状吗?如果是这样,您需要在绘制形状之前设置颜色...

public void paint(Graphics g) {
//draw/paint box triangle and oval
super.paint(g);
g.setColor(currentPaintColor);
for (Box box : boxes)
{
box.drawMe(g);
}
}

或者您只想将颜色应用于更改后添加的新对象?

您应该避免覆盖顶级容器的paint,原因很多,它们不是双缓冲的,这会在更新它们时导致闪烁,并且您将在其他所有内容的顶部进行绘制在框架上,您可以在框架边框下绘画...

相反,您应该使用 JPanel 并覆盖它的 paintComponent 方法,看看 Performing Custom Painting了解更多详情

关于java - 如何画颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21868856/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com