相关疑难解决方法(0)

在Java中使用按钮单击在JPanel中绘制一条线

我想在JPanel中画一条线.这是我的GUI,我希望JPanel中的一行是白色的.

在此输入图像描述

我找到很多例子,但问题是如何使用它.

在许多例子中,他们总是画一个从JPanel扩展的JFrame.

我想将面板添加到框架中并添加一些按钮以在多个方向上绘制线条,并使用中心的X按钮来清洁JPanel.

这是界面的代码:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class circuit extends JFrame {

 private JPanel contentPane;

 /**
  * Launch the application.
  */
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    try {
     circuit frame = new circuit();
     frame.setVisible(true);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }

 /**
  * Create the frame.
  */
 public …
Run Code Online (Sandbox Code Playgroud)

java graphics swing line jpanel

19
推荐指数
2
解决办法
4万
查看次数

Java Bouncing Ball

我正在尝试编写一个Java应用程序,它在屏幕上绘制多个球,从框架的边缘反弹.我可以成功地画出一个球.但是当我添加第二个球时,它会覆盖我绘制的初始球.代码是:

import java.awt.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

public class Ball extends JPanel implements Runnable {

    List<Ball> balls = new ArrayList<Ball>();   
Color color;
int diameter;
long delay;
private int x;
private int y;
private int vx;
private int vy;

public Ball(String ballcolor, int xvelocity, int yvelocity) {
    if(ballcolor == "red") {
        color = Color.red;
    }
    else if(ballcolor == "blue") {
        color = Color.blue;
    }
    else if(ballcolor == "black") {
        color = Color.black;
    }
    else if(ballcolor == "cyan") {
        color …
Run Code Online (Sandbox Code Playgroud)

java swing jframe

14
推荐指数
2
解决办法
3万
查看次数

如何使线条动画更流畅?

我正在用Java制作一个简单的动画,我试图让它尽可能顺利.

我只使用每个Shape对象的*.Double内部类,并在Graphics2D对象中设置抗锯齿.只要我只使用fill()方法,但是如果我也使用draw()方法在同一个Shape周围绘制线条,那么这些线条的动画都是不连续的 - 逐个像素.

画布上的每个矩形都有这种绘制方法.它每20ms移动一次,整个画布使用Timer和TimerListener重新绘制.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AnimationTest {
    public static void main(String[] args) {
        JFrame frm = new JFrame("Test");
        frm.setBounds(200, 200, 400, 400);
        frm.setResizable(false);
        frm.setLocationRelativeTo(null);

        AnimationCanvas a = new AnimationCanvas();
        frm.add(a);

        frm.setVisible(true);

        a.startAnimation();
    }
}

class AnimationCanvas extends JPanel {

    SimpleSquare[] squares = new SimpleSquare[2];

    AnimationCanvas() {

        squares[0] = new SimpleSquare(50, 80, true);
        squares[1] = new SimpleSquare(160, 80, false);

    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        for (SimpleSquare c : …
Run Code Online (Sandbox Code Playgroud)

java animation smooth timer graphics2d

2
推荐指数
1
解决办法
3944
查看次数

标签 统计

java ×3

swing ×2

animation ×1

graphics ×1

graphics2d ×1

jframe ×1

jpanel ×1

line ×1

smooth ×1

timer ×1