「操作矩形java」矩形的操作策略
本篇文章给大家谈谈操作矩形java,以及矩形的操作策略对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java绘制矩形
里面有矩形,自己参考下:
package ziliao;
//鼠标画图1
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//鼠标事件处理
//事件接口: MouseListener, MouseMotionListener
//使用鼠标画图
//使用JPanel面板绘图
class Co13 extends JFrame
{
JPanel jp;
JRadioButton jrb1, jrb2, jrb3, jrb4;
Co13()
{
jrb1 = new JRadioButton("直线",true);
jrb2 = new JRadioButton("矩形");
jrb3 = new JRadioButton("椭圆");
jrb4 = new JRadioButton("圆");
ButtonGroup bg = new ButtonGroup(); //按钮组
bg.add(jrb1); //加入按钮组
bg.add(jrb2);
bg.add(jrb3);
bg.add(jrb4);
jp = new JPanel();
jp.add(jrb1);
jp.add(jrb2);
jp.add(jrb3);
jp.add(jrb4);
add(jp,BorderLayout.NORTH);
add(new MyPanel(this));
setBounds(120,125,300,200);
setTitle("使用鼠标绘图");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public int getJrb() //判断判断并返回选择的是哪个单选按钮
{
int jrb;
if(jrb1.isSelected())
jrb = 1;
else if(jrb2.isSelected())
jrb = 2;
else if(jrb3.isSelected())
jrb = 3;
else
jrb = 4;
return jrb;
}
}
class MyPanel extends JPanel implements MouseListener, MouseMotionListener
{ //将JPanel面板作为画布
int x1, y1, x2, y2;
Co13 jf; //窗体对象,用于传递窗体中选择的按钮
MyPanel(Co13 jf)
{
this.jf = jf;
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
//覆盖 paintComponent 方法
public void paintComponent(Graphics g)
{
super.paintComponent(g); //调用超类JPanel构造方法
String s = null;
g.setColor(Color.red);
switch (jf.getJrb()) //判断选择什么图形
{
case 1: //画直线
g.drawLine(x1, y1, x2, y2); //起始点和终点,两点固定一条直线
s = "直线绘制";
break;
case 2: //画矩形
g.drawRect(x1, y1, x2 - x1, y2 - y1); //以起始点为原点,移动距离为长宽
s = "矩形绘制";
break;
case 3: //画椭圆
g.drawOval(x1, y1, x2 - x1, y2 - y1); //以起始点为圆心,移动距离为半径
s = "椭圆绘制";
break;
case 4: //画圆
int dx = Math.abs(x2 - x1);
int dy = Math.abs(y2 - y1);
int r = (int)(Math.sqrt(dx * dx + dy * dy));
g.drawOval(x1 - r, y1 - r, r + r, r + r);
g.setColor(Color.gray);
g.drawLine(x1, y1, x2, y2);
s = "正圆绘制";
break;
}
g.setColor(Color.blue);
g.drawString(s, 10, 10);
}
public void mousePressed(MouseEvent e)
{
x1 = e.getX(); //获取鼠标按下时的坐标
y1 = e.getY();
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
x2 = e.getX(); //获取鼠标拖动时的坐标
y2 = e.getY();
repaint(); //重绘图形
}
public void mouseMoved(MouseEvent e)
{
}
}
public class E6_13
{ public static void main(String args[])
{ new Co13();
}
}
提交回答
编写一个JAVA程序,描写一个矩形类,并输出某个矩形的长,宽,面积。具体描述如下?
// 矩形
public class RectangleDemo {
public static void main(String[] args) {
RectangleDemo demo = new RectangleDemo(12, 32);
System.out.println(demo.getPerimeter());
System.out.println(demo.getArea());
demo = new RectangleDemo();
System.out.println(demo.getArea());
System.out.println(demo.getPerimeter());
demo.setHeight(50);
demo.setWidth(30);
System.out.println(demo.getArea());
System.out.println(demo.getPerimeter());
}
// 求周
public double getPerimeter() {
return (height + width) * 2;
}
// 求面积
public double getArea() {
return height * width;
}
public RectangleDemo(double height, double width) {
this.height = height;
this.width = width;
}
public RectangleDemo() {
this.height = 10;
this.width = 10;
}
private double height;// 高度
private double width;// 宽度
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
}
编写矩形类RectangleJava程序矩形类两数据员别rLength宽rWidth通getLength()、getWidth()、getArea()别查看矩形、宽面积通setLength()setWidth()重新设置矩形宽
Java编写一个矩形类,并计算面积和周长?
class Rectangle{
private int width = 2;
private int length = 1;
public int getWidth(){
return this.width;
}
public void setWidth(int w){
this.width = w;
}
public int getLength(){
return this.length;
}
public void setLength(int l){
this.length = l;
}
public int getArea(){
return this.length * this.width;
}
public int getCircumference(){
return (this.length + this.width) * 2;
}
public Rectangle(){}
public Rectangle(int l, int w){
this.length = l;
this.width = w;
}
}
public class demo{
public static void main(String[] args) {
Rectangle rect = new Rectangle(30, 8);
System.out.print("长方形的面积是:");
System.out.println(rect.getArea());
System.out.printf("长方形的周长是:%d\n", rect.getCircumference());
}
}
java 画矩形
你的代码有问题,你的类本身是frame,你也在类中绘制,但是你却没有显示,而是另外定义了一个frame来显示,你修改一下:
import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Rectangle;
public class FrameTest extends Frame {
/**
* @param args
*/
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.black);
g.fillRect(100, 100, 30, 30);
try {
Thread.sleep(500);
}
catch (Exception ex) {
ex.printStackTrace();
}
//repaint();
}
FrameTest()
{
super("title");
setLocation(100,100);
setSize(600,400);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FrameTest ft=new FrameTest();
}
}
这样应该没问题了。
关于操作矩形java和矩形的操作策略的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-24,除非注明,否则均为
原创文章,转载请注明出处。