标签: paintcomponent

JPanel在java中发生了什么?我究竟做错了什么?

我的JPanel出了问题.我告诉它在构造函数的末尾重绘()然后我有动画(一个精灵在屏幕上移动),但是JPanel不会自己画画,直到精灵经过它的动画并到达它的新点.屏幕.我粘贴了下面的代码.我之前从未使用JPanel进行自定义图形处理.我究竟做错了什么?

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import javax.swing.*;

class Battle extends JPanel implements KeyListener {

    AllyParty ap;
    boolean showMenu = false;
    BufferedImage image;
    EnemyParty ep;
    Graphics screen;
    Image allyPic[], enemyPic[];
    int enemyCount;
    int mtCount = 0;
    int turn[];
    MediaTracker mt;
    Random rand;

    public Battle(AllyParty allyparty) {

        /*Initial JPanel subclass setup*/
        setSize(800, 600);
        setBackground(Color.black);

        /*Create our ally party*/
        ap = new AllyParty();
        ap = allyparty;
        ap.setCallingObject(this);

        /*Create randomizer and random number variable*/
        long seed = System.currentTimeMillis();
        rand = …
Run Code Online (Sandbox Code Playgroud)

java concurrency swing jpanel paintcomponent

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

如何在恢复代码之前等待setIcon()方法执行?

我有一个代码如下:

jLabel.setIcon(map);
Graphics2D g = (Graphics2D) jLabel.getGraphics();
g.drawPolygon(xpoints,ypoints,vpolygonpointsarray.length);
Run Code Online (Sandbox Code Playgroud)

但是会发生的是在更新标签图标之前首先绘制多边形.我怀疑设置一个图标会创建一个新线程.我想等到图标设置完毕,然后执行其余的代码.我该怎么办?

java swing jlabel graphics2d paintcomponent

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

SWING - 绘制直线,编译时不会出现

我试图在JFrame窗口中用鼠标画线,当我尝试这样做时,它不起作用!请忽略菜单,我想稍后再做一些事情!我错过了什么!如果你能给我一些提示,我将不胜感激!

public class newGUI extends JFrame implements ActionListener, MouseMotionListener, MouseListener {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
Point point1;
Point point2;
Line2D line2d;

public static void main(String[] args){
    newGUI gui = new newGUI();
    gui.setVisible(true);
}

public newGUI()
{
    super("Menu Demonstration");
    setSize(WIDTH, HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenu colorMenu = new JMenu("Choose Colors");
    JMenuItem greenChoice = new JMenuItem("GREEN");
    greenChoice.addActionListener(this);
    colorMenu.add(greenChoice);
    JMenuItem redChoice = new JMenuItem("RED");
    colorMenu.add(redChoice);
    JMenuBar …
Run Code Online (Sandbox Code Playgroud)

java swing jframe paintcomponent

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

为什么我的面板上的形状和图像消失了?

我已经使用该paintComponent方法在我的面板上绘制形状.但是,每次我最小化帧或调整大小时,它们都会消失.不确定要添加到我的代码中的内容.

   public class ShapePanel extends JPanel implements ActionListener, MouseListener{

    int a,b,c,d;
    Graphics2D g2D;
    private Rectangle2D rect = new Rectangle2D(a,b,c-a,d-b);

    public ShapePanel(){

    addMouseListener(this);
    setLayout(new GridLayout());
}

    public void paintComponent(Graphics g) {

    g2D = (Graphics2D) g;
    g2D.draw(rect);
    repaint();

}


   //get methods for coordinates: MousePressed, MouseReleased
Run Code Online (Sandbox Code Playgroud)

java graphics swing shapes paintcomponent

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

我可以问一下如何反复重绘()吗?

对不起,请问java重复重复的问题,我遇到麻烦,我反复使用起草来表达Pacman Open&Close嘴巴动作.但是这个程序只有一次不会动......有人可以帮我解决这个问题......非常非常感谢!:D

我的代码如下:

package Strive;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

class CDrawF extends JFrame {
    CDrawF (){
        setTitle("??????");                       //JFrame interface
        setBounds(50, 50, 490, 260);        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        for(int i = 0; i<= 360; i++){              //repeatly 360 times
        repaint();
        g2.setColor(new Color(1.0f, 0.0f, 1.0f));               
        g2.fill(new Arc2D.Double(100, 100, 80, 80, 30, 300, Arc2D.PIE)); 
        //PacMan close mouth
        repaint();
        try{            //Delay setions
                Thread.sleep(1000);
             }catch(InterruptedException ex){}
        g2.fill(new Arc2D.Double(100, 100, 80, 80, 10, 340, Arc2D.PIE)); 
        //PacMan …
Run Code Online (Sandbox Code Playgroud)

java swing paint repaint paintcomponent

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

JFrame pack()和requestFocus()不起作用

我的主要问题是在设置时使用以下代码JFrame:

  1. 为什么面板不显示我是否使用它pack()以及如何使其工作?

  2. 为什么第一个requestFocusInWindow()不起作用,使用它的原理是什么?

  3. JPanel如果我删除了默认布局管理器为什么不起作用setLayout()


public class SoundGUI extends KeyAdapter{

public static void main(String[] args) {
    SoundGUI sGUI = new SoundGUI();
    sGUI.setUp();
}
public void setUp () {
    JFrame frame = new JFrame ("Key test");
    frame.setSize (1000, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible (true);

    Panel p = new Panel ();
    p.setLayout(new BorderLayout());//why this sentence is necessary  FlowLayout doesn't fill the container but rather lets components size to their preferredSizes.
    p.addKeyListener (this);
    p.requestFocusInWindow();//it's useless here
    //requestFocus …
Run Code Online (Sandbox Code Playgroud)

java swing jpanel paintcomponent preferredsize

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

矩形没有按照我想要的方式移动

我已经编写了这个java代码,我希望蓝色矩形像一个边界球一样四处移动.但是当我试图运行代码时,它似乎停留在一个位置并且在一行中移动.以下是代码.

import javax.swing.*;
import java.awt.*;

public class MoveBody extends JFrame {
    BallPanel ballpanel;
    JFrame frame;

    int X=15;
    int Y=15;


    boolean up=false;
    boolean down=true;
    boolean left=false;
    boolean right=true;




    public static void main(String[]args)
    {

        new MoveBody().go();
    }


    private void go()
    {

            frame = new JFrame("Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            ballpanel = new BallPanel();

            frame.getContentPane().add(BorderLayout.CENTER, ballpanel);
            frame.setVisible(true);
            frame.setResizable(false);
            frame.setSize(500,500);
            frame.setLocation(375, 55);
            moveIt();
    }



class BallPanel extends JPanel
{
    public void paintComponent(Graphics g)
    {
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        g.setColor(Color.WHITE);
        g.fillRect(10, 10, this.getWidth()-10,this.getHeight()-10);
        g.setColor(Color.BLUE);
        g.fillRect(X,Y, 50, 50); …
Run Code Online (Sandbox Code Playgroud)

java swing paintcomponent

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

问题在负方向绘制矩形 - Java

我在尝试从屏幕上单击的点向负方向绘制矩形时遇到问题。我有以下类来模拟 Gyazo 之类的屏幕捕获软件:

class DrawSquare extends JPanel implements MouseListener, MouseMotionListener {

// Components
public JDialog frame;
public Rectangle rectangle;
public BufferedImage bufferedImage;
public Point start, end;

// Variables
public String capturedImage;

public DrawSquare(JDialog frame) {

    this.frame = frame;

    // Read in crosshair image to replace mouse icon
    Toolkit tool = Toolkit.getDefaultToolkit();
    Image newImage = getToolkit().getImage("components/cursor.png");
    Cursor cursor = tool.createCustomCursor(newImage, new Point (this.frame.getX(), this.frame.getY()), "img");

    this.frame.setCursor(cursor);
    this.frame.addMouseListener(this);
    this.frame.addMouseMotionListener(this);

}

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

    Graphics2D g2d = (Graphics2D) g.create(); …
Run Code Online (Sandbox Code Playgroud)

java jframe jdialog paintcomponent

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

Java重绘方法不起作用,为什么?

好吧,我的英语不太好,但我会尽力解释。

我被分了两个班,一等班和二等班(第二班的名字叫“Grafika”)。

我希望我的矩形移动到我被单击的位置,但显然他没有移动,我不明白为什么,请帮忙。

import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.*;
    public class Grafika extends JPanel implements MouseListener{
        static int x=0,y=0;
        @Override
    public void paintComponent(Graphics g){
        g.setColor(Color.BLACK);
        g.fillRect(x, y, 20, 30);
    }

    public void mouseClicked(MouseEvent arg0) {
        // TODO Auto-generated method stub``
        x=arg0.getX();
        y=arg0.getY();
        this.repaint(x, y, 20, 30);
    }
Run Code Online (Sandbox Code Playgroud)

我将向您展示完整的代码,它很小。这是第二类。我的(我认为)唯一的问题是 repaint() 方法。为什么我不知道:D。

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
public class Grafika extends JPanel implements MouseListener{
    static int x=0,y=0;
    @Override
public void paintComponent(Graphics g){
    g.setColor(Color.BLACK);
    g.fillRect(x, y, 20, 30); …
Run Code Online (Sandbox Code Playgroud)

java swing paintcomponent mouselistener

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

在缓冲图像上绘图

我正在尝试绘制缓冲图像。我能够在框架上获取图片,但它似乎没有在图像上绘制。如果我使用

BufferedImage bufferedImage = new BufferedImage(1280, 800,BufferedImage.TYPE_INT_RGB);

然后它似乎绘制了字符串,但我想理想地在图像上绘制,因为我需要在图像上为项目绘制一些坐标。任何指导将不胜感激。原谅糟糕的缩进

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class drawTest extends JPanel {

public void paint(Graphics g) {
   Image img = createImageWithText();
   g.drawImage(img, 20,20,this);
}

private Image createImageWithText(){
   BufferedImage bufferedImage = new BufferedImage(1280, 800,BufferedImage.TYPE_INT_RGB);
 //   BufferedImage bufferedImage = new BufferedImage()
  Graphics g = bufferedImage.getGraphics();

  try {
    bufferedImage = ImageIO.read(getClass().getResource("Unknown.jpg"));

  } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  } …
Run Code Online (Sandbox Code Playgroud)

java swing bufferedimage jframe paintcomponent

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