importjavax.swing.*;importjava.awt.*;publicclassDrawRectangleextendsJPanel{privateintcenterX;privateintcenterY;privateintwidth;privateintheight;publicDrawRectangle(intcenterX,intcenterY,intwidth,intheight){this.centerX=centerX;this.centerY=centerY;this.width=width;this.height=height;}@Overrideprotectedv...
import javax.swing.JFrame; public class RectangleDrawing extends JFrame { public RectangleDrawing() { super(\Rectangle Drawing\ setSize(400, 300); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } @Override public void paint(Graphics g) { super.paint(g); g.drawRect(50, 50, 100...
importjavax.swing.*;importjava.awt.*;publicclassDrawRectangleextendsJPanel{@OverrideprotectedvoidpaintComponent(Graphicsg){super.paintComponent(g);g.drawRect(50,50,100,50);}publicstaticvoidmain(String[]args){JFrameframe=newJFrame();frame.add(newDrawRectangle());frame.setSize(200,200);frame.setDef...
警告: 绝大多数 Swing 组件类都以“ J” 开头, 例如,JButton、JFrame 等。在 Java 中有 Button 和 Frame 这样的类, 但它们属于 AWT 组件。如果偶然地忘记书写“ J”,程序仍然可以进行编译和运行,但是将 Swing 和 AWT 组件混合在一起使用将会导致视觉和行 为的不一致。 在本节中,将介绍有关 Swing 的 ...
g2.draw(r); } }publicRectangle2Dfind(Point2D p){for(Rectangle2D r : squares) {if(r.contains(p))returnr; }returnnull; }publicvoidadd(Point2D p){doublex=p.getX();doubley=p.getY(); current =newRectangle2D.Double(x - SIDELENGTH /2, y - SIDELENGTH /2, SIDELENGTH, SIDELENGTH);...
draw(rectangle); 常见问题与易错点 3.1 忘记调用super.paintComponent(g) 在重写paintComponent()时,忘记调用super.paintComponent(g)会导致背景不清除,新的图形会叠加在旧的图形上。 3.2 不正确的坐标系统理解 Java的坐标系统原点在左上角,x轴向右,y轴向下。错误地理解这一点可能导致图形位置不准确。 3.3 忽略...
equals("灰色"))g_2d.setColor(Color.gray);if(color.equals("黄色"))g_2d.setColor(Color.yellow);if(type==1) {g_2d.draw(new Line2D.Double(30, 30, 80, 100));}if(type==2) {g_2d.draw(new Rectangle2D.Double(50, 50, 150, 100));}if(type==3) {g_2d.draw(...
简介:第5章 边框、图标和动作 本章介绍Swing的三种实用工具:边框、图标和动作。 边框绘制在组件的边界周围,它有许多不同的各类:线边框、雕刻边框、不光滑的边框等等。 第5章 边框、图标和动作 本章介绍Swing的三种实用工具:边框、图标和动作。 边框绘制在组件的边界周围,它有许多不同的各类:线边框、雕刻边框、不...
关键知识点1:圆角效果(1)通过setClip设置剪切区域,只绘制圆角区域RoundRectangle2D.Double rect = new RoundRectangle2D.Double(0, 0, this.getWidth(), this.getHeight(), 20, 20);g.setClip(rect);注意要在super之前设置(2)重载paintBorder方法绘制圆角边框RoundRectangle2D.Double rect = new ...
java swing awt shapes 所以我试着写一个代码,我在圆内画正方形,如果正方形在圆外画,它就不会画(基本上圆充当边框或框架)。我一直在想我该怎么做。有人能帮忙吗。谢谢 这是我的密码。 public class Portal { public void drawPortal(Graphics2D g2){ g2.setColor(Color.black); g2.fillOval(80, 110,...