/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package firsttext;
import javax.swing.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.MouseInputListener;
import jdk.nashorn.internal.codegen.CompilerConstants;
import sun.security.x509.X400Address;
/**
*
* @author Asus
*/
public class Firsttext extends JFrame implements MouseListener,ActionListener,MouseInputListener{
public static final int Width=1000;
public static final int Height=1000;
public static int jishu=0;
MyPanel myPanel=new MyPanel();
public static int X1=0,Y1=0;
@Override
public void paint(Graphics g) {
//super.paint(g); //To change body of generated methods, choose Tools | Templates.
this.setBackground(Color.white);
Color color=g.getColor();
g.setColor(myPanel.color);
g.fillOval(X1,Y1,5,5);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new Firsttext();
}
public Firsttext(){
/*JFrame jFrame=new JFrame("第一个测试窗口");//上层容器
jFrame.setSize(Width,Height);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
JPanel contJPanel=new JPanel();//中间容器
jFrame.setContentPane(contJPanel);
jFrame.getContentPane().add(myPanel);
jFrame.addMouseListener(this);
//jFrame.pack();*/
this.setSize(WIDTH, HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.getContentPane().add(myPanel);
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.pack();
}
@Override
public void mouseClicked(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
//
// X1=e.getX();
//Y1=e.getY();
//System.out.println(X1+" "+Y1);
// repaint();
// JOptionPane.showConfirmDialog(this, "你很帅吗");
if(jishu==0){
jishu=1;
}
else jishu=0;
}
@Override
public void mousePressed(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseReleased(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseEntered(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseExited(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void actionPerformed(ActionEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseDragged(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseMoved(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
if(jishu==1){
X1=e.getX();
Y1=e.getY();
repaint();
}
}
}
class MyPanel extends JPanel implements ActionListener,MouseListener{
public JButton button,red,rgb,green,blue;
public Color color=new Color(0,0,0);
public MyPanel(){
button=new JButton("Get Color");
rgb=new JButton("RGB:");
red=new JButton("Red:");
green=new JButton("Green:");
blue=new JButton("Blue:");
button.addActionListener((ActionListener) this);
setPreferredSize(new Dimension(550,250));
setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
setBackground(color);
add(button);
add(rgb);
add(red);
add(green);
add(blue);
}
public void actionPerformed(ActionEvent e){
color=JColorChooser.showDialog(MyPanel.this,"Choose Color", color);
//setBackground(color);
button.setText("Get Again");
rgb.setText("RGB:"+color.getRGB());
red.setText("Red:"+color.getRed());
green.setText("Green:"+color.getGreen());
blue.setText("Blue:"+color.getBlue());
}
@Override
public void mouseClicked(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
//JOptionPane.showConfirmDialog(this, "dasda");
}
@Override
public void mousePressed(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseReleased(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseEntered(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseExited(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package firsttext;
import javax.swing.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.MouseInputListener;
import jdk.nashorn.internal.codegen.CompilerConstants;
import sun.security.x509.X400Address;
/**
*
* @author Asus
*/
public class Firsttext extends JFrame implements MouseListener,ActionListener,MouseInputListener{
public static final int Width=1000;
public static final int Height=1000;
public static int jishu=0;
MyPanel myPanel=new MyPanel();
public static int X1=0,Y1=0;
@Override
public void paint(Graphics g) {
//super.paint(g); //To change body of generated methods, choose Tools | Templates.
this.setBackground(Color.white);
Color color=g.getColor();
g.setColor(myPanel.color);
g.fillOval(X1,Y1,5,5);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new Firsttext();
}
public Firsttext(){
/*JFrame jFrame=new JFrame("第一个测试窗口");//上层容器
jFrame.setSize(Width,Height);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
JPanel contJPanel=new JPanel();//中间容器
jFrame.setContentPane(contJPanel);
jFrame.getContentPane().add(myPanel);
jFrame.addMouseListener(this);
//jFrame.pack();*/
this.setSize(WIDTH, HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.getContentPane().add(myPanel);
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.pack();
}
@Override
public void mouseClicked(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
//
// X1=e.getX();
//Y1=e.getY();
//System.out.println(X1+" "+Y1);
// repaint();
// JOptionPane.showConfirmDialog(this, "你很帅吗");
if(jishu==0){
jishu=1;
}
else jishu=0;
}
@Override
public void mousePressed(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseReleased(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseEntered(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseExited(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void actionPerformed(ActionEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseDragged(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseMoved(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
if(jishu==1){
X1=e.getX();
Y1=e.getY();
repaint();
}
}
}
class MyPanel extends JPanel implements ActionListener,MouseListener{
public JButton button,red,rgb,green,blue;
public Color color=new Color(0,0,0);
public MyPanel(){
button=new JButton("Get Color");
rgb=new JButton("RGB:");
red=new JButton("Red:");
green=new JButton("Green:");
blue=new JButton("Blue:");
button.addActionListener((ActionListener) this);
setPreferredSize(new Dimension(550,250));
setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
setBackground(color);
add(button);
add(rgb);
add(red);
add(green);
add(blue);
}
public void actionPerformed(ActionEvent e){
color=JColorChooser.showDialog(MyPanel.this,"Choose Color", color);
//setBackground(color);
button.setText("Get Again");
rgb.setText("RGB:"+color.getRGB());
red.setText("Red:"+color.getRed());
green.setText("Green:"+color.getGreen());
blue.setText("Blue:"+color.getBlue());
}
@Override
public void mouseClicked(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
//JOptionPane.showConfirmDialog(this, "dasda");
}
@Override
public void mousePressed(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseReleased(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseEntered(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void mouseExited(MouseEvent e) {
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
代码可以实现选择颜色并进行随心绘画,但还有一些bug,后期会进行更改
如图所示可以进行任意绘画