我想制作一个油漆程序。 当用户单击文本按钮时,用户单击画布,用户可以在单击的位置输入所需的文本。
我可以用什么来实现这一目标?
当我使用drawstring
时,我可以放置预定义文本和用户点击位置
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Font font = new Font("Serif", Font.PLAIN, 40);
g2.setFont(font);
g2.drawString("text", x, y);
如何将"text"
更改为用户从键盘输入的内容?
我的主要代码(主要用于查看)
public class MainPanel extends JPanel implements ActionListener, ChangeListener {
private JButton button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12, activeButton;
private ButtonGroup buttonGroup;
private DrawPanel drawPanel;
private boolean insertedDrawPanel;
private JSlider slider;
private JTextField valueField;
private int jSliderMin, jSliderMax, jSliderInit;
private Toolkit toolkit;
private Image cursorImage;
private Point cursorHotSpot;
private Cursor fillCursor, penCursor, eraserCursor, lineCursor, rectCursor, ovalCursor, triangleCursor, starCursor;
private Color color1, color2;
public MainPanel()
{
try {
javax.swing.UIManager.setLookAndFeel( javax.swing.UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(MainPanel.class.getName()).log(Level.SEVERE, null, ex);
}
setPreferredSize(new Dimension(1280, 720));
initiateComponent();
buildLayout();
registerListener();
checkDrawPanel();
}
public JPanel getDrawPanel() {
return drawPanel;
}
public void initiateComponent()
{
int width=54;
int height=48;
jSliderMin=1;
jSliderMax=100;
jSliderInit=1;
insertedDrawPanel=false;
BufferedImage buttonIcon;
activeButton= new JButton();
button1 = new JButton();
button1.setPreferredSize(new Dimension(width, height));
button1.setName("");
button2 = new JButton();
button2.setPreferredSize(new Dimension(width, height));
button2.setName("");
button3 = new JButton();
button3.setPreferredSize(new Dimension(width, height));
button4 = new JButton("");
button4.setPreferredSize(new Dimension(width, height));
button5 = new JButton("");
button5.setPreferredSize(new Dimension(width, height));
button6 = new JButton("");
button6.setPreferredSize(new Dimension(width, height));
button7 = new JButton("1");
button7.setPreferredSize(new Dimension(width, height));
button8 = new JButton("2");
button8.setPreferredSize(new Dimension(width, height));
button9 = new JButton("");
button9.setPreferredSize(new Dimension(width, height));
button10 = new JButton("");
button10.setPreferredSize(new Dimension(width, height));
button11 = new JButton("");
button11.setPreferredSize(new Dimension(width, height));
button12 = new JButton("");
button12.setPreferredSize(new Dimension(width, height));
public void buildLayout()
{
this.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
String colSizes = "20px, 54px, 10px, 54px, 20px";
String rowSizes = "20px, 48px, 20px, 48px, 20px, 48px, 20px, 48px, 20px, 200px, 48px, 48px, 20px, 48px";
FormLayout layout = new FormLayout(colSizes, rowSizes);
buttonPanel.setLayout(layout);
CellConstraints cc = new CellConstraints();
buttonPanel.add(button1, cc.xy(2, 2));
buttonPanel.add(button2, cc.xy(4, 2));
buttonPanel.add(button3, cc.xy(2, 4));
buttonPanel.add(button4, cc.xy(4, 4));
buttonPanel.add(button5, cc.xy(2, 6));
buttonPanel.add(button6, cc.xy(4, 6));
buttonPanel.add(button7, cc.xy(2, 8));
buttonPanel.add(button8, cc.xy(4, 8));
buttonPanel.add(slider, cc.xywh(2, 10, 3, 1));
buttonPanel.add(valueField, cc.xyw(2, 11, 3));
buttonPanel.add(button9, cc.xy(2, 12));
buttonPanel.add(button10, cc.xy(4, 12));
buttonPanel.add(button11, cc.xy(2, 14));
buttonPanel.add(button12, cc.xy(4, 14));
this.add(buttonPanel, BorderLayout.WEST);
}
public void registerListener()
{
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
button10.addActionListener(this);
button11.addActionListener(this);
button12.addActionListener(this);
slider.addChangeListener(this);
}
public void addDrawPanel(DrawPanel panel)
{
drawPanel=panel;
JPanel centerPanel = new JPanel();
String colSizes = "20px, pref,20px";
String rowSizes = "20px, pref,20px";
FormLayout layout = new FormLayout(colSizes, rowSizes);
centerPanel.setLayout(layout);
CellConstraints cc = new CellConstraints();
centerPanel.add(drawPanel, cc.xy(2, 2));
this.add(centerPanel, BorderLayout.CENTER);
insertedDrawPanel=true;
checkDrawPanel();
}
public void addOpenedImage(Image img)
{
drawPanel = new DrawPanel();
drawPanel.setPreferredSize(new Dimension(img.getWidth(null),img.getHeight(null)));
drawPanel.setBgImg(img);
JPanel centerPanel = new JPanel();
String colSizes = "20px, pref,20px";
String rowSizes = "20px, pref,20px";
FormLayout layout = new FormLayout(colSizes, rowSizes);
centerPanel.setLayout(layout);
CellConstraints cc = new CellConstraints();
centerPanel.add(drawPanel, cc.xy(2, 2));
this.add(centerPanel, BorderLayout.CENTER);
insertedDrawPanel=true;
checkDrawPanel();
}
@Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource().equals(button1))
{
activeButton=button1;
this.setCursor(penCursor);
resetButtonChanges();
drawPanel.setPenActive(true);
}
if(ae.getSource().equals(button2))
{
activeButton=button2;
this.setCursor(lineCursor);
resetButtonChanges();
drawPanel.setLineActive(true);
}
if(ae.getSource().equals(button3))
{
activeButton=button3;
this.setCursor(eraserCursor);
resetButtonChanges();
drawPanel.setEraserActive(true);
}
if(ae.getSource().equals(button4))
{
activeButton=button4;
resetButtonChanges();
drawPanel.setTextActive(true);
}
if(ae.getSource().equals(button7))
{
activeButton= button7;
resetButtonChanges();
color1 = JColorChooser.showDialog(null, "Pick your Background Color", color1);
if(color1!=null)
{
drawPanel.setBackground(color1);
}
}
if(ae.getSource().equals(button8))
{
activeButton= button8;
resetButtonChanges();
color2 = JColorChooser.showDialog(null, "Pick your Stroke / Shape Color", color2);
if(color2!=null)
{
drawPanel.setStrokecolor(color2);
}
}
if(ae.getSource().equals(button9))
{
activeButton=button9;
this.setCursor(rectCursor);
resetButtonChanges();
drawPanel.setRectActive(true);
}
if(ae.getSource().equals(button10))
{
activeButton=button10;
this.setCursor(ovalCursor);
resetButtonChanges();
drawPanel.setOvalActive(true);
}
if(ae.getSource().equals(button11))
{
activeButton=button11;
this.setCursor(triangleCursor);
resetButtonChanges();
drawPanel.setTriangleActive(true);
}
if(ae.getSource().equals(button12))
{
activeButton=button12;
this.setCursor(starCursor);
resetButtonChanges();
drawPanel.setStarActive(true);
}
}
public void resetButtonChanges()
{
if(activeButton!=button1)
{
drawPanel.setPenActive(false);
}
if(activeButton!=button2)
{
drawPanel.setLineActive(false);
}
if(activeButton!=button3)
{
drawPanel.setEraserActive(false);
}
if(activeButton!=button4)
{
drawPanel.setTextActive(false);
}
if(activeButton!=button9)
{
drawPanel.setRectActive(false);
}
if(activeButton!=button10)
{
drawPanel.setOvalActive(false);
}
if(activeButton!=button11)
{
drawPanel.setTriangleActive(false);
}
if(activeButton!=button12)
{
drawPanel.setStarActive(false);
}
}
public void checkDrawPanel()
{
if(insertedDrawPanel==false)
{
button1.setEnabled(false);
button2.setEnabled(false);
button3.setEnabled(false);
button4.setEnabled(false);
button5.setEnabled(false);
button6.setEnabled(false);
button7.setEnabled(false);
button8.setEnabled(false);
button9.setEnabled(false);
button10.setEnabled(false);
button11.setEnabled(false);
button12.setEnabled(false);
slider.setEnabled(false);
}
else
{
button1.setEnabled(true);
button2.setEnabled(true);
button3.setEnabled(true);
button4.setEnabled(true);
button5.setEnabled(true);
button6.setEnabled(true);
button7.setEnabled(true);
button8.setEnabled(true);
button9.setEnabled(true);
button10.setEnabled(true);
button11.setEnabled(true);
button12.setEnabled(true);
slider.setEnabled(true);
}
}
@Override
public void stateChanged(ChangeEvent ce) {
if(ce.getSource().equals(slider))
{
drawPanel.setStrokevalue(slider.getValue());
valueField.setText(String.valueOf(slider.getValue()));
}
if(ce.getSource().equals(button7))
{
button7.setBackground(color1);
button7.setForeground(color2);
}
if(ce.getSource().equals(button8))
{
button8.setBackground(color2);
button8.setForeground(color1);
}
}
}
我的DrawPanel代码
公共类DrawPanel扩展JPanel实现ActionListener,FocusListener,DocumentListener,MouseListener,MouseMotionListener {
private boolean penActive ,eraserActive, lineActive, rectActive, ovalActive, triangleActive, starActive, textActive, isDrag, isReleased;
private int x, y , xDrag, yDrag, xRelease, yRelease, strokevalue;
private Color strokecolor ,dragColor;
private Vector<int[]> allShapes;
private int[] dragShape;
private Vector<Color> shapeColor;
private Vector<String> type;
private String dragType;
private JTextField text;
private Image bgImg;
public boolean isPenActive() {
return penActive;
}
public void setPenActive(boolean penActive) {
this.penActive = penActive;
}
public boolean isEraserActive() {
return eraserActive;
}
public void setEraserActive(boolean eraserActive) {
this.eraserActive = eraserActive;
}
public boolean isTriangleActive() {
return triangleActive;
}
public void setTriangleActive(boolean triangleActive) {
this.triangleActive = triangleActive;
}
public boolean isLineActive() {
return lineActive;
}
public void setLineActive(boolean lineActive) {
this.lineActive = lineActive;
}
public int getStrokevalue() {
return strokevalue;
}
public void setStrokevalue(int strokevalue) {
this.strokevalue = strokevalue;
}
public Color getStrokecolor() {
return strokecolor;
}
public void setStrokecolor(Color strokecolor) {
this.strokecolor = strokecolor;
}
public boolean isRectActive() {
return rectActive;
}
public void setRectActive(boolean rectActive) {
this.rectActive = rectActive;
}
public boolean isOvalActive() {
return ovalActive;
}
public void setOvalActive(boolean ovalActive) {
this.ovalActive = ovalActive;
}
public boolean isStarActive() {
return starActive;
}
public void setStarActive(boolean starActive) {
this.starActive = starActive;
}
public boolean isTextActive() {
return textActive;
}
public void setTextActive(boolean textActive) {
this.textActive = textActive;
}
public Image getBgImg() {
return bgImg;
}
public void setBgImg(Image bgImg) {
this.bgImg = bgImg;
}
public DrawPanel()
{
setFocusable(true);
setBackground(Color.white);
initiateComponents();
addMouseMotionListener(this);
addMouseListener(this);
}
public void initiateComponents()
{
allShapes=new Vector<int[]>();
shapeColor=new Vector<Color>();
type=new Vector<String>();
eraserActive = false;
penActive = false;
lineActive = false;
rectActive = false;
ovalActive = false;
triangleActive = false;
starActive = false;
strokevalue=1;
strokecolor=Color.BLACK;
}
@Override
public void mouseClicked(MouseEvent me) {
if (me.getClickCount() == 2)
text.setEditable( true );
}
@Override
public void mousePressed(MouseEvent me) {
x = me.getX();
y = me.getY();
if(me.getClickCount() == 1)
{
this.requestFocusInWindow();
}
if(me.getClickCount() == 2)
{
System.out.println("asd");
text.setLocation(me.getPoint());
this.add(text);
text.requestFocusInWindow();
text.selectAll();
repaint();
}
}
@Override
public void mouseReleased(MouseEvent me) {
isDrag=false;
isReleased=true;
xRelease=me.getX();
yRelease=me.getY();
repaint();
}
@Override
public void mouseEntered(MouseEvent me) {
}
@Override
public void mouseExited(MouseEvent me) {
}
@Override
public void mouseDragged(MouseEvent me) {
isDrag=true;
isReleased=false;
xDrag=me.getX();
yDrag=me.getY();
repaint();
}
@Override
public void mouseMoved(MouseEvent me) {
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
if(penActive==true)
{
int[]coordinates=new int[5];
if(isDrag)
{
coordinates[0]=x;
coordinates[1]=y;
coordinates[2]=xDrag;
coordinates[3]=yDrag;
coordinates[4]=strokevalue;
allShapes.add(coordinates);
shapeColor.add(strokecolor);
type.add("Pen");
x=xDrag;
y=yDrag;
}
}
else if(eraserActive==true)
{
int[]coordinates=new int[5];
if(isDrag)
{
coordinates[0]=x;
coordinates[1]=y;
coordinates[2]=xDrag;
coordinates[3]=yDrag;
coordinates[4]=strokevalue;
allShapes.add(coordinates);
shapeColor.add(this.getBackground());
type.add("Eraser");
x=xDrag;
y=yDrag;
}
}
else if(lineActive==true)
{
int[]coordinates=new int[5];
if(isDrag)
{
dragShape=new int[5];
dragShape[0]=x;
dragShape[1]=y;
dragShape[2]=xDrag;
dragShape[3]=yDrag;
dragShape[4]=strokevalue;
dragColor=strokecolor;
dragType="Line";
}
if(isReleased)
{
dragShape=null;
coordinates[0]=x;
coordinates[1]=y;
coordinates[2]=xDrag;
coordinates[3]=yDrag;
coordinates[4]=strokevalue;
allShapes.add(coordinates);
shapeColor.add(strokecolor);
type.add("Line");
}
}
else if(textActive==true)
{
int[]coordinates=new int[3];
if(isReleased)
{
dragShape=null;
coordinates[0]=x;
coordinates[1]=y;
coordinates[2]=strokevalue;
allShapes.add(coordinates);
shapeColor.add(strokecolor);
type.add("Text");
}
}
else if(triangleActive==true)
{
int[]coordinates=new int[4];
if(isDrag)
{
dragShape=new int[4];
dragShape[0]=x;
dragShape[1]=y;
dragShape[2]=-y+yDrag;
dragShape[3]=strokevalue;
dragColor=strokecolor;
dragType="Triangle";
}
if(isReleased)
{
dragShape=null;
coordinates[0]=x;
coordinates[1]=y;
coordinates[2]=-y+yDrag;
coordinates[3]=strokevalue;
allShapes.add(coordinates);
shapeColor.add(strokecolor);
type.add("Triangle");
}
}
else if(starActive==true)
{
int[]coordinates=new int[4];
if(isDrag)
{
dragShape=new int[4];
dragShape[0]=x;
dragShape[1]=y;
dragShape[2]=-y+yDrag;
dragShape[3]=strokevalue;
dragColor=strokecolor;
dragType="Star";
}
if(isReleased)
{
dragShape=null;
coordinates[0]=x;
coordinates[1]=y;
coordinates[2]=-y+yDrag;
coordinates[3]=strokevalue;
allShapes.add(coordinates);
shapeColor.add(strokecolor);
type.add("Star");
}
}
else if(rectActive==true)
{
int[]coordinates=new int[5];
if(isDrag)
{
dragShape=new int[5];
dragShape[0]=x;
dragShape[1]=y;
dragShape[2]=xDrag;
dragShape[3]=yDrag;
dragShape[4]= strokevalue;
dragColor=strokecolor;
dragType="Rectangle";
}
if(isReleased)
{
dragShape=null;
coordinates[0]=x;
coordinates[1]=y;
coordinates[2]=xDrag;
coordinates[3]=yDrag;
coordinates[4]=strokevalue;
allShapes.add(coordinates);
shapeColor.add(strokecolor);
type.add("Rectangle");
}
}
else if(ovalActive==true)
{
int[]coordinates=new int[4];
if(isDrag)
{
dragShape=new int[4];
dragShape[0]=x;
dragShape[1]=y;
dragShape[2]=-y+yDrag;
dragShape[3]=strokevalue;
dragColor=strokecolor;
dragType="Oval";
}
if(isReleased)
{
dragShape=null;
coordinates[0]=x;
coordinates[1]=y;
coordinates[2]=-y+yDrag;
coordinates[3]=strokevalue;
allShapes.add(coordinates);
shapeColor.add(strokecolor);
type.add("Oval");
}
}
if(bgImg!=null)
{
g2.drawImage(bgImg, 0, 0, null);
}
for(int a=0;a<allShapes.size();a++)
{
if(type.get(a).equals("Pen")||type.get(a).equals("Eraser")||type.get(a).equals("Line"))
{
int[]coordinates=allShapes.get(a);
Color color=shapeColor.get(a);
g2.setStroke(new BasicStroke(coordinates[4]));
g2.setPaint(color);
g2.drawLine(coordinates[0], coordinates[1], coordinates[2], coordinates[3]);
}
else if(type.get(a).equals("Text"))
{
text = new JTextField("asd");
Font font = new Font("Serif", Font.PLAIN, 40);
g2.setFont(font);
}
else if(type.get(a).equals("Triangle"))
{
int[]coordinates=allShapes.get(a);
Point2D b= new Point2D.Double(coordinates[0], coordinates[1]);
Triangle triangle = new Triangle(b,coordinates[2]);
g2.setStroke(new BasicStroke(coordinates[3]));
g2.setPaint(dragColor);
g2.draw(triangle);
}
else if(type.get(a).equals("Star"))
{
int[]coordinates=allShapes.get(a);
Point2D b= new Point2D.Double(coordinates[0], coordinates[1]);
Star star = new Star(b,coordinates[2], coordinates[2]);
g2.setStroke(new BasicStroke(coordinates[3]));
g2.setPaint(dragColor);
g2.draw(star);
}
else if(type.get(a).equals("Rectangle"))
{
int[]coordinates=allShapes.get(a);
Point2D b=new Point2D.Double(coordinates[0], coordinates[1]);
Point2D c=new Point2D.Double(coordinates[2], coordinates[3]);
Rectangle rectangle = new Rectangle(b,c);
g2.setStroke(new BasicStroke(coordinates[4]));
g2.setPaint(dragColor);
g2.draw(rectangle);
}
else if(type.get(a).equals("Oval"))
{
int[]coordinates=allShapes.get(a);
Point2D b= new Point2D.Double(coordinates[0], coordinates[1]);
Circle oval = new Circle(b,coordinates[0], coordinates[1], coordinates[2]);
g2.setStroke(new BasicStroke(coordinates[3]));
g2.setPaint(dragColor);
g2.draw(oval);
}
}
if(dragShape!=null)
{
if(dragType.equals("Line"))
{
g2.setStroke(new BasicStroke(dragShape[4]));
g2.setPaint(dragColor);
g2.drawLine(dragShape[0], dragShape[1], dragShape[2], dragShape[3]);
}
else if(dragType.equals("Triangle"))
{
Point2D a= new Point2D.Double(dragShape[0], dragShape[1]);
Triangle triangle = new Triangle(a,dragShape[2]);
g2.setStroke(new BasicStroke(dragShape[3]));
g2.setPaint(dragColor);
g2.draw(triangle);
}
else if(dragType.equals("Star"))
{
Point2D a= new Point2D.Double(dragShape[0], dragShape[1]);
Star star = new Star(a,dragShape[2],dragShape[2]);
g2.setStroke(new BasicStroke(dragShape[3]));
g2.setPaint(dragColor);
g2.draw(star);
}
else if(dragType.equals("Rectangle")){
Point2D b=new Point2D.Double(dragShape[0], dragShape[1]);
Point2D c=new Point2D.Double(dragShape[2], dragShape[3]);
Rectangle rectangle = new Rectangle(b,c);
g2.setStroke(new BasicStroke(dragShape[4]));
g2.setPaint(dragColor);
g2.draw(rectangle);
}
else if(dragType.equals("Oval"))
{
Point2D a= new Point2D.Double(dragShape[0], dragShape[1]);
Circle oval = new Circle(a,dragShape[0],dragShape[1], dragShape[2]);
g2.setStroke(new BasicStroke(dragShape[3]));
g2.setPaint(dragColor);
g2.draw(oval);
}
}
}
@Override
public void focusGained(FocusEvent e) {
}
@Override
public void focusLost(FocusEvent e) {
text.setEditable(false);
}
@Override
public void insertUpdate(DocumentEvent e) {
text.setSize(getPreferredSize());
}
@Override
public void removeUpdate(DocumentEvent e) {
text.setSize(getPreferredSize());
}
@Override
public void changedUpdate(DocumentEvent e) {
}
@Override
public void actionPerformed(ActionEvent e) {
text.setEditable(false);
}
}
答案 0 :(得分:1)
以下是使用JTextField在该位置输入文本的示例。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class InvisibleTextField extends JTextField
implements ActionListener, FocusListener, MouseListener, DocumentListener
{
public InvisibleTextField()
{
setOpaque( false );
setColumns( 1 );
// setBorder( null );
setSize( getPreferredSize() );
setColumns( 0 );
addActionListener( this );
addFocusListener( this );
addMouseListener( this );
getDocument().addDocumentListener( this );
}
// Implement ActionListener
public void actionPerformed(ActionEvent e)
{
setEditable( false );
}
// Implement FocusListener
public void focusLost(FocusEvent e)
{
setEditable( false );
}
public void focusGained(FocusEvent e) {}
// Implement MouseListener
public void mouseClicked( MouseEvent e )
{
if (e.getClickCount() == 2)
setEditable( true );
}
public void mouseEntered( MouseEvent e ) {}
public void mouseExited( MouseEvent e ) {}
public void mousePressed( MouseEvent e ) {}
public void mouseReleased( MouseEvent e ) {}
// Implement DocumentListener
public void insertUpdate(DocumentEvent e)
{
updateSize();
}
public void removeUpdate(DocumentEvent e)
{
updateSize();
}
public void changedUpdate(DocumentEvent e) {}
private void updateSize()
{
setSize( getPreferredSize() );
}
public static void main(String[] args)
{
JPanel panel = new JPanel();
panel.setFocusable( true );
panel.setLayout( null );
panel.addMouseListener( new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
JPanel panel = (JPanel)e.getSource();
if (e.getClickCount() == 1)
{
panel.requestFocusInWindow();
}
if (e.getClickCount() == 2)
{
InvisibleTextField tf = new InvisibleTextField();
tf.setLocation(e.getPoint());
panel.add( tf );
tf.requestFocusInWindow();
}
}
});
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.add(new JLabel("Double Click to Add Text"), BorderLayout.NORTH);
frame.add(panel);
frame.setSize(650, 300);
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}
您可以通过双击文本随时编辑文本。