当前,我正在使用Java开发Paint程序。发生错误,出现了两个问题。
MouseListener不起作用(如果我单击它,应设置Color,但不起作用)。
如何用光标在绘图区域上绘画。 如果mousePressed == true-在鼠标位置绘制。 如果mouseReleased == true-停止绘制。
如何获取光标相对于框架的位置。 到此为止,它占据了整个屏幕。
主要类别:
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Icon;
import javax.swing.JFrame;
public class Main extends Canvas {
static int WIDTH = 830, HEIGHT = 1440;
static String TITLE = "Paint";
static Canvas canvas;
public static void main(String [] args) {
createWindow();
start();
}
private static void start() {
//Start class Draw
Draw draw = new Draw();
draw.start();
}
private static void createWindow() {
//Initialize the main frame
JFrame frame = new JFrame(TITLE);
frame.setSize(WIDTH,HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setVisible(true);
//Create canvas
canvas = new Canvas();
canvas.setPreferredSize(new Dimension(WIDTH,HEIGHT));
canvas.setMaximumSize(new Dimension(WIDTH,HEIGHT));
canvas.setMinimumSize(new Dimension(WIDTH, HEIGHT));
frame.add(canvas);
frame.pack();
}
public static Canvas getCanvas() {
return canvas;
}
}
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Icon;
import javax.swing.JFrame;
public class Main extends Canvas {
static int WIDTH = 830, HEIGHT = 1440;
static String TITLE = "Paint";
static Canvas canvas;
public static void main(String [] args) {
createWindow();
start();
}
private static void start() {
//Start class Draw
Draw draw = new Draw();
draw.start();
}
private static void createWindow() {
//Initialize the main frame
JFrame frame = new JFrame(TITLE);
frame.setSize(WIDTH,HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setVisible(true);
//Create canvas
canvas = new Canvas();
canvas.setPreferredSize(new Dimension(WIDTH,HEIGHT));
canvas.setMaximumSize(new Dimension(WIDTH,HEIGHT));
canvas.setMinimumSize(new Dimension(WIDTH, HEIGHT));
frame.add(canvas);
frame.pack();
}
public static Canvas getCanvas() {
return canvas;
}
}
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Icon;
import javax.swing.JFrame;
public class Main extends Canvas {
static int WIDTH = 830, HEIGHT = 1440;
static String TITLE = "Paint";
static Canvas canvas;
public static void main(String [] args) {
createWindow();
start();
}
private static void start() {
//Start class Draw
Draw draw = new Draw();
draw.start();
}
private static void createWindow() {
//Initialize the main frame
JFrame frame = new JFrame(TITLE);
frame.setSize(WIDTH,HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setVisible(true);
//Create canvas
canvas = new Canvas();
canvas.setPreferredSize(new Dimension(WIDTH,HEIGHT));
canvas.setMaximumSize(new Dimension(WIDTH,HEIGHT));
canvas.setMinimumSize(new Dimension(WIDTH, HEIGHT));
frame.add(canvas);
frame.pack();
}
public static Canvas getCanvas() {
return canvas;
}
}
绘画班:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
public class Draw extends Main implements Runnable{
static boolean running = false;
public Thread thread;
int mouseX, mouseY;
boolean mousePressed = false ,mouseReleased = false;
String COLOR, transitionColor;
private BufferStrategy bs;
private Graphics g;
public synchronized void start() {
if(running)return;
running = true;
thread = new Thread(this);
thread.start();
}
public synchronized void stop() {
if(!running)return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void run() {
init();
while(running == true) {
update();
render();
}
stop();
}
private void init() {
}
private void update() {
mouseX = MouseInfo.getPointerInfo().getLocation().x;
mouseY = MouseInfo.getPointerInfo().getLocation().y;
if(mouseY <= 96) {
if(mouseX <= 50) {
transitionColor = "GREEN";
}else if(mouseX <= 100 && mouseX >= 50) {
transitionColor = "BLUE";
}else if(mouseX <= 150 && mouseX >= 100) {
transitionColor = "RED";
}
if(mouseY >= 96 && mouseX >= 150) {
transitionColor = "NULL";
}
}
}
private void render() {
bs = Main.getCanvas().getBufferStrategy();
if(bs == null) {
Main.getCanvas().createBufferStrategy(3);
return;
}
g = bs.getDrawGraphics();
//Clear Screen
g.clearRect(0, 0, WIDTH, 50);
g.clearRect(0, 750, WIDTH, 50);
//----Draw-----
g.setColor(Color.WHITE);
g.fillRect(0, 50, WIDTH, 680);
g.setColor(Color.GREEN);
g.fillRect(0, 0, 50, 50);
g.setColor(Color.BLUE);
g.fillRect(50, 0, 50, 50);
g.setColor(Color.RED);
g.fillRect(100, 0, 50, 50);
g.setColor(Color.BLACK);
g.drawString("X: " + mouseX + " Y:" + mouseY + " Color: " + COLOR , 10, 760);
g.drawString("mousePressed: " + mousePressed , 620, 750);
g.drawString("mouseReleased: " + mouseReleased , 620, 765);
//-------------
bs.show();
g.dispose();
}
class CustomMouseListener implements MouseListener {
@Override
public void mouseClicked(MouseEvent e) {
COLOR = transitionColor;
}
@Override
public void mousePressed(MouseEvent e) {
mousePressed = true;
mouseReleased = false;
}
@Override
public void mouseReleased(MouseEvent e) {
mouseReleased = true;
mousePressed = false;
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
}
}
预先感谢