Java 绘制程序颜色不保留颜色
我真的被卡住了。我似乎不明白为什么我的Java 绘制程序颜色不保留颜色,java,swing,Java,Swing,我真的被卡住了。我似乎不明白为什么我的paintComponent以前的颜色一直在改变。我相信问题出在我的arraylist,但我不知道如何更改arraylist,以便它可以接受不同的颜色 public class Drawing extends JPanel { private JRadioButton red, yellow, blue, erase; private JButton button; private Color c; private Array
paintComponent
以前的颜色一直在改变。我相信问题出在我的arraylist
,但我不知道如何更改arraylist
,以便它可以接受不同的颜色
public class Drawing extends JPanel {
private JRadioButton red, yellow, blue, erase;
private JButton button;
private Color c;
private ArrayList<Point> pointList;
private int counter = 0;
private boolean isDrawingMode = true;
private boolean eraser, isBlue;
private HashMap test;
public Drawing() {
setLayout(new FlowLayout());
setBackground(Color.white);
JPanel pane = new JPanel();
pointList = new ArrayList<Point>();
addMouseListener(new MouseTrackerListener());
red = new JRadioButton("Red");
yellow = new JRadioButton("Yellow");
blue = new JRadioButton("Blue");
erase = new JRadioButton("Erase");
button = new JButton("Clear Drawing");
red.addActionListener(new ColorActionListener());
yellow.addActionListener(new ColorActionListener());
blue.addActionListener(new ColorActionListener());
erase.addActionListener(new ColorActionListener());
button.addActionListener(new ColorActionListener());
pane.add(red);
pane.add(yellow);
pane.add(blue);
pane.add(erase);
pane.add(button);
add(pane);
pane.setBackground(Color.gray);
ButtonGroup group = new ButtonGroup();
group.add(red);
group.add(yellow);
group.add(blue);
group.add(erase);
setPreferredSize(new Dimension(500, 500));
}
public void paintComponent(Graphics pen) {
super.paintComponent(pen);
pen.setColor(Color.red);
pen.setColor(c);
for (int i = 0; i < pointList.size(); i++) {
Point p = pointList.get(i);
pen.fillOval(p.x, p.y, 10, 10);
if (eraser = true) {
p = pointList.get(i);
pen.drawOval(p.x, p.y, 10, 10);
}
}
}
private class MouseTrackerListener extends MouseInputAdapter {
public void mouseClicked(MouseEvent e) {
counter++;
if (counter % 2 != 0) {
addMouseMotionListener(new MouseTrackerListener());
isDrawingMode = true;
} else {
isDrawingMode = false;
}
}
public void mouseMoved(MouseEvent e) {
if (isDrawingMode) {
Point point = e.getPoint();
pointList.add(point);
repaint();
}
}
}
private class ColorActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == red) {
c = Color.red;
} else if (e.getSource() == yellow) {
c = Color.yellow;
} else if (e.getSource() == blue) {
isBlue=true;
c = Color.blue;
} else if (e.getSource() == erase) {
eraser = true;
c=Color.white;
} else if (e.getSource() == button) {
pointList.clear();
repaint();
}
}
}
public static void main(String[] args) {
Drawing draw = new Drawing();
JFrame frame = new JFrame("Draw Something!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(draw);
frame.setSize(450, 450);
frame.setVisible(true);
}
}
公共类绘图扩展了JPanel{
私人JRadioButton红色、黄色、蓝色、擦除;
私人按钮;
私有颜色c;
私有ArrayList点列表;
专用整数计数器=0;
私有布尔值isDrawingMode=true;
专用布尔橡皮擦,蓝色;
私有HashMap测试;
公众绘图(){
setLayout(新的FlowLayout());
挫折地面(颜色:白色);
JPanel窗格=新的JPanel();
pointList=新的ArrayList();
addMouseListener(新的MouseTrackerListener());
红色=新的JRadioButton(“红色”);
黄色=新的JRadioButton(“黄色”);
蓝色=新的JRadioButton(“蓝色”);
擦除=新的JRadioButton(“擦除”);
按钮=新按钮(“清除图纸”);
red.addActionListener(新的ColorActionListener());
黄色.addActionListener(新的ColorActionListener());
blue.addActionListener(新的ColorActionListener());
erase.addActionListener(新的ColorActionListener());
addActionListener(新的ColorActionListener());
窗格。添加(红色);
窗格。添加(黄色);
窗格。添加(蓝色);
窗格。添加(删除);
窗格。添加(按钮);
添加(窗格);
窗格。背景(颜色。灰色);
ButtonGroup=新建ButtonGroup();
组。添加(红色);
添加组(黄色);
添加组(蓝色);
组。添加(删除);
setPreferredSize(新尺寸(500500));
}
公共组件(图形笔){
超级油漆组件(pen);
钢笔颜色(颜色:红色);
pen.setColor(c);
对于(int i=0;i
创建一个自定义类,该类可以包含有关要绘制的内容以及如何绘制的信息(颜色和形状)
使用此选项,而不是存储点
public void mouseMoved(MouseEvent e) {
if (isDrawingMode) {
Point point = e.getPoint();
pointList.add(new Dot(point, c));
repaint();
}
}
(您需要更新点列表
以接受点
,而不是点
)
然后简单地画你的“点”
@覆盖
受保护的组件(图形笔){
超级油漆组件(pen);
对于(int i=0;i
示例
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.event.MouseInputAdapter;
public class Drawing extends JPanel {
private JRadioButton red, yellow, blue, erase;
private JButton button;
private Color c;
private ArrayList<Dot> pointList;
private int counter = 0;
private boolean isDrawingMode = true;
private boolean eraser, isBlue;
private HashMap test;
public Drawing() {
setLayout(new FlowLayout());
setBackground(Color.white);
JPanel pane = new JPanel();
pointList = new ArrayList<Dot>();
addMouseListener(new MouseTrackerListener());
red = new JRadioButton("Red");
yellow = new JRadioButton("Yellow");
blue = new JRadioButton("Blue");
erase = new JRadioButton("Erase");
button = new JButton("Clear Drawing");
red.addActionListener(new ColorActionListener());
yellow.addActionListener(new ColorActionListener());
blue.addActionListener(new ColorActionListener());
erase.addActionListener(new ColorActionListener());
button.addActionListener(new ColorActionListener());
pane.add(red);
pane.add(yellow);
pane.add(blue);
pane.add(erase);
pane.add(button);
add(pane);
pane.setBackground(Color.gray);
ButtonGroup group = new ButtonGroup();
group.add(red);
group.add(yellow);
group.add(blue);
group.add(erase);
setPreferredSize(new Dimension(500, 500));
}
public void paintComponent(Graphics pen) {
super.paintComponent(pen);
for (int i = 0; i < pointList.size(); i++) {
Dot p = pointList.get(i);
p.draw(pen);
}
}
private class MouseTrackerListener extends MouseInputAdapter {
public void mouseClicked(MouseEvent e) {
counter++;
if (counter % 2 != 0) {
addMouseMotionListener(new MouseTrackerListener());
isDrawingMode = true;
} else {
isDrawingMode = false;
}
}
public void mouseMoved(MouseEvent e) {
if (isDrawingMode) {
Point point = e.getPoint();
pointList.add(new Dot(point, c));
repaint();
}
}
}
private class ColorActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == red) {
c = Color.red;
} else if (e.getSource() == yellow) {
c = Color.yellow;
} else if (e.getSource() == blue) {
c = Color.blue;
} else if (e.getSource() == erase) {
eraser = true;
c = Color.white;
} else if (e.getSource() == button) {
pointList.clear();
}
repaint();
}
}
public class Dot {
private Point p;
private Color color;
public Dot(Point p, Color color) {
this.p = p;
this.color = color;
}
public void draw(Graphics g) {
g.setColor(color);
g.drawOval(p.x, p.y, 10, 10);
}
}
public static void main(String[] args) {
Drawing draw = new Drawing();
JFrame frame = new JFrame("Draw Something!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(draw);
frame.setSize(450, 450);
frame.setVisible(true);
}
}
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.FlowLayout;
导入java.awt.Graphics;
导入java.awt.Point;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.MouseEvent;
导入java.util.ArrayList;
导入java.util.HashMap;
导入javax.swing.ButtonGroup;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JRadioButton;
导入javax.swing.event.MouseInputAdapter;
公共类绘图扩展了JPanel{
私人JRadioButton红色、黄色、蓝色、擦除;
私人按钮;
私有颜色c;
私有ArrayList点列表;
专用整数计数器=0;
私有布尔值isDrawingMode=true;
专用布尔橡皮擦,蓝色;
私有HashMap测试;
公众绘图(){
setLayout(新的FlowLayout());
挫折地面(颜色:白色);
JPanel窗格=新的JPanel();
pointList=新的ArrayList();
addMouseListener(新的MouseTrackerListener());
红色=新的JRadioButton(“红色”);
黄色=新的JRadioButton(“黄色”);
蓝色=新的JRadioButton(“蓝色”);
擦除=新的JRadioButton(“擦除”);
按钮=新按钮(“清除图纸”);
red.addActionListener(新的ColorActionListener());
黄色.addActionListener(新的ColorActionListener());
blue.addActionListener(新的ColorActionListener());
erase.addActionListener(新的ColorActionListener());
addActionListener(新的ColorActionListener());
窗格。添加(红色);
窗格。添加(黄色);
窗格。添加(蓝色);
窗格。添加(删除);
窗格。添加(按钮);
添加(窗格);
@Override
protected void paintComponent(Graphics pen) {
super.paintComponent(pen);
for (int i = 0; i < pointList.size(); i++) {
Dot p = pointList.get(i);
p.draw(pen);
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.event.MouseInputAdapter;
public class Drawing extends JPanel {
private JRadioButton red, yellow, blue, erase;
private JButton button;
private Color c;
private ArrayList<Dot> pointList;
private int counter = 0;
private boolean isDrawingMode = true;
private boolean eraser, isBlue;
private HashMap test;
public Drawing() {
setLayout(new FlowLayout());
setBackground(Color.white);
JPanel pane = new JPanel();
pointList = new ArrayList<Dot>();
addMouseListener(new MouseTrackerListener());
red = new JRadioButton("Red");
yellow = new JRadioButton("Yellow");
blue = new JRadioButton("Blue");
erase = new JRadioButton("Erase");
button = new JButton("Clear Drawing");
red.addActionListener(new ColorActionListener());
yellow.addActionListener(new ColorActionListener());
blue.addActionListener(new ColorActionListener());
erase.addActionListener(new ColorActionListener());
button.addActionListener(new ColorActionListener());
pane.add(red);
pane.add(yellow);
pane.add(blue);
pane.add(erase);
pane.add(button);
add(pane);
pane.setBackground(Color.gray);
ButtonGroup group = new ButtonGroup();
group.add(red);
group.add(yellow);
group.add(blue);
group.add(erase);
setPreferredSize(new Dimension(500, 500));
}
public void paintComponent(Graphics pen) {
super.paintComponent(pen);
for (int i = 0; i < pointList.size(); i++) {
Dot p = pointList.get(i);
p.draw(pen);
}
}
private class MouseTrackerListener extends MouseInputAdapter {
public void mouseClicked(MouseEvent e) {
counter++;
if (counter % 2 != 0) {
addMouseMotionListener(new MouseTrackerListener());
isDrawingMode = true;
} else {
isDrawingMode = false;
}
}
public void mouseMoved(MouseEvent e) {
if (isDrawingMode) {
Point point = e.getPoint();
pointList.add(new Dot(point, c));
repaint();
}
}
}
private class ColorActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == red) {
c = Color.red;
} else if (e.getSource() == yellow) {
c = Color.yellow;
} else if (e.getSource() == blue) {
c = Color.blue;
} else if (e.getSource() == erase) {
eraser = true;
c = Color.white;
} else if (e.getSource() == button) {
pointList.clear();
}
repaint();
}
}
public class Dot {
private Point p;
private Color color;
public Dot(Point p, Color color) {
this.p = p;
this.color = color;
}
public void draw(Graphics g) {
g.setColor(color);
g.drawOval(p.x, p.y, 10, 10);
}
}
public static void main(String[] args) {
Drawing draw = new Drawing();
JFrame frame = new JFrame("Draw Something!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(draw);
frame.setSize(450, 450);
frame.setVisible(true);
}
}