Graphical artifact with BorderFactory in paintComponent - java

I have some code for a Scrabble Game I am writing (actually rewriting), and it is split up into 3 classes so far. The Tile class seems to be generating a BorderFactory artifact when it is put in the window. Why is this artifact appearing, and how can I eliminate it?
*If you don't see the artifact, try resizing the window slowly.
Here is the Scrabble class.
import javax.swing.JFrame;
import java.awt.BorderLayout;
public class Scrabble extends JFrame implements Runnable
{
public Board board;
public Scrabble()
{
super("Scrabble!");
board = new Board();
addBorderLayoutObjects();
}
public void run()
{
makeSettings();
setVisible(true);
}
public void makeSettings()
{
setSize(850, 900);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//this.setResizable(false);
}
public void addBorderLayoutObjects()
{
getContentPane().add(BorderLayout.CENTER, board);
}
public static void main(String[] args)
{
Scrabble s = new Scrabble();
javax.swing.SwingUtilities.invokeLater(s);
}
}
Here is the Board class.
import javax.swing.JPanel;
import java.awt.Point;
import java.awt.Color;
import java.awt.GridLayout;
public class Board extends JPanel
{
public Tile [][] board;
public Board()
{
super(new GridLayout(15, 15));
board = new Tile[15][15];
makeBoard();
}
public void makeBoard()
{
for(int k = 0; k < 225; k++)
{
board[k/15][k%15] = new Tile(new Color(0xCBC4A8), Color.BLACK);
}
for(int a = 0; a < 15; a++)
{
for(int l = 0; l < 15; l++)
{
add(board[a][l]);
}
}
}
}
And the Tile class.
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Point;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class Tile extends JPanel implements MouseListener
{
private Color color;
private Color border;
public Tile(Color _color, Color _border)
{
color = _color;
border = _border;
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
#Override
public void paintComponent(Graphics g)
{
g.setColor(color);
setBorder(BorderFactory.createLineBorder(border, 2));
}
}

This is your first problem (and possibly your second)
public void paintComponent(Graphics g)
{
g.setColor(color);
setBorder(BorderFactory.createLineBorder(border, 2));
}
Firstly, you must call super.paintComponent or take responsibility for its actions. This will clear and prepare the Graphics context for further painting.
Secondly, you should never modify any UI component from within a paintXxx method that may cause it be invalidated or repainted. Doing so will end you an infinite loop of burning CPU...
Set the border within the constructor or change it as the program needs, do not do so from within the paint method...
Updated with additional examples
When you need to change the border, simply call setBorder on the instance of the title you want change....
public Tile(Color _color, Color _border)
{
color = _color;
border = _border;
setBorder(BorderFactory.createLineBorder(border, 2));
}
or
Title tile = new Tile(Color.RED, Color.BLUE);
//...
title.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2));
Check out How to use borders for more info...

Related

Repainting-Thread doesn't repaint Inner-Class JPanel

I want to make a little rain program in swing, but for some reason I cannot repaint the panel from another class. I tried using an inner class for the panel this time, but it doesn't seem to work with repainting it from another class/thread. Does someone know why?
sscce:
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
public class UI extends JFrame {
public static void main(String[] args) {
UI myProgram = new UI();
myProgram.setVisible(true);
}
public UI() {
this.setSize(new Dimension(500,300));
this.setBackground(Color.WHITE);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
UserPanel p = new UserPanel(this);
}
public class UserPanel extends JPanel implements ActionListener {
private Timer time = new Timer(1, this);
private UI myFrame;
public UserPanel(UI myFrame) {
this.myFrame = myFrame;
this.setSize(myFrame.getSize());
time.start();
}
#Override
public void actionPerformed(ActionEvent e) {
repaint();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("painting");
g.setColor(Color.BLACK);
g.fillRect(this.getWidth()/2, this.getHeight()/2, 50,50);
}
}
}
UI Class (with inner class JPanel):
package Rain;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
public class UI extends JFrame {
public UI() {
this.setSize(new Dimension(500,300));
this.setBackground(Color.WHITE);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
UserPanel p = new UserPanel(this);
}
private class UserPanel extends JPanel implements ActionListener {
private Timer time = new Timer(1, this);
private UI myFrame;
private ArrayList<Raindrop> rain = new ArrayList<Raindrop>();
private static final int AMOUNT = 50;
private Random rand = new Random();
public UserPanel(UI myFrame) {
this.myFrame = myFrame;
this.setSize(myFrame.getSize());
for(int i = 0; i < AMOUNT; i++) {
createRain();
}
new Painter(this);
time.start();
}
public void createRain() {
float distance = rand.nextFloat() * 90 + 10;
int x = rand.nextInt(this.getWidth());
int y = 100;
rain.add(new Raindrop(distance,x,y));
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("tick");
for(Raindrop r : rain) {
r.fall();
}
}
public void paintComponent(Graphics g) {
System.out.println("painting");
g.setColor(this.getBackground());
g.fillRect(0,0,this.getWidth(),this.getHeight());
for(Raindrop r : rain) {
r.draw(g);
}
}
}
}
Painter:
package Rain;
import javax.swing.JPanel;
public class Painter extends Thread {
private JPanel p;
public Painter(JPanel p) {
this.p = p;
this.start();
}
public void run() {
while(true) {
System.out.println("trying to paint..");
p.repaint();
}
}
}
Console Output:
trying to paint..
tick
trying to paint..
tick
...
Expected Output:
trying to paint..
painting
tick
trying to paint..
...
The thread does work but it never calls the paintComponent(Graphics g) function in the panel
All Swing applications must run on their own thread, called EDT. (Hopefully, you start your application by calling SwingUtilities#invokelater method). So, repainting a component outside of Event Dispatch Thread is really bad bad (bad) idea. Instead of creating new Thread, repaint the component inside javax.swing.Timer's action listener since it will run in EDT.
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("tick");
for(Raindrop r : rain) {
r.fall();
}
repaint(); //repaint in EDT
}
Also, when you #Override paintComponent method, always start by calling super.paintComponent(g);
public void paintComponent(Graphics g) {
super.paintComponent(g);//let component get painted normally
System.out.println("painting");
g.setColor(this.getBackground());
g.fillRect(0,0,this.getWidth(),this.getHeight());
for(Raindrop r : rain) {
r.draw(g);
}
}
UPDATE after your SSCCE
In order a component to get painted, it must have a parent. You UserPanel p = new UserPanel(this); but you never add it to the frame:
UserPanel p = new UserPanel(this);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p);
The complete SSCCE:
public class UI extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> { //Run in EDT
UI myProgram = new UI();
myProgram.setVisible(true);
});
}
public UI() {
super("title");//call super for frame
this.setSize(new Dimension(500, 300));
this.setBackground(Color.WHITE);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
UserPanel p = new UserPanel(this);
//Use border layout to make p fit the whole frame
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p, BorderLayout.CENTER);
}
public class UserPanel extends JPanel implements ActionListener {
private Timer time = new Timer(1, this);
private UI myFrame;
public UserPanel(UI myFrame) {
this.myFrame = myFrame;
this.setSize(myFrame.getSize());
time.start();
}
#Override
public void actionPerformed(ActionEvent e) {
repaint();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("painting");
g.setColor(Color.BLACK);
g.fillRect(this.getWidth() / 2, this.getHeight() / 2, 50, 50);
}
}
}
Don't ignore the SwingUtilities.invokeLater.

BufferStrategy cleans Frame background

I have tried to make a simple GUI in Java Using graphics2D and JFrame.
I have added a background-color on JFrame typing this.setBackground(new Color(54, 71, 99)) inside initWindow() method. It turned out that backBuffer was clearing that background and not repainting the line that causes this is in render() method, last line backBuffer.show().
How Do I make it NOT clear the main background?
package asteroids;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
public class Main extends Canvas implements KeyListener {
private boolean gameOver;
private BufferStrategy backBuffer;
private Dimension dimension = new Dimension(Config.WINDOW_WH[0], Config.WINDOW_WH[1]);
private List<Star> stars = new ArrayList<Star>();
private HashMap<Integer,Boolean> keyDownMap = new HashMap<Integer, Boolean>();
public Main() {
// Initialize Window
initWindow();
addKeyListener(this);
this.setBackground(new Color(54, 71, 99));
this.createBufferStrategy(2);
backBuffer = this.getBufferStrategy();
// init variables
gameOver = false;
// Generating stars
generateStars();
// Init loop
gameLoop();
}
public void initWindow(){
JFrame window = new JFrame("Asteroids");
setPreferredSize(dimension);
window.add(this);
window.pack();
window.setResizable(false);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBackground(new Color(54, 71, 99));
window.requestFocus();
}
public void update() {
if(keyDownMap.containsKey(KeyEvent.VK_ESCAPE)){
gameOver = false;
System.exit(0);
}
}
public void render(){
Graphics2D g = (Graphics2D) backBuffer.getDrawGraphics();
g.setColor(Color.WHITE);
for(Star s: stars) {
g.fillOval(s.posx - (s.width/2), s.posy - (s.height/2), s.width, s.height);
}
g.dispose();
backBuffer.show();
}
public void generateStars() {
for(int i = 0;i < 20;i++) {
int starX = new Random().nextInt(Config.WINDOW_WH[0]-10)+5;
int starY = new Random().nextInt(Config.WINDOW_WH[1]-10)+5;
stars.add(new Star(starX, starY));
}
}
public void gameLoop(){
while(!gameOver){
update();
render();
try{ Thread.sleep(20);}catch(Exception e){};
}
}
public static void main(String[] args) {
new Main();
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
keyDownMap.put(e.getKeyCode(), true);
}
#Override
public void keyReleased(KeyEvent e) {
keyDownMap.remove(e.getKeyCode());
}
}
How Do I make it NOT clear the main background?
You can't. Apart from Canvas been non-transparent (which can't be changed), BufferStrategy also has more then one page onto which it paints it's content (thus allowing to perform page flipping). Combined, this would make it impossible to maintain the background of the parent container.
Instead, you should (in fact, you must) clear the Graphics context of the buffer you painting to do, every time render is called, otherwise you will be painting onto what ever was previously painted on to it.
One technique might be to generate a BufferedImage with the "static" content and simply paint that to the buffer first

I cannot draw objects and have them move on the screen

I can draw static things to the screen, but I want to make them move with user key input. I don't know what to do, I've been searching and searching and haven't come up with an answer yet. Please help!
package com.Game.game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game extends JFrame
{
final static int width = 500;
final static int height = 500;
public int x = 250;
public int y = 250;
public int changeX = 10;
public int changeY = 10;
public static void main(String[] args)
{
new Game();
}
public Game()
{
KeyListener listener = new KeyListening();
addKeyListener(listener);
setFocusable(true);
DrawingStuff drawingstuff = new DrawingStuff();
add(drawingstuff);
setSize(width, height);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public class DrawingStuff extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawString("Hey there!", 300, 300);
g.setColor(Color.RED);
g.fillRect(x, y, 50, 50);
}
}
public class KeyListening implements KeyListener
{
DrawingStuff drawingstuff = new DrawingStuff();
#Override
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_UP)
{
y = y + changeY;
System.out.println("Hey");
drawingstuff.repaint();
}
}
#Override
public void keyReleased(KeyEvent e)
{
}
#Override
public void keyTyped(KeyEvent e)
{
}
}
public void update()
{
}
}
EDIT: Fixed it. I took away the key listener stuff in the constructor method, added a command to focus on "drawingstuff" in the constructor method, and, most importantly, added this bit of code to the end of the constructor method:
while(true)
{
drawingstuff.repaint();
}
The problem is that your KeyListening object has a reference to a different DrawingStuff object than the one you added to your UI inside the Game constructor.
public class KeyListening implements KeyListener
{
DrawingStuff drawingstuff = new DrawingStuff();
...
You should pass a DrawingStuff reference to the KeyListening instance so that it can tell the right object to repaint itself.

how to implement mouselistener on a particular shape?

i have created an circular strip using swing in java, and now i want to display some text on mouse click clicking a particular region of the strip such as region between 45 degrees and 135 degrees central angle can anybody help me out?
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Test extends JFrame implements MouseListener
{
//public static final int MOUSE_CLICKED;
public static void main(String[] args)
{
new Test();
}
public Test()
{
this.setSize(400,400);
this.setVisible(true);
addMouseListener(this);
}
public void paint(Graphics g)
{
g.fillArc(50,50,230,270,45,90);
g.setColor(Color.red);
double radius1 = 230;
double theta=90;
double a1 = (Math.PI * radius1 * radius1*theta)/360;
System.out.println("area"+a1);
double radius2 = 200;
double a2 = (Math.PI * radius2 * radius2*theta)/360;
System.out.println("area2"+a2);
double a=a1-a2;
System.out.println("fin area"+a);
g.fillArc(50,50,230,270,135,90);
g.setColor(Color.blue);
g.fillArc(50,50,230,270,225,90);
g.setColor(Color.yellow);
g.fillArc(50,50,230,270,315,90);
g.setColor(Color.magenta);
Graphics2D comp2D=(Graphics2D)g;
comp2D.setColor(Color.white);
Ellipse2D.Float sign=new Ellipse2D.Float(90F,90F,150F,200F);
comp2D.fill(sign);
{
//System.out.println("hello");
}
}
//}
}
Don't extend JFrame and don't override paint. Instead, you should extend JPanel and override paintComponent. Additionally, you should create the GUI on the Event Dispatch Thread.
Concerning the actual question: There are different possible solutions for this. You'll have to implement the MouseListener interface in any case. And after a mouse click, you have to check whether the mouse position is contained in the respective region. You could to this manually, by comparing coordinates and computing angles, but this could be a hassle. It should be much easier to create Shape objects that you fill with the respective colors, and then just check whether any of the shapes contains the mouse position.
This approach is roughly sketched here, although I did not reproduce your exact shapes. (You may have to create them with help of the Area class and its methods for union and intersection).
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Arc2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ShapeClickTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new ShapeClickTestPanel());
f.setSize(400, 400);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class ShapeClickTestPanel extends JPanel implements MouseListener {
private final List<Shape> shapes;
private final List<Color> colors;
public ShapeClickTestPanel() {
addMouseListener(this);
shapes = new ArrayList<Shape>();
colors = new ArrayList<Color>();
shapes.add(new Arc2D.Double(50, 50, 230, 270, 45, 90, Arc2D.OPEN));
colors.add(Color.RED);
}
#Override
protected void paintComponent(Graphics gr) {
super.paintComponent(gr);
Graphics2D g = (Graphics2D) gr;
for (int i = 0; i < shapes.size(); i++) {
Shape shape = shapes.get(i);
Color color = colors.get(i);
g.setColor(color);
g.fill(shape);
}
}
#Override
public void mouseClicked(MouseEvent e) {
for (int i = 0; i < shapes.size(); i++) {
Shape shape = shapes.get(i);
if (shape.contains(e.getPoint())) {
System.out.println("Clicked shape " + i);
}
}
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
}

How to use paintComponent in Java to paint multiple things, but rotate one?

I'm making a program in Java for my CS class. My teacher has little experience with graphics programing in Java so I've turned to you. I'm currently using the paintComponent method of my main panel to draw two things, one, a rectangle (my cannon, possibly replaced with a image later), and two, a .png file of a cannon ball. I use the Graphics g (which I convert to Graphics2D) to paint the cannon and Ball on to the screen. I then rotate, but, the cannon and ball rotate, not just the cannon. Any tips, suggestions, or helpful tutorials are greatly appreciated.
Here is my code (the commented out links are where I got certain code, ignore them):
package Cannon;
import java.awt.geom.Point2D;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class NewMain{
public static void main(String[] args) {
FraMainWindow frame = new FraMainWindow();
}
}
class FraMainWindow extends JFrame {
DrawCannon pnlCannon = new DrawCannon();
ButtonPannel pnlButtons = new ButtonPannel();
public FraMainWindow() {
this.setDefaultCloseOperation(JFrame.EXI…
this.setTitle("Super Mario Cannon Bro's");
this.setSize(900, 550);
this.setLayout(new BorderLayout());
this.add(pnlCannon, BorderLayout.CENTER);
this.add(pnlButtons, BorderLayout.SOUTH);
MouseMovement mouseMove = new MouseMovement();
MouseAction mouseClick = new MouseAction();
pnlCannon.addMouseMotionListener(mouseMo…
pnlCannon.addMouseListener(mouseClick);
FireButton actnFire = new FireButton();
pnlButtons.btnFire.addActionListener(act…
this.setVisible(true);
}
public class DrawCannon extends JPanel{
Rectangle.Float rectCannon = new Rectangle.Float(30, 450, 50, 10);
Image imgBall=new ImageIcon("ball.png").getImage();
double dAngle = 0;
boolean isFired = false;
public void addCannonBall(){
isFired=true;
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_… RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_… RenderingHints.VALUE_RENDER_QUALITY);//A… Aliasing from http://www.java2s.com/Code/Java/2D-Graph…
g2d.rotate(0 - dAngle, rectCannon.getX(), rectCannon.getY() + 5);
g2d.fill(rectCannon);
if(isFired){
g2d.drawImage(imgBall, 0, 0, null);
}
//Dimension size = getSize();
}
}
public class ButtonPannel extends JPanel {
JButton btnFire = new JButton("Fire!");
ButtonPannel() {
this.add(btnFire);
}
}
public class FireButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
pnlCannon.addCannonBall();
System.out.println("Fire ZE MISSILES");
}
}
public class MouseMovement implements MouseMotionListener {
public void mouseDragged(MouseEvent e) {
double dBase, dHeight, dAngle;
dBase = e.getX() - pnlCannon.rectCannon.getX();
dHeight = pnlCannon.rectCannon.getY() - 5 - e.getY() + 10;
dAngle = Math.atan2(dHeight, dBase);
pnlCannon.dAngle = dAngle;
pnlCannon.repaint();
}//http://download.oracle.com/javase/tutori…
public void mouseMoved(MouseEvent e) {
}
}
public class MouseAction implements MouseListener {
public void mousePressed(MouseEvent e) {
double dBase, dHeight, dAngle;
dBase = e.getX() - pnlCannon.rectCannon.getX();
dHeight = pnlCannon.rectCannon.getY() - 5 - e.getY() + 10;
dAngle = Math.atan2(dHeight, dBase);
pnlCannon.dAngle = dAngle;
pnlCannon.repaint();
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
} // From http://www.rgagnon
Try moving this bit:
if(isFired){
g2d.drawImage(imgBall, 0, 0, null);
}
before this line:
g2d.rotate(0 - dAngle, rectCannon.getX(), rectCannon.getY() + 5);
Any transformations you apply to your Graphics2D will affect anything from that point, so you have to either be careful to apply transforms when you need them, or to "un-apply" them before you don't need them.
You have to unrotate after drawing the cannon and before drawing the ball :)
You could try to save the transform before you do a rotate and then set it back again. This example is from setTransform in the Java Docs:
// Get the current transform
AffineTransform saveAT = g2.getTransform();
// Perform transformation
g2d.transform(...);
// Render
g2d.draw(...);
// Restore original transform
g2d.setTransform(saveAT);

Categories