重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
input type="button" onmouseover="this.className='left'" onmousedown="this.className='right'"/
创新互联专注于青白江网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供青白江营销型网站建设,青白江网站制作、青白江网页设计、青白江网站官网定制、小程序开发服务,打造青白江网络公司原创品牌,更为您提供青白江网站排名全网营销落地服务。
随便再定义两个class即可,一个class把图片的位置设置在左边,一个设置在右边,位置结合position实现即可。
//画布类
import java.awt.Graphics;
import java.awt.Polygon;
import javax.swing.JPanel;
public class MyPanel extends JPanel{
public void drawRect(Graphics g, int x, int y, int width, int height){
g.drawRect(x, y, width, height);
}
public void drawOval(Graphics g, int x, int y, int width ,int height){
g.drawOval(x, y, width, height);
}
public void drawLine(Graphics g, int x1 ,int y1 ,int x2, int y2){
g.drawLine(x1, y1, x2, y2);
}
public void drawTriangle(Graphics g, int x, int y, int borderSize){
int[] xx = {borderSize,x,borderSize*2};
int[] yy = {y,borderSize*2,borderSize*2};
g.drawPolygon(new Polygon(xx,yy,3));
}
}
//主界面类
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyPaint extends JFrame implements ActionListener{
private JPanel toolArea;
private MyPanel imageArea;
private JButton rect;
private JButton oval;
private JButton line;
private JButton triangle;
public MyPaint() {
rect = new JButton("矩形");
rect.addActionListener(this);
oval = new JButton("圆形");
oval.addActionListener(this);
line = new JButton("直线");
line.addActionListener(this);
triangle = new JButton("三角");
triangle.addActionListener(this);
toolArea = new JPanel(new GridLayout(4,1));
toolArea.add(rect,0);
toolArea.add(oval,1);
toolArea.add(line,2);
toolArea.add(triangle,3);
imageArea = new MyPanel();
this.add("West",toolArea);
this.add("Center",imageArea);
this.setVisible(true);
this.setBounds(112, 84, 800, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
Graphics g = imageArea.getGraphics();
if(e.getSource() == rect){
imageArea.drawRect(g, 10, 10, 500, 300);
}else if(e.getSource() == oval){
imageArea.drawOval(g, 50, 50, 100, 100);
}else if(e.getSource() == line){
imageArea.drawLine(g, 20, 20, 80, 80);
}else if(e.getSource() == triangle){
imageArea.drawTriangle(g, 0, 40, 100);
}
}
public static void main(String[] args) {
new MyPaint();
}
}
有点麻烦,没写完,先给你开个头吧。你现在自己写写,有什么不懂的知识点再问我好啦。我吃早餐去了。
我只给你放了一个 panel ,你可以参考一下
要是不懂的话,继续留言
采用了你要求的 frame 但是,panel中没有border属性,采用了JPanel
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
public class PanelDraw extends Frame{
JPanel panel = new JPanel();
public PanelDraw() {
this.add(panel);
this.setSize(300, 200);
this.setVisible(true);
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
panel.setBorder(BorderFactory.createLineBorder(Color.RED));
}
@Override
public void mouseExited(MouseEvent e) {
panel.setBorder(null);
}
});
}
public static void main(String[] args) {
new PanelDraw();
}
}
使用组件的paint函数用于绘图, 使用MouseListener来响应鼠标的点击
效果图
代码
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
public class DemoWin extends JFrame {
public DemoWin() {
MyPanel mp = new MyPanel();
mp.addMouseListener(mp);
add(mp);
// 窗口属性设置
setTitle("Demo");// 标题
setSize(300, 280);// 窗口大小
setLocationRelativeTo(null);// 窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);// 窗口点击关闭时,退出程序
}
public static void main(String[] args) {
DemoWin win = new DemoWin(); // 创建窗口
win.setVisible(true);// 显示窗口
}
class MyPanel extends JPanel implements MouseListener {
int times;// 记录点击的次数
int x;// 记录鼠标X轴的位置
int y;// 记录鼠标Y轴的位置
@Override
public void paint(Graphics g) {
super.paint(g);
if (times == 0) {
g.setColor(Color.BLUE);// 颜色
g.fillOval(150, 150, 50, 50);// 150,150代表位置 50,50代表宽高
} else if (times == 1) {
g.setColor(Color.RED);
g.fillRect(150, 150, 50, 50);
} else {
g.setColor(Color.RED);
g.fillRect(x, y, 50, 50);
}
repaint();
}
public void mouseClicked(MouseEvent e) {
// if (e.getButton()==MouseEvent.BUTTON1) {//单击左键时有效..
// times++;// 记录点击的次数
// x = e.getX();
// y = e.getY();
// }
}
public void mousePressed(MouseEvent e) {// 鼠标按下就有效
times++;// 记录点击的次数
x = e.getX();
y = e.getY();
}
public void mouseReleased(MouseEvent e) {// 鼠标释放
}
public void mouseEntered(MouseEvent e) {// 鼠标移入
}
public void mouseExited(MouseEvent e) {// 鼠标移出
}
}
}
楼主提问的水平很高啊,题目的要求都没有,如何回答。。。
相关类都在
import java.awt.*;
import javax.swing.* ;
动作事件相关类在import java.awt.event.*;中,在代码头import就可以看了
给一个相关的代码吧,包括所有的鼠标事件。
下面代码包括
按下,释放,进入区域,离开区域,点击 这5个事件
鼠标的相关事件会在窗口上方的文本区显示。。。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TestMouseEvent extends JFrame implements MouseListener{
JPanel jp = new JPanel();
JTextField jtf = new JTextField();
public TestMouseEvent(){
setTitle("Mouse Event");
getContentPane().add(jtf,BorderLayout.NORTH);
getContentPane().add(jp,BorderLayout.CENTER);
jp.addMouseListener(this);
}
public static void main(String[] args){
TestMouseEvent frame = new TestMouseEvent();
frame.setSize(200,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void mousePressed(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出按下";
jtf.setText(s);
}
public void mouseReleased(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出释放";
jtf.setText(s);
}
public void mouseClicked(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出点击";
jtf.setText(s);
}
public void mouseEntered(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出进入";
jtf.setText(s);
}
public void mouseExited(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出离开";
jtf.setText(s);
}
}